如何在 Spark 中读取多个文本文件以进行文档聚类?

How to read multiple text files in Spark for document clustering?

我想从一个目录中读取多个文本文档进行文档聚类。 为此,我想将数据读取为:

SparkConf sparkConf = new SparkConf().setAppName(appName).setMaster("local[*]").set("spark.executor.memory", "2g");
JavaSparkContext context = new JavaSparkContext(sparkConf);
SparkSession spark = SparkSession.builder().config(sparkConf).getOrCreate();
Dataset<Row> dataset = spark.read().textFile("path to directory");

这里,我不想用
JavaPairRDD 数据 = context.wholeTextFiles(路径); 因为我想要数据集作为 return 类型。

在 Scala 中你可以这样写:

context.wholeTextFiles("...").toDS()

在java中您需要使用编码器。参见 the javadoc for more detail

JavaPairRDD<String, String> rdd = context.wholeTextFiles("hdfs:///tmp/test_read");
Encoder<Tuple2<String, String>> encoder = Encoders.tuple(Encoders.STRING(), Encoders.STRING());
spark.createDataset(rdd.rdd(), encoder).show();