gpt4 book ai didi

apache-spark - Spark 结构化流中的 Parquet 数据和分区问题

转载 作者:行者123 更新时间:2023-12-02 03:39:37 25 4
gpt4 key购买 nike

我正在使用 Spark 结构化流;我的 DataFrame 具有以下架构

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

如何使用 Parquet 格式执行 writeStream 并写入数据(包含 zoneId、deviceId、timeSinceLast;除日期之外的所有内容)并按日期对数据进行分区?我尝试了下面的代码,partition 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()

关于apache-spark - Spark 结构化流中的 Parquet 数据和分区问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49088796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com