0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 package org.apache.spark.examples.ml;
0019
0020 import org.apache.spark.sql.SparkSession;
0021
0022
0023 import java.util.Arrays;
0024 import java.util.List;
0025
0026 import org.apache.spark.ml.feature.MinHashLSH;
0027 import org.apache.spark.ml.feature.MinHashLSHModel;
0028 import org.apache.spark.ml.linalg.Vector;
0029 import org.apache.spark.ml.linalg.VectorUDT;
0030 import org.apache.spark.ml.linalg.Vectors;
0031 import org.apache.spark.sql.Dataset;
0032 import org.apache.spark.sql.Row;
0033 import org.apache.spark.sql.RowFactory;
0034 import org.apache.spark.sql.types.DataTypes;
0035 import org.apache.spark.sql.types.Metadata;
0036 import org.apache.spark.sql.types.StructField;
0037 import org.apache.spark.sql.types.StructType;
0038
0039 import static org.apache.spark.sql.functions.col;
0040
0041
0042
0043
0044
0045
0046
0047 public class JavaMinHashLSHExample {
0048 public static void main(String[] args) {
0049 SparkSession spark = SparkSession
0050 .builder()
0051 .appName("JavaMinHashLSHExample")
0052 .getOrCreate();
0053
0054
0055 List<Row> dataA = Arrays.asList(
0056 RowFactory.create(0, Vectors.sparse(6, new int[]{0, 1, 2}, new double[]{1.0, 1.0, 1.0})),
0057 RowFactory.create(1, Vectors.sparse(6, new int[]{2, 3, 4}, new double[]{1.0, 1.0, 1.0})),
0058 RowFactory.create(2, Vectors.sparse(6, new int[]{0, 2, 4}, new double[]{1.0, 1.0, 1.0}))
0059 );
0060
0061 List<Row> dataB = Arrays.asList(
0062 RowFactory.create(0, Vectors.sparse(6, new int[]{1, 3, 5}, new double[]{1.0, 1.0, 1.0})),
0063 RowFactory.create(1, Vectors.sparse(6, new int[]{2, 3, 5}, new double[]{1.0, 1.0, 1.0})),
0064 RowFactory.create(2, Vectors.sparse(6, new int[]{1, 2, 4}, new double[]{1.0, 1.0, 1.0}))
0065 );
0066
0067 StructType schema = new StructType(new StructField[]{
0068 new StructField("id", DataTypes.IntegerType, false, Metadata.empty()),
0069 new StructField("features", new VectorUDT(), false, Metadata.empty())
0070 });
0071 Dataset<Row> dfA = spark.createDataFrame(dataA, schema);
0072 Dataset<Row> dfB = spark.createDataFrame(dataB, schema);
0073
0074 int[] indices = {1, 3};
0075 double[] values = {1.0, 1.0};
0076 Vector key = Vectors.sparse(6, indices, values);
0077
0078 MinHashLSH mh = new MinHashLSH()
0079 .setNumHashTables(5)
0080 .setInputCol("features")
0081 .setOutputCol("hashes");
0082
0083 MinHashLSHModel model = mh.fit(dfA);
0084
0085
0086 System.out.println("The hashed dataset where hashed values are stored in the column 'hashes':");
0087 model.transform(dfA).show();
0088
0089
0090
0091
0092
0093 System.out.println("Approximately joining dfA and dfB on Jaccard distance smaller than 0.6:");
0094 model.approxSimilarityJoin(dfA, dfB, 0.6, "JaccardDistance")
0095 .select(col("datasetA.id").alias("idA"),
0096 col("datasetB.id").alias("idB"),
0097 col("JaccardDistance")).show();
0098
0099
0100
0101
0102
0103
0104
0105 System.out.println("Approximately searching dfA for 2 nearest neighbors of the key:");
0106 model.approxNearestNeighbors(dfA, key, 2).show();
0107
0108
0109 spark.stop();
0110 }
0111 }