Spark 结构化流式处理中的镶木地板数据和分区问题



>我正在使用Spark Structured Streaming;我的数据帧具有以下架构

root 
 |-- data: struct (nullable = true) 
 |    |-- zoneId: string (nullable = true) 
 |    |-- deviceId: string (nullable = true) 
 |    |-- timeSinceLast: long (nullable = true) 
 |-- date: date (nullable = true) 

如何使用镶木地板格式进行写入流并写入数据(包含zoneId,deviceId,timeSinceLast;除日期以外的所有内容(并按日期对数据进行分区?我尝试了以下代码,分区 by 子句确实不工作

val query1 = df1 
  .writeStream 
  .format("parquet") 
  .option("path", "/Users/abc/hb_parquet/data") 
  .option("checkpointLocation", "/Users/abc/hb_parquet/checkpoint") 
  .partitionBy("data.zoneId") 
  .start() 
如果要

按日期分区,则必须partitionBy()方法中使用它。

val query1 = df1 
  .writeStream 
  .format("parquet") 
  .option("path", "/Users/abc/hb_parquet/data") 
  .option("checkpointLocation", "/Users/abc/hb_parquet/checkpoint") 
  .partitionBy("date") 
  .start()

如果要对按<year>/<month>/<day>结构化的数据进行分区,则应确保date列的类型DateType然后创建格式适当的列:

val df = dataset.withColumn("date", dataset.col("date").cast(DataTypes.DateType))
df.withColumn("year", functions.date_format(df.col("date"), "YYYY"))
  .withColumn("month", functions.date_format(df.col("date"), "MM"))
  .withColumn("day", functions.date_format(df.col("date"), "dd"))
  .writeStream 
  .format("parquet") 
  .option("path", "/Users/abc/hb_parquet/data") 
  .option("checkpointLocation", "/Users/abc/hb_parquet/checkpoint") 
  .partitionBy("year", "month", "day")
  .start()

我认为您应该尝试可以接受两种参数的方法repartition

  • 列名
  • 所需分区数。

我建议使用repartition("date")按日期对数据进行分区。

关于该主题的一个很好的链接:https://hackernoon.com/managing-spark-partitions-with-coalesce-and-repartition-4050c57ad5c4

相关内容

最新更新