├── data └── sample.dat │ ├── _SUCCESS │ ├── ._SUCCESS.crc │ ├── part-00000 │ ├── part-00001 │ ├── part-00002 │ ├── part-00003 │ ├── part-00004 │ ├── part-00005 │ ├── part-00006 │ ├── part-00007 │ ├── .part-00000.crc │ ├── .part-00001.crc │ ├── .part-00002.crc │ ├── .part-00003.crc │ ├── .part-00004.crc │ ├── .part-00005.crc │ ├── .part-00006.crc │ └── .part-00007.crc ├── shell_local.sh ├── run_local.sh ├── shell_cluster.sh ├── run_cluster.sh ├── src ├── main │ ├── java │ │ └── com │ │ │ └── invincea │ │ │ └── spark │ │ │ └── hash │ │ │ └── example │ │ │ └── Example.java │ └── scala │ │ └── com │ │ └── invincea │ │ └── spark │ │ └── hash │ │ ├── Hasher.scala │ │ ├── LSHModel.scala │ │ ├── OpenPortDriver.scala │ │ └── LSH.scala └── test │ └── scala │ └── com │ └── invincea │ └── spark │ └── hash │ ├── LSHSuite.scala │ └── LocalSparkContext.scala ├── log4j.properties ├── pom.xml ├── README.md └── LICENSE /data/sample.dat/_SUCCESS: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /data/sample.dat/._SUCCESS.crc: -------------------------------------------------------------------------------- 1 | crc -------------------------------------------------------------------------------- /data/sample.dat/part-00000: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/part-00000 -------------------------------------------------------------------------------- /data/sample.dat/part-00001: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/part-00001 -------------------------------------------------------------------------------- /data/sample.dat/part-00002: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/part-00002 -------------------------------------------------------------------------------- /data/sample.dat/part-00003: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/part-00003 -------------------------------------------------------------------------------- /data/sample.dat/part-00004: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/part-00004 -------------------------------------------------------------------------------- /data/sample.dat/part-00005: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/part-00005 -------------------------------------------------------------------------------- /data/sample.dat/part-00006: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/part-00006 -------------------------------------------------------------------------------- /data/sample.dat/part-00007: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/part-00007 -------------------------------------------------------------------------------- /data/sample.dat/.part-00000.crc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/.part-00000.crc -------------------------------------------------------------------------------- /data/sample.dat/.part-00001.crc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/.part-00001.crc -------------------------------------------------------------------------------- /data/sample.dat/.part-00002.crc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/.part-00002.crc -------------------------------------------------------------------------------- /data/sample.dat/.part-00003.crc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/.part-00003.crc -------------------------------------------------------------------------------- /data/sample.dat/.part-00004.crc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/.part-00004.crc -------------------------------------------------------------------------------- /data/sample.dat/.part-00005.crc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/.part-00005.crc -------------------------------------------------------------------------------- /data/sample.dat/.part-00006.crc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/.part-00006.crc -------------------------------------------------------------------------------- /data/sample.dat/.part-00007.crc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mrsqueeze/spark-hash/HEAD/data/sample.dat/.part-00007.crc -------------------------------------------------------------------------------- /shell_local.sh: -------------------------------------------------------------------------------- 1 | $SPARK_HOME/bin/spark-shell \ 2 | --master local[*] \ 3 | --executor-memory 8G \ 4 | --driver-memory 8G \ 5 | --jars target/spark-hash-0.1.2.jar $@ 6 | -------------------------------------------------------------------------------- /run_local.sh: -------------------------------------------------------------------------------- 1 | $SPARK_HOME/bin/spark-submit \ 2 | --class "com.invincea.spark.hash.OpenPortApp" \ 3 | --master local[*] \ 4 | --executor-memory 8G \ 5 | --driver-memory 8G \ 6 | target/spark-hash-0.1.2.jar $@ 7 | -------------------------------------------------------------------------------- /shell_cluster.sh: -------------------------------------------------------------------------------- 1 | $SPARK_HOME/bin/spark-shell \ 2 | --master yarn-client \ 3 | --num-executors 8 \ 4 | --driver-memory 4g \ 5 | --executor-memory 400g \ 6 | --executor-cores 60 \ 7 | --jars target/spark-hash-0.1.2.jar $@ 8 | -------------------------------------------------------------------------------- /run_cluster.sh: -------------------------------------------------------------------------------- 1 | $SPARK_HOME/bin/spark-submit \ 2 | --class "com.invincea.spark.hash.OpenPortApp" \ 3 | --master yarn-client \ 4 | --num-executors 80 \ 5 | --driver-memory 4g \ 6 | --executor-memory 8g \ 7 | --executor-cores 16 \ 8 | target/target/spark-hash-0.1.2.jar $@ 9 | -------------------------------------------------------------------------------- /src/main/java/com/invincea/spark/hash/example/Example.java: -------------------------------------------------------------------------------- 1 | package com.invincea.spark.hash.example; 2 | 3 | 4 | /** 5 | * This class is just used to generate javadocs 6 | * https://issues.sonatype.org/browse/OSSRH-637 7 | */ 8 | public class Example { 9 | 10 | } 11 | 12 | -------------------------------------------------------------------------------- /log4j.properties: -------------------------------------------------------------------------------- 1 | # Set everything to be logged to the console 2 | log4j.rootCategory=ERROR, console 3 | log4j.appender.console=org.apache.log4j.ConsoleAppender 4 | log4j.appender.console.target=System.err 5 | log4j.appender.console.layout=org.apache.log4j.PatternLayout 6 | log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n 7 | 8 | # Settings to quiet third party logs that are too verbose 9 | log4j.logger.org.eclipse.jetty=WARN 10 | log4j.logger.org.eclipse.jetty.util.component.AbstractLifeCycle=ERROR 11 | log4j.logger.org.apache.spark.repl.SparkIMain$exprTyper=INFO 12 | log4j.logger.org.apache.spark.repl.SparkILoop$SparkILoopInterpreter=INFO 13 | -------------------------------------------------------------------------------- /src/main/scala/com/invincea/spark/hash/Hasher.scala: -------------------------------------------------------------------------------- 1 | package com.invincea.spark.hash 2 | 3 | import scala.util.Random 4 | import org.apache.spark.mllib.linalg.SparseVector 5 | 6 | /** 7 | * simple hashing function. defined by ints a, b, p, m 8 | * where a and b are seeds with a > 0. 9 | * p is a prime number, >= u (largest item in the universe) 10 | * m is the number of hash bins 11 | */ 12 | class Hasher(a : Int, b : Int, p : Int, m : Int) extends Serializable { 13 | 14 | override def toString(): String = "(" + a + ", " + b + ")"; 15 | 16 | def hash(x : Int) : Int = { 17 | ( ((a.longValue*x) + b) % p ).intValue % m 18 | } 19 | 20 | def minhash(v : SparseVector) : Int = { 21 | v.indices.map(i => hash(i)).min 22 | } 23 | 24 | } 25 | 26 | object Hasher { 27 | /** create a new instance providing p and m. a and b random numbers mod p */ 28 | def create(p : Int, m : Int) = new Hasher(a(p), b(p), p, m) 29 | 30 | 31 | 32 | /** create a seed "a" */ 33 | def a(p : Int) : Int = { 34 | val r = new Random().nextInt(p) 35 | if(r == 0) 36 | a(p) 37 | else 38 | r 39 | } 40 | 41 | /** create a seed "b" */ 42 | def b(p : Int) : Int = { 43 | new Random().nextInt(p) 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/test/scala/com/invincea/spark/hash/LSHSuite.scala: -------------------------------------------------------------------------------- 1 | package com.invincea.spark.hash 2 | 3 | import scala.util.Random 4 | 5 | import org.scalatest.FunSuite 6 | 7 | import org.apache.spark.mllib.linalg.{SparseVector, Vectors} 8 | 9 | class LSHSuite extends FunSuite with LocalSparkContext { 10 | test("simple test") { 11 | 12 | 13 | val data = List(List(21, 25, 80, 110, 143, 443), 14 | List(21, 25, 80, 110, 143, 443, 8080), 15 | List(80, 2121, 3306, 3389, 8080, 8443), 16 | List(13, 17, 21, 23, 80, 137, 443, 3306, 3389)) 17 | 18 | val rdd = sc.parallelize(data) 19 | 20 | //make sure we have 4 21 | assert(rdd.count() == 4) 22 | 23 | val vctr = rdd.map(r => (r.map(i => (i, 1.0)))).map(a => Vectors.sparse(65535, a).asInstanceOf[SparseVector]) 24 | 25 | //make sure we still have 4 26 | assert(vctr.count() == 4) 27 | 28 | 29 | val lsh = new LSH(data = vctr, p = 65537, m = 1000, numRows = 1000, numBands = 100, minClusterSize = 2) 30 | val model = lsh.run 31 | 32 | //this should return 1 cluster 33 | assert(model.clusters.count() == 1) 34 | 35 | //filter for clusters with jaccard score > 0.85. should still return 1 36 | assert(model.filter(0.85).clusters.count() == 1) 37 | 38 | } 39 | } -------------------------------------------------------------------------------- /src/test/scala/com/invincea/spark/hash/LocalSparkContext.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to the Apache Software Foundation (ASF) under one or more 3 | * contributor license agreements. See the NOTICE file distributed with 4 | * this work for additional information regarding copyright ownership. 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 6 | * (the "License"); you may not use this file except in compliance with 7 | * the License. You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | * 17 | * This code is a modified version of the original Spark 1.0.2 implementation. 18 | */ 19 | package com.invincea.spark.hash 20 | 21 | import org.scalatest.Suite 22 | import org.scalatest.BeforeAndAfterAll 23 | 24 | import org.apache.spark.{SparkConf, SparkContext} 25 | 26 | trait LocalSparkContext extends BeforeAndAfterAll { self: Suite => 27 | @transient var sc: SparkContext = _ 28 | 29 | override def beforeAll() { 30 | val conf = new SparkConf() 31 | .setMaster("local") 32 | .setAppName("test") 33 | sc = new SparkContext(conf) 34 | super.beforeAll() 35 | } 36 | 37 | override def afterAll() { 38 | if (sc != null) { 39 | sc.stop() 40 | } 41 | super.afterAll() 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/main/scala/com/invincea/spark/hash/LSHModel.scala: -------------------------------------------------------------------------------- 1 | package com.invincea.spark.hash 2 | 3 | import org.apache.spark.mllib.linalg.SparseVector 4 | import org.apache.spark.rdd.RDD 5 | import scala.collection.mutable.ListBuffer 6 | import org.apache.spark.SparkContext._ 7 | 8 | 9 | class LSHModel(p : Int, m : Int, numRows : Int) extends Serializable { 10 | 11 | /** generate rows hash functions */ 12 | private val _hashFunctions = ListBuffer[Hasher]() 13 | for (i <- 0 until numRows) 14 | _hashFunctions += Hasher.create(p, m) 15 | final val hashFunctions : List[(Hasher, Int)] = _hashFunctions.toList.zipWithIndex 16 | 17 | /** the signature matrix with (hashFunctions.size signatures) */ 18 | var signatureMatrix : RDD[List[Int]] = null 19 | 20 | /** the "bands" ((hash of List, band#), row#) */ 21 | var bands : RDD[((Int, Int), Iterable[Long])] = null 22 | 23 | /** (vector id, cluster id) */ 24 | var vector_cluster : RDD[(Long, Long)] = null 25 | 26 | /** (cluster id, vector id) */ 27 | var cluster_vector : RDD[(Long, Long)] = null 28 | 29 | /** (cluster id, List(Vector) */ 30 | var clusters : RDD[(Long, Iterable[SparseVector])] = null 31 | 32 | /** jaccard cluster scores */ 33 | var scores : RDD[(Long, Double)] = null 34 | 35 | /** filter out scores below threshold. this is an optional step.*/ 36 | def filter(score : Double) : LSHModel = { 37 | 38 | val scores_filtered = scores.filter(x => x._2 > score) 39 | val clusters_filtered = scores_filtered.join(clusters).map(x => (x._1, x._2._2)) 40 | val cluster_vector_filtered = scores_filtered.join(cluster_vector).map(x => (x._1, x._2._2)) 41 | scores = scores_filtered 42 | clusters = clusters_filtered 43 | cluster_vector = cluster_vector_filtered 44 | vector_cluster = cluster_vector.map(x => x.swap) 45 | this 46 | } 47 | 48 | //def compare(SparseVector v) : RDD 49 | 50 | 51 | } -------------------------------------------------------------------------------- /src/main/scala/com/invincea/spark/hash/OpenPortDriver.scala: -------------------------------------------------------------------------------- 1 | package com.invincea.spark.hash 2 | 3 | import org.apache.spark.SparkContext 4 | import org.apache.spark.SparkContext._ 5 | import org.apache.spark.SparkConf 6 | import org.apache.spark.mllib.linalg.{Vectors, SparseVector} 7 | import java.io._ 8 | 9 | object OpenPortApp { 10 | def main(args: Array[String]) { 11 | 12 | if(args.length != 3) { 13 | println("Usage: OpenPortApp ") 14 | System.exit(1) 15 | } 16 | 17 | //parse input arguments 18 | val file : String = args(0) 19 | val partitions : Int = args(1).toInt 20 | val data_sample : Float = args(2).toFloat 21 | 22 | //spark job setup 23 | val conf = new SparkConf().setAppName("Open Port LSH") 24 | val sc = new SparkContext(conf) 25 | 26 | //read data file in as a RDD, partition RDD across cores 27 | val port_set : org.apache.spark.rdd.RDD[(List[Int], Int)] = sc.objectFile(file).repartition(partitions) 28 | 29 | //remove any port sets less than size 30 | val port_set_filtered = port_set.filter(tpl => tpl._1.size >= 2) 31 | 32 | //take a random sample of the data 33 | val sample = port_set_filtered.sample(false, data_sample).repartition(partitions) 34 | 35 | //include index with each point sample 36 | val points = sample.zipWithIndex().map(x => x.swap) 37 | 38 | //convert points to vectors 39 | val vctr = sample.map(r => (r._1.map(i => (i, 1.0)))).map(a => Vectors.sparse(65535, a).asInstanceOf[SparseVector]) 40 | 41 | //run locality sensitive hashing 42 | val lsh = new LSH(data = vctr, p = 65537, m = 1000, numRows = 1000, numBands = 25, minClusterSize = 2) 43 | val model = lsh.run 44 | 45 | 46 | println("samples: " + sample.count()) 47 | println("clusters: " + model.clusters.count()) 48 | 49 | //-- generate a CSV of the results 50 | //-- cluster original points with scores 51 | val points_clustered_scores = points.join(model.vector_cluster).map(x => (x._2._2, x._2._1)).groupByKey().join(model.scores) 52 | 53 | //--port list union, cluster size, # ip addresses, 54 | val score_coverage_size = points_clustered_scores.map(x => (x._2._2,x._2._1.foldLeft(List())((a1, b1) => a1.union(b1._1.asInstanceOf[List[Nothing]])).distinct.size, x._2._1.size, x._2._1.foldLeft(0)((a,b) => a + b._2))).collect() 55 | 56 | //write out the results 57 | val writer = new PrintWriter(new File("results.csv" )) 58 | writer.write("score,port_coverage,cluster_size,num_ips\n") 59 | score_coverage_size.foreach(x => writer.write(x._1 + "," + x._2 + "," + x._3 + "," + x._4 +"\n")) 60 | 61 | writer.flush 62 | writer.close 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/scala/com/invincea/spark/hash/LSH.scala: -------------------------------------------------------------------------------- 1 | package com.invincea.spark.hash 2 | 3 | import org.apache.spark.mllib.linalg.SparseVector 4 | import org.apache.spark.rdd.RDD 5 | import scala.collection.mutable.ListBuffer 6 | import org.apache.spark.SparkContext._ 7 | 8 | class LSH(data : RDD[SparseVector], p : Int, m : Int, numRows : Int, numBands : Int, minClusterSize : Int) extends Serializable { 9 | 10 | /** run LSH using the constructor parameters */ 11 | def run() : LSHModel = { 12 | 13 | //create a new model object 14 | val model = new LSHModel(p, m, numRows) 15 | 16 | //preserve vector index 17 | val zdata = data.zipWithIndex().cache() 18 | 19 | //compute signatures from matrix 20 | // - hash each vector times 21 | // - position hashes into bands. we'll later group these signature bins and has them as well 22 | //this gives us ((vector idx, band#), minhash) 23 | val signatures = zdata.flatMap(v => model.hashFunctions.flatMap(h => List(((v._2, h._2 % numBands),h._1.minhash(v._1))))).cache() 24 | 25 | //reorganize data for shuffle 26 | //this gives us ((band#, hash of minhash list), vector id) 27 | //groupByKey gives us items that hash together in the same band 28 | model.bands = signatures.groupByKey().map(x => ((x._1._2, x._2.hashCode), x._1._1)).groupByKey().cache() 29 | 30 | //we only want groups of size >= 31 | //(vector id, cluster id) 32 | model.vector_cluster = model.bands.filter(x => x._2.size >= minClusterSize).map(x => x._2.toList.sorted).distinct().zipWithIndex().map(x => x._1.map(y => (y.asInstanceOf[Long], x._2))).flatMap(x => x.grouped(1)).map(x => x(0)).cache() 33 | 34 | //(cluster id, vector id) 35 | model.cluster_vector = model.vector_cluster.map(x => x.swap).cache() 36 | 37 | //(cluster id, List(vector)) 38 | model.clusters = zdata.map(x => x.swap).join(model.vector_cluster).map(x => (x._2._2, x._2._1)).groupByKey().cache() 39 | 40 | //compute the jaccard similarity of each cluster 41 | model.scores = model.clusters.map(row => (row._1, jaccard(row._2.toList))).cache() 42 | 43 | model 44 | } 45 | 46 | /** compute a single vector against an existing model */ 47 | def compute(data : SparseVector, model : LSHModel, minScore : Double) : RDD[(Long, Iterable[SparseVector])] = { 48 | model.clusters.map(x => (x._1, x._2++List(data))).filter(x => jaccard(x._2.toList) >= minScore) 49 | } 50 | 51 | /** compute jaccard between two vectors */ 52 | def jaccard(a : SparseVector, b : SparseVector) : Double = { 53 | val al = a.indices.toList 54 | val bl = b.indices.toList 55 | al.intersect(bl).size / al.union(bl).size.doubleValue 56 | } 57 | 58 | /** compute jaccard similarity over a list of vectors */ 59 | def jaccard(l : List[SparseVector]) : Double = { 60 | l.foldLeft(l(0).indices.toList)((a1, b1) => a1.intersect(b1.indices.toList.asInstanceOf[List[Nothing]])).size / 61 | l.foldLeft(List())((a1, b1) => a1.union(b1.indices.toList.asInstanceOf[List[Nothing]])).distinct.size.doubleValue 62 | } 63 | 64 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | spark-hash 5 | com.invincea 6 | spark-hash 7 | scalable set hashing algorithms 8 | 0.1.3 9 | https://github.com/mrsqueeze/spark-hash 10 | 11 | 12 | Apache 2.0 License 13 | http://www.apache.org/licenses/LICENSE-2.0.html 14 | repo 15 | 16 | 17 | 18 | scm:git:git@github.com:mrsqueeze/spark-hash.git 19 | scm:git:https://github.com/mrsqueeze/spark-hash.git 20 | scm:git:git@github.com:mrsqueeze/spark-hash.gitt 21 | HEAD 22 | 23 | 24 | 25 | mrsqueeze 26 | Michael Orr 27 | michael.d.orr@gmail.com 28 | www.linkedin.com/pub/michael-orr/30/b33/7a9 29 | Invincea, Inc. 30 | http://www.invincea.com 31 | 32 | 33 | 34 | 2.10.4 35 | 2.10 36 | 1.7 37 | 1.4.1 38 | UTF-8 39 | UTF-8 40 | newest,nearest 41 | 42 | 43 | 44 | org.apache.logging.log4j 45 | log4j-api 46 | 2.1 47 | 48 | 49 | org.apache.logging.log4j 50 | log4j-core 51 | 2.1 52 | 53 | 54 | org.apache.spark 55 | spark-core_${scala.binary.version} 56 | 1.4.1 57 | 58 | 59 | org.apache.spark 60 | spark-sql_${scala.binary.version} 61 | 1.4.1 62 | 63 | 64 | org.apache.spark 65 | spark-mllib_${scala.binary.version} 66 | 1.4.1 67 | 68 | 69 | org.scalatest 70 | scalatest_${scala.binary.version} 71 | 2.2.0 72 | test 73 | 74 | 75 | org.scalacheck 76 | scalacheck_${scala.binary.version} 77 | 1.11.3 78 | test 79 | 80 | 81 | junit 82 | junit 83 | 4.10 84 | test 85 | 86 | 87 | 88 | target/scala-${scala.binary.version}/classes 89 | target/scala-${scala.binary.version}/test-classes 90 | 91 | 92 | org.scala-tools 93 | maven-scala-plugin 94 | 95 | 96 | compile 97 | 98 | compile 99 | 100 | compile 101 | 102 | 103 | test-compile 104 | 105 | testCompile 106 | 107 | test-compile 108 | 109 | 110 | process-resources 111 | 112 | compile 113 | 114 | 115 | 116 | 117 | 118 | maven-compiler-plugin 119 | 120 | 1.5 121 | 1.5 122 | 123 | 124 | 125 | 126 | org.apache.maven.plugins 127 | maven-surefire-plugin 128 | 2.7 129 | 130 | true 131 | 132 | 133 | 134 | 135 | org.scalatest 136 | scalatest-maven-plugin 137 | 1.0 138 | 139 | ${project.build.directory}/surefire-reports 140 | . 141 | WDF TestSuite.txt 142 | 143 | 144 | 145 | test 146 | 147 | test 148 | 149 | 150 | 151 | 152 | 153 | org.apache.maven.plugins 154 | maven-dependency-plugin 155 | 2.8 156 | 157 | 158 | copy-dependencies 159 | package 160 | 161 | copy-dependencies 162 | 163 | 164 | false 165 | false 166 | true 167 | 168 | 169 | 170 | 171 | 172 | org.apache.maven.plugins 173 | maven-source-plugin 174 | 2.2.1 175 | 176 | 177 | attach-sources 178 | 179 | jar-no-fork 180 | 181 | 182 | 183 | 184 | 185 | org.apache.maven.plugins 186 | maven-javadoc-plugin 187 | 2.9.1 188 | 189 | 190 | attach-javadocs 191 | 192 | jar 193 | 194 | 195 | 196 | 197 | 198 | org.apache.maven.plugins 199 | maven-gpg-plugin 200 | 1.5 201 | 202 | 203 | sign-artifacts 204 | verify 205 | 206 | sign 207 | 208 | 209 | 210 | 211 | 212 | org.sonatype.plugins 213 | nexus-staging-maven-plugin 214 | 1.6.3 215 | true 216 | 217 | ossrh 218 | https://oss.sonatype.org/ 219 | true 220 | 221 | 222 | 223 | 224 | 225 | 226 | ossrh 227 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 228 | 229 | 230 | ossrh 231 | https://oss.sonatype.org/content/repositories/snapshots 232 | 233 | 234 | 235 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | spark-hash 2 | ============================== 3 | 4 | Locality sensitive hashing for [Apache Spark](http://spark.apache.org/). 5 | This implementation was largely based on the algorithm described in chapter 3 of [Mining of Massive Datasets](http://mmds.org/) with some modifications for use in spark. 6 | 7 | Maven Central Repository 8 | -------------- 9 | spark-hash is on maven central and is accessible at: 10 | 11 | 12 | com.invincea 13 | spark-hash 14 | 0.1.3 15 | 16 | 17 | Building 18 | -------------- 19 | spark-hash can be built using maven 20 | 21 | > mvn clean package 22 | 23 | **Note: to use the scripts included in this release, make sure $SPARK_HOME is set** 24 | 25 | 26 | Example Data 27 | ----- 28 | We executed some nmap probes and now would like to group IP addresses with similar sets of exposed ports. An small example of this dataset has been provided in data/sample.dat for illustration. 29 | 30 | As part of the preprocessing which was performed on nmap xml files, we flattened IP addresses with identical port sets. The flattening did not factor the time when the port was open. This processing resulted in the following dataset which is highlighted with the Spark REPL in local mode: 31 | 32 | bash$ ./shell_local.sh 33 | scala> val port_set : org.apache.spark.rdd.RDD[(List[Int], Int)] = sc.objectFile("data/sample.dat") 34 | 35 | scala> port_set.take(5).foreach(println) 36 | (List(21, 23, 443, 8000, 8080),1) 37 | (List(80, 3389, 49152, 49153, 49154, 49155, 49156, 49157),9) 38 | (List(21, 23, 80, 2000, 3000),13) 39 | (List(1723),1) 40 | (List(3389),1) 41 | 42 | Each row in the RDD is a Tuple2 containing the open ports alone with the number of distinct IP addresses that had those ports. In the above example, 13 IP addresses had ports 21, 23, 80, 2000, 3000 opened. 43 | 44 | Count the RDD for the total dataset size: 45 | 46 | scala> port_set.count() 47 | res1: Long = 261 48 | 49 | Count the total number of IP addresses that contributed to this dataset: 50 | 51 | scala> port_set.map(x => x._2).reduce(_ + _) 52 | res2: Int = 2273 53 | 54 | Show the top five port sets sorted by IP count: 55 | 56 | scala> port_set.sortBy(_._2, false).take(5).foreach(println) 57 | (List(21, 23, 80),496) 58 | (List(22, 53, 80),289) 59 | (List(80, 443),271) 60 | (List(80),228) 61 | (List(22, 53, 80, 443),186) 62 | 63 | Filter the dataset to drop port sets smaller than 2. Show the top results: 64 | 65 | scala> val port_set_filtered = port_set.filter(tpl => tpl._1.size >= 2) 66 | scala> port_set_filtered.sortBy(_._2, false).take(5).foreach(println) 67 | (List(21, 23, 80),496) 68 | (List(22, 53, 80),289) 69 | (List(80, 443),271) 70 | (List(22, 53, 80, 443),186) 71 | (List(21, 22, 23, 80, 2000),73) 72 | 73 | Show the top three port sets by set size: 74 | 75 | scala> port_set.sortBy(_._1.size, false).take(3).foreach(println) 76 | (List(13, 21, 37, 80, 113, 443, 1025, 1026, 1027, 2121, 3000, 5000, 5101, 5631),1) 77 | (List(21, 80, 443, 1723, 3306, 5800, 5900, 8081, 49152, 49153, 49154, 49155),1) 78 | (List(21, 22, 26, 80, 110, 143, 443, 465, 587, 993, 995, 3306),3) 79 | 80 | 81 | Implementation Details 82 | ---- 83 | 84 | Implementation of LSH follows the rough steps 85 | 86 | 1. minhash each vector some number of times. The number of times to hash is an input parameter. The hashing function is defined in com.invincea.spark.hash.Hasher. Essentially each element of the input vector is hashed and the minimum hash value for the vector is returned. Minhashing produces a set of signatures for each vector. 87 | 2. Chop up each vector's minhash signatures into bands where each band contains an equal number of signatures. Bands with a greater number of signatures will produce clusters with *greater* similarity. A greater number of bands will increase the probabilty that similar vector signatures hash to the same value. 88 | 3. Order each of the vector bands such that for each band the vector's data for that band are grouped together. 89 | 4. Hash each band and group similar values. These similar values for a given band that hash to the same value are added to the result set. 90 | 5. Optionally filter results. An example operation would be to filter out singleton sets. 91 | 92 | #### Example 93 | 94 | Input data 95 | 96 | 1. [21, 25, 80, 110, 143, 443] 97 | 2. [21, 25, 80, 110, 143, 443, 8080] 98 | 3. [80, 2121, 3306, 3389, 8080, 8443] 99 | 4. [13, 17, 21, 23, 80, 137, 443, 3306, 3389] 100 | 101 | Let's hash each vector 1000 times. To do this we'll need to create 1000 hash functions and minhash each vector 1000 times. 102 | 103 | 1. 1000 minhash signatures 104 | 2. 1000 minhash signatures 105 | 3. 1000 minhash signatures 106 | 4. 1000 minhash signatures 107 | 108 | Now we want to chop up the signatures into 100 bands where each band will have 10 elements. 109 | 110 | 1. band 1 (10 elements), band 2 (10 elements), ... , band 100 (10 elements) 111 | 2. band 1 (10 elements), band 2 (10 elements), ... , band 100 (10 elements) 112 | 3. band 1 (10 elements), band 2 (10 elements), ... , band 100 (10 elements) 113 | 4. band 1 (10 elements), band 2 (10 elements), ... , band 100 (10 elements) 114 | 115 | For each of the 4 sets of bands, group all of band 1, band 2, .... band 4 116 | 117 | band 1: vector 1 (10 elements), ... , vector 4 (10 elements) 118 | 119 | band 2: vector 1 (10 elements), ... , vector 4 (10 elements) 120 | 121 | band 100: vector 1 (10 elements), ... , vector 4 (10 elements) 122 | 123 | For each band, hash each of 10 element signatures. 124 | 125 | band 1: 10, 10, 3, 4 126 | 127 | band 2: 6, 5, 2, 4 128 | 129 | band 100: 1, 2, 3, 8 130 | 131 | Group identical values within each band. This correspond to the similar clusters. In the above example, only two vectors (1 and 2) are deemed similar. 132 | 133 | scala> model.clusters.foreach(println) 134 | (0,CompactBuffer((65535,[21,25,80,110,143,443],[1.0,1.0,1.0,1.0,1.0,1.0]), (65535,[21,25,80,110,143,443,8080],[1.0,1.0,1.0,1.0,1.0,1.0,1.0]))) 135 | 136 | 137 | Usage 138 | ----- 139 | 140 | The previously described data can easily be converted into Spark's SparseVector class with the open port corresponding to an index in the vector. With that in mind, there are a few domain specific tunables that will need to be set prior to running LSH 141 | 142 | - *p* - a prime number > the largest vector index. In the case of open ports, this number is set to 65537. 143 | 144 | - *m* - the number of "bins" to hash data into. Smaller numbers increase collisions. We use 1000. 145 | 146 | - *numRows* - the total number of times to minhash a vector. *numRows* separate hash functions are generated. Larger numbers produce more samples and increase the likelihood similar vectors will hash together. 147 | 148 | - *numBands* - how many times to chop *numRows*. Each band will have *numRows*/*numBand* hash signatures. The larger number of elements the higher confidence in vector similarity. 149 | 150 | - *minClusterSize* - a post processing filter function that excludes clusters below a threshold. 151 | 152 | There are two ways to execute LSH. The first being a driver class that is submitted to a spark cluster (can be a single machine running in local mode). The second is using spark's REPL. The later is useful for parameter tuning. 153 | 154 | 155 | ### Driver Class 156 | 157 | Executing a driver in local mode. This executes com.invincea.sparki.hash.OpenPortDriver and saves the resulting cluster output to the file results.csv. In this case 50% of the data is sampled and spread over 8 partitions. For normal use the driver will need to be modified to handle data load, requisite transforms, and parameter tuning. Also, for some datasets it may not be practical to save all results to a local driver. 158 | 159 | bash$ ./run_local.sh 160 | Usage: OpenPortApp 161 | 162 | bash$ ./run_local.sh data/sample.dat 8 0.5 163 | 164 | ### Spark REPL 165 | 166 | bash$ ./shell_local.sh 167 | spark> 168 | import com.invincea.spark.hash.{LSH, LSHModel} 169 | import org.apache.spark.mllib.linalg.{Vector, Vectors, SparseVector} 170 | import org.apache.spark.mllib.linalg.{Matrix, Matrices} 171 | 172 | 173 | val port_set : org.apache.spark.rdd.RDD[(List[Int], Int)] = sc.objectFile("data/sample.dat") 174 | port_set.repartition(8) 175 | val port_set_filtered = port_set.filter(tpl => tpl._1.size > 3) 176 | 177 | val vctr = port_set_filtered.map(r => (r._1.map(i => (i, 1.0)))).map(a => Vectors.sparse(65535, a).asInstanceOf[SparseVector]) 178 | 179 | 180 | val lsh = new LSH(data = vctr, p = 65537, m = 1000, numRows = 1000, numBands = 25, minClusterSize = 2) 181 | val model = lsh.run 182 | 183 | spark> model.clusters.count() 184 | res3: Long = 80648 185 | 186 | ### Finding similar sets for a new point 187 | 188 | val np = List(21, 23, 80, 2000, 8443) 189 | val nv = Vectors.sparse(65535, np.map(x => (x, 1.0))).asInstanceOf[SparseVector] 190 | 191 | //use jaccard score of 0.50 across the entire cluster. This may be a bit harsh for large tests. 192 | scala> val sim = lsh.compute(nv, model, 0.50) 193 | 194 | scala> sim.count() 195 | res6: Long = 5 196 | 197 | scala> sim.collect().foreach(println) 198 | (9,List((65535,[21,22,23,80,2000,3389,8000],[1.0,1.0,1.0,1.0,1.0,1.0,1.0]), (65535,[21,22,23,80,2000,3389],[1.0,1.0,1.0,1.0,1.0,1.0]), (65535,[21,23,80,2000,8443],[1.0,1.0,1.0,1.0,1.0]))) 199 | (4,List((65535,[21,23,80,81,2000],[1.0,1.0,1.0,1.0,1.0]), (65535,[21,23,80,2000,8081],[1.0,1.0,1.0,1.0,1.0]), (65535,[21,23,80,2000,8443],[1.0,1.0,1.0,1.0,1.0]))) 200 | (5,List((65535,[21,22,23,80,1723,2000,8000],[1.0,1.0,1.0,1.0,1.0,1.0,1.0]), (65535,[21,22,23,80,1723,2000],[1.0,1.0,1.0,1.0,1.0,1.0]), (65535,[21,23,80,2000,8443],[1.0,1.0,1.0,1.0,1.0]))) 201 | (6,List((65535,[21,22,23,53,80,2000,8000],[1.0,1.0,1.0,1.0,1.0,1.0,1.0]), (65535,[21,22,23,53,80,2000],[1.0,1.0,1.0,1.0,1.0,1.0]), (65535,[21,23,80,2000,8443],[1.0,1.0,1.0,1.0,1.0]))) 202 | (7,List((65535,[21,22,23,80,554,2000],[1.0,1.0,1.0,1.0,1.0,1.0]), (65535,[21,22,23,80,554,2000,8000],[1.0,1.0,1.0,1.0,1.0,1.0,1.0]), (65535,[21,23,80,2000,8443],[1.0,1.0,1.0,1.0,1.0]))) 203 | 204 | ### Spark REPL (cluster mode) 205 | 206 | bash$ ./shell_cluster.sh 207 | import org.apache.spark.SparkContext 208 | import org.apache.spark.SparkContext._ 209 | import org.apache.spark.SparkConf 210 | import org.apache.spark.mllib.linalg.{Vectors, SparseVector} 211 | import java.io._ 212 | import com.invincea.spark.hash.{LSH, LSHModel} 213 | 214 | val port_set : org.apache.spark.rdd.RDD[(List[Int], Int)] = sc.objectFile("sample.dat").repartition(320) 215 | val port_set_filtered = port_set.filter(tpl => tpl._1.size >= 3) 216 | val points = port_set.zipWithIndex().map(x => x.swap) 217 | val vctr = port_set.map(r => (r._1.map(i => (i, 1.0)))).map(a => Vectors.sparse(65535, a).asInstanceOf[SparseVector]) 218 | 219 | val lsh = new LSH(data = vctr, p = 65537, m = 1000, numRows = 2000, numBands = 100, minClusterSize = 2) 220 | val model = lsh.run 221 | 222 | ### Finding the largest cluster 223 | 224 | //by count 225 | model.clusters.sortBy(_._2.size, false).map(x => (x._1, x._2.size)).take(5).foreach(println) 226 | 227 | //retrieve underlying data 228 | model.clusters.sortBy(_._2.size, false).take(5).foreach(println) 229 | 230 | 231 | ### Tuning Results 232 | As described in the MMDS book, LSH can be tuned by adjusting the number of rows and bands such that: 233 | 234 | threshold = Math.pow(1/bands),(1/(rows/bands)) 235 | 236 | Naturally, the number of rows, bands, and the resulting size of the band (rows/bands) dictates the quality of results yielded by LSH. Higher thresholds produces clusters with higher similarity. Lower thresholds typically produce more clusters but sacrifices similarity. 237 | 238 | Regardless of parameters, it may be good to independently verify each cluster. One such verification method is to calculate the jaccard similarity of the cluster (it is a set of sets). Implementation of jaccard similarity is provided in com.invincea.spark.hash.LSH.jaccard. 239 | 240 | 241 | 242 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------