从 table 中导出多个文件
export many files from a table
我有一个 sql 查询生成一个 table 格式如下
|sex |country|popularity|
|null |null | x |
|null |value | x |
|value|null | x |
|value|null | x |
|null |value | x |
|value|value | x |
性别列的值可以是女人,男人
国家/地区的值可以是意大利、英国、美国等。
x 是一个整数
现在我想根据数据组合(value,null)保存四个文件。所以 file1 由(值,值)组成,用于列性别,国家/地区。
file2 由 (value,null) 组成,用于列性别、国家/地区。 file3 由 (null,value) 组成,file4 由
(空,空)。
我搜索了很多东西,但找不到任何有用的信息。我也试过下面的
val df1 = data.withColumn("combination",concat(col("sex") ,lit(","), col("country")))
df1.coalesce(1).write.partitionBy("combination").format("csv").option("header", "true").mode("overwrite").save("text.csv")
但我收到了更多文件,因为此命令会根据(性别国家)的所有可能数据生成文件。
同下
val df1 = data.withColumn("combination",concat(col("sex")))
df1.coalesce(1).write.partitionBy("combination").format("csv").option("header", "true").mode("overwrite").save("text.csv")
是否有任何类似于 partitionby 的命令可以为我提供成对 (value,null) 而不是列的组合?
您可以根据它们是否为空将列转换为布尔值,并连接成一个字符串,类似于“true_true”、“true_false”等
df = df.withColumn("coltype", concat(col("sex").isNull(), lit("_"), col("country").isNull()))
df.coalesce(1)
.write
.partitionBy("coltype")
.format("csv")
.option("header", "true")
.mode("overwrite")
.save("output")
我有一个 sql 查询生成一个 table 格式如下
|sex |country|popularity|
|null |null | x |
|null |value | x |
|value|null | x |
|value|null | x |
|null |value | x |
|value|value | x |
性别列的值可以是女人,男人
国家/地区的值可以是意大利、英国、美国等。
x 是一个整数
现在我想根据数据组合(value,null)保存四个文件。所以 file1 由(值,值)组成,用于列性别,国家/地区。 file2 由 (value,null) 组成,用于列性别、国家/地区。 file3 由 (null,value) 组成,file4 由 (空,空)。
我搜索了很多东西,但找不到任何有用的信息。我也试过下面的
val df1 = data.withColumn("combination",concat(col("sex") ,lit(","), col("country")))
df1.coalesce(1).write.partitionBy("combination").format("csv").option("header", "true").mode("overwrite").save("text.csv")
但我收到了更多文件,因为此命令会根据(性别国家)的所有可能数据生成文件。 同下
val df1 = data.withColumn("combination",concat(col("sex")))
df1.coalesce(1).write.partitionBy("combination").format("csv").option("header", "true").mode("overwrite").save("text.csv")
是否有任何类似于 partitionby 的命令可以为我提供成对 (value,null) 而不是列的组合?
您可以根据它们是否为空将列转换为布尔值,并连接成一个字符串,类似于“true_true”、“true_false”等
df = df.withColumn("coltype", concat(col("sex").isNull(), lit("_"), col("country").isNull()))
df.coalesce(1)
.write
.partitionBy("coltype")
.format("csv")
.option("header", "true")
.mode("overwrite")
.save("output")