【问题标题】:How to read data from HDFS using spark streaming?如何使用火花流从 HDFS 读取数据?
【发布时间】:2018-08-20 02:21:46
【问题描述】:
JavaSparkContext sc = new JavaSparkContext(conf);
SQLContext sqlContext = new SQLContext(sc);
JavaStreamingContext ssc = new JavaStreamingContext(sc, new Duration(1000));

我的 HDFS 目录包含 json 文件

【问题讨论】:

标签: java apache-spark hdfs spark-streaming


【解决方案1】:

您可以使用textFileStream 将其作为文本文件读取并稍后转换。

val dstream = ssc.textFileStream("path to hdfs directory")

这会给你DStream[Strings],它是RDD[String]的集合

然后你可以得到每个时间间隔的RDD为

dstream.foreachRDD(rdd => {
  //now apply a transformation or anything with the each rdd
 spark.read.json(rdd) // to change it to dataframe
})

scc.start()             // Start the computation
ssc.awaitTermination()   // Wait for the computation to terminate

希望对你有帮助

【讨论】:

  • 很抱歉我在java方面不太好,但和这个相比不会有太大区别。
猜你喜欢
  • 1970-01-01
  • 2015-05-15
  • 2023-03-18
  • 2018-12-30
  • 2019-11-13
  • 2017-04-16
  • 2017-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多