【发布时间】:2019-10-29 09:31:12
【问题描述】:
我在 Databricks 工作,这是一个基于 Spark 的数据处理平台,具有类似 HDFS 的文件系统,所以基本上我相信那些熟悉 Spark 和 HDFS 的人在没有 Databricks 经验的情况下能够帮助我。
我使用结构化流从 Kafka 读取数据:
var streamingInputDF =
spark.readStream
.format("kafka")
.option("kafka.bootstrap.servers", "<XX.XX.XXX.XX:9092")
.option("subscribe", "answers")
.option("startingOffsets", "earliest")
.option("minPartitions", "1")
.option("failOnDataLoss", "true")
.load()
做一些转换:
val streamingSelectDF = streamingInputDF
.withWatermark("timestamp","1 days")
.select(explode(split($"value".cast("string"), "\\s+")).as("word"), col("timestamp"))
.groupBy(window($"timestamp", "1 minute"), $"word")
.count
.where("count >= 11")
然后我通过打印到控制台来确保有一些数据:
+--------------------+----+-----+
| window|word|count|
+--------------------+----+-----+
|[2019-06-10 14:33...| the| 763|
|[2019-06-09 20:48...| the| 523|
|[2019-06-10 14:33...| and| 489|
|[2019-06-10 14:33...| a| 479|
|[2019-06-08 19:07...| the| 435|
|[2019-06-10 14:33...| to| 430|
|[2019-06-10 14:33...| of| 365|
|[2019-06-09 20:48...| a| 314|
|[2019-06-09 20:48...| and| 303|
|[2019-06-09 20:48...| to| 285|
|[2019-06-10 14:33...| is| 272|
|[2019-06-08 19:07...| a| 264|
|[2019-06-08 19:07...| and| 250|
|[2019-06-08 19:07...| to| 233|
|[2019-06-09 20:48...| of| 231|
|[2019-06-10 14:33...| in| 219|
|[2019-06-10 14:33...|that| 211|
|[2019-06-08 19:07...| of| 186|
|[2019-06-10 14:33...| for| 166|
|[2019-06-09 20:48...| is| 158|
+--------------------+----+-----+
only showing top 20 rows
然后,我想将数据流式传输到 parquet 文件中:
val query =
streamingSelectDF
.writeStream
.format("parquet")
.option("path", "/mnt/kafka/answers")
.option("checkpointLocation", "/mnt/kafka/checkpoint")
.partitionBy("window")
.start()
但是没有创建文件。只创建了一个“kafka”目录:
ls /mnt/
path name size
dbfs:/mnt/kafka/ kafka/ 0
【问题讨论】:
-
尝试不分区。
-
不确定我是否会在 Streaming 中使用镶木地板。
标签: scala apache-spark parquet databricks spark-structured-streaming