This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object RunningLengthEncoding { | |
| def encode(s:String):String = { | |
| def encSeq(s:String):String = s.head.toString+s.length | |
| if (s.isEmpty()) "" else { | |
| val (same,rest) = s.span(_==s(0)); | |
| encSeq(same) ++ encode(rest) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object CaesarCodex { | |
| class CyclicCharRange(start:Char,end:Char) extends PartialFunction[Char,Int=>Char] { | |
| val size = end-start+1 | |
| def isDefinedAt(c:Char) = c>=start && c<=end | |
| def apply(c:Char): Int => Char = { amount => | |
| val rangeAmount = ((amount % size)+size) % size | |
| val shiftedValue = (((c-start)+rangeAmount)%size)+start | |
| shiftedValue.toChar | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| object MergeSort { | |
| def mergeSort[T](list: List[T])(implicit order: Ordering[T]): List[T] = { | |
| def merge[T](l1: List[T], l2: List[T])(implicit order: Ordering[T]): List[T] = { | |
| (l1, l2) match { | |
| case (Nil, Nil) => Nil | |
| case (Nil, l) => l | |
| case (l, Nil) => l | |
| case (x :: tail1, y :: tail2) => if (order.compare(x, y) > 0) y :: merge(l1, tail2) else x :: merge(tail1, l2) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def invoke1[T,U](obj:Any, method:Method)(param:T):U = method.invoke(obj,Seq(param.asInstanceOf[java.lang.Object]):_*) match { | |
| case x: java.lang.Object if x==null => null.asInstanceOf[U] | |
| case x => x.asInstanceOf[U] | |
| } | |
| def time[T](b: => T):(T, Long) = { | |
| val t0 = System.nanoTime() | |
| val res = b | |
| val t = System.nanoTime() - t0 | |
| (res,t ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** Adds support for Http/404 - Not Found to a processing pipeline. | |
| * A 404/Not found is converted to None while other HTTP statuses will get converted as usual | |
| * (HTTP/200 - will result in a marshalled object, errors will throw an exception, failing the future. | |
| */ | |
| def convertOption[T](implicit unmarshaller:FromResponseUnmarshaller[T]): Future[HttpResponse] => Future[Option[T]] = | |
| (futRes: Future[HttpResponse]) => futRes.map{res => | |
| if (res.status == StatusCodes.NotFound) None | |
| else Some(unmarshal[T](unmarshaller)(res)) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // given a list of elements and a predicate over those elements, | |
| // cluster the elements into a lists of lists of elements that fulfil the predicate, preserving the order of the original list. | |
| def cluster[T](l:List[T], cond:List[T] => Boolean):List[List[T]] = { | |
| def splitCond[T](l:List[T], cond:List[T]=>Boolean):(List[T], List[T]) = { | |
| def splitCond0(l:List[T], cond:List[T]=>Boolean, cumm:List[T]):(List[T], List[T]) = l match { | |
| case Nil => (cumm, Nil) | |
| case h::t => if (cond(h::cumm)) splitCond0(t, cond, h::cumm) else (cumm,l) | |
| } | |
| splitCond0(l,cond,Nil) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def saveToCassandra(sc:SparkContext, total:Int): Long = { | |
| val cas = CasBuilder.cql3.withColumnFamily("keyspace", "raw").onHost(host).onPort(port) | |
| .saveWithQuery("UPDATE keyspace.raw set metric=?, vals=?") | |
| implicit def TsToCasKeys(src:Array[String]): Map[String, ByteBuffer] = src match { | |
| case Array(device_id, aggregation_type, t0, metric, vals) => | |
| Map("device_id" -> device_id, "aggregation_type" -> aggregation_type, "t0" -> t0) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
| <modelVersion>4.0.0</modelVersion> | |
| <groupId>org.acme</groupId> | |
| <artifactId>spark-cassandra-assembly</artifactId> | |
| <version>1.0.0-SNAPSHOT</version> | |
| <packaging>jar</packaging> | |
| <name>A uber jar to easily use the Cassandra-Spark driver with spark-shell </name> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| -- Datamodel | |
| -- local keyspace | |
| CREATE KEYSPACE example | |
| WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 }; | |
| -- table schema | |
| CREATE TABLE example.words ( | |
| word text PRIMARY KEY, | |
| count int |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.