{"id":20376345,"url":"https://github.com/tdebatty/spark-knn-graphs","last_synced_at":"2025-04-12T07:37:26.301Z","repository":{"id":33405185,"uuid":"37050384","full_name":"tdebatty/spark-knn-graphs","owner":"tdebatty","description":"Spark algorithms for building k-nn graphs","archived":false,"fork":false,"pushed_at":"2018-11-26T10:22:30.000Z","size":102547,"stargazers_count":42,"open_issues_count":7,"forks_count":15,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-26T02:51:08.604Z","etag":null,"topics":["algorithm","knn-graphs","lsh-superbit","nearest-neighbor-search","nn-descent","processing-knn-graphs","spark","spark-knn-graphs"],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tdebatty.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-08T06:43:21.000Z","updated_at":"2024-10-07T15:17:56.000Z","dependencies_parsed_at":"2022-09-17T08:20:36.792Z","dependency_job_id":null,"html_url":"https://github.com/tdebatty/spark-knn-graphs","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdebatty%2Fspark-knn-graphs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdebatty%2Fspark-knn-graphs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdebatty%2Fspark-knn-graphs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tdebatty%2Fspark-knn-graphs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tdebatty","download_url":"https://codeload.github.com/tdebatty/spark-knn-graphs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248536091,"owners_count":21120680,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","knn-graphs","lsh-superbit","nearest-neighbor-search","nn-descent","processing-knn-graphs","spark","spark-knn-graphs"],"created_at":"2024-11-15T01:36:54.186Z","updated_at":"2025-04-12T07:37:26.278Z","avatar_url":"https://github.com/tdebatty.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# spark-knn-graphs\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/info.debatty/spark-knn-graphs/badge.svg)](https://maven-badges.herokuapp.com/maven-central/info.debatty/spark-knn-graphs) [![Build Status](https://travis-ci.org/tdebatty/spark-knn-graphs.svg?branch=master)](https://travis-ci.org/tdebatty/spark-knn-graphs) [![Javadocs](http://www.javadoc.io/badge/info.debatty/spark-knn-graphs.svg)](http://www.javadoc.io/doc/info.debatty/spark-knn-graphs)\n\nSpark algorithms for building and processing k-nn graphs.\n\nCurrently implemented k-nn graph building algorithms:\n* Brute force\n* NN-Descent (which supports any similarity)\n* LSH SuperBit (for cosine similarity)\n* NNCTPH (for text datasets)\n* Fast online graph building\n\nImplemented k-nn graph processing algorithms:\n* Distributed exhaustive nearest neighbor search\n* Distributed graph based nearest neighbor search\n\n\nAll algorithms support custom classes as value. See [an example with custom class as value](https://github.com/tdebatty/spark-knn-graphs/blob/master/spark-knn-graphs/src/main/java/info/debatty/spark/knngraphs/example/NNDescentCustomValue.java).\n\n## Installation and requirements\n\nspark-knn-graphs requires **Spark 1.4.0** or above. It is currently tested with Spark versions **1.4.1**, **1.5.2**, **1.6.0** and **1.6.2**.\n\nInstallation using Maven:\n```\n\u003cdependency\u003e\n    \u003cgroupId\u003einfo.debatty\u003c/groupId\u003e\n    \u003cartifactId\u003espark-knn-graphs\u003c/artifactId\u003e\n    \u003cversion\u003eRELEASE\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\nOr check [Spark Packages](http://spark-packages.org/package/tdebatty/spark-knn-graphs)\n\n## Examples\nHere are only a few short examples. Check [the examples folder](https://github.com/tdebatty/spark-knn-graphs/tree/master/spark-knn-graphs/src/main/java/info/debatty/spark/knngraphs/example) for more examples and complete code.\n\n### NN-Descent\n```java\npublic class NNDescentExample {\n\n    public static void main(String[] args) {\n\n        // Configure spark instance\n        SparkConf conf = new SparkConf();\n        JavaSparkContext sc = new JavaSparkContext(conf);\n\n        // Create some nodes\n        // the value of the nodes will simply be an integer:\n        List\u003cNode\u003cInteger\u003e\u003e data = new ArrayList\u003cNode\u003cInteger\u003e\u003e();\n        for (int i = 0; i \u003c 1000; i++) {\n            data.add(new Node(String.valueOf(i), i));\n        }\n        JavaRDD\u003cNode\u003cInteger\u003e\u003e nodes = sc.parallelize(data);\n        \n        // Instanciate and configure NNDescent for Integer node values\n        NNDescent nndes = new NNDescent\u003cInteger\u003e();\n        nndes.setK(10);\n        nndes.setMaxIterations(10);\n        nndes.setSimilarity(new SimilarityInterface\u003cInteger\u003e() {\n\n                    // Define the similarity that will be used\n                    // in this case: 1 / (1 + delta)\n                    public double similarity(Integer value1, Integer value2) {\n\n                        // The value of nodes is an integer...\n                        return 1.0 / (1.0 + Math.abs((Integer) value1 - (Integer) value2));\n                    }\n        });\n        \n        // Compute the graph...\n        JavaPairRDD\u003cNode, NeighborList\u003e graph = nndes.computeGraph(nodes);\n        \n        // BTW: until now graph is only an execution plan and nothing has been\n        // executed by the spark cluster...\n        \n        // This will actually compute the graph...\n        double total_similarity = graph.aggregate(\n                0.0,\n                new  Function2\u003cDouble,Tuple2\u003cNode,NeighborList\u003e,Double\u003e() {\n\n                    public Double call(Double val, Tuple2\u003cNode, NeighborList\u003e tuple) throws Exception {\n                        for (Neighbor n : tuple._2()) {\n                            val += n.similarity;\n                        }\n                        \n                        return val;\n                    }\n                },\n                new Function2\u003cDouble, Double, Double\u003e() {\n\n                    public Double call(Double val0, Double val1) throws Exception {\n                        return val0 + val1;\n                    }\n                    \n                });\n        \n        System.out.println(\"Total sim: \" + total_similarity);\n        System.out.println(graph.first());\n    }\n}\n```\n\n## LSH SuperBit\n\n```java\npublic class LSHSuperBitExample {\n\n    public static void main(String[] args) {\n        \n        // Configure spark instance\n        SparkConf conf = new SparkConf();\n        JavaSparkContext sc = new JavaSparkContext(conf);\n        \n        // Create some nodes consisting of double[]\n        int d = 100; // dimensions\n        int n = 1000; // items\n        Random r = new Random();\n        List\u003cNode\u003cdouble[]\u003e\u003e data = new ArrayList\u003cNode\u003cdouble[]\u003e\u003e();\n        for (int i = 0; i \u003c n; i++) {\n            double[] vector = new double[d];\n            for (int j = 0; j \u003c d; j++) {\n                vector[j] = r.nextDouble() * 100;\n            }\n            \n            data.add(new Node(String.valueOf(i), vector));\n        }\n        JavaRDD\u003cNode\u003cdouble[]\u003e\u003e nodes = sc.parallelize(data);\n        \n        // Configure LSHSuperBit graph builder\n        LSHSuperBitDoubleArray gbuilder = new LSHSuperBitDoubleArray();\n        gbuilder.setK(10);\n        gbuilder.setStages(2);\n        gbuilder.setBuckets(10);\n        // LSH hashing requires the dimensionality\n        gbuilder.setDim(d);\n        \n        // Build the graph...\n        JavaPairRDD\u003cNode\u003cdouble[]\u003e, NeighborList\u003e graph = gbuilder.computeGraph(nodes);\n        System.out.println(graph.first());\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftdebatty%2Fspark-knn-graphs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftdebatty%2Fspark-knn-graphs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftdebatty%2Fspark-knn-graphs/lists"}