我有帶有空陣列 (array (nullable = true)和element: (containsNull = true)) 的 JSON 檔案,我想將其轉換為鑲木地板檔案。這些空欄位會自動洗掉,而所有其他列都會按預期進行轉換。有沒有辦法用其他東西(例如["-"])替換空陣列?我在 AWS Glue 中運行我的代碼,但替換將使用純 PySpark 和資料幀進行。
資料型別仍然必須是 parquet 檔案中的陣列(只是不能使用空值)。
json:
{
"a":[],
"b":[1,2],
"c": "a string"
}
預期輸出:
a | b | c
["-"] | [1,2] | "a string"
當前腳本:
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
spark.conf.set("spark.sql.jsonGenerator.ignoreNullFields", "false")
job = Job(glueContext)
job.init(args['JOB_NAME'], args)
inputDF = glueContext.create_dynamic_frame_from_options(connection_type="s3",
connection_options:{
"s3://my-bucket",
"recurse":True
},
format="json")
df = inputDF.toDF()
# replace empty arrays here
dynamic = DynamicFrame.fromDF(df, glueContext, "dynamic")
output = glueContext.write_dynamic_frame.from_options(frame=dynamic,
connection_type="s3",
connection_options={
"path":"s3://my-bucket"
},
format="parquet"
)
uj5u.com熱心網友回復:
不確定您使用的是哪個 spark 版本,我已經檢查了 spark 3.1.2 和 2.4.5,其中沒有忽略空陣列欄位。
您可以使用下面的行來獲得所需的結果,
df.withColumn('a', when(size('a')== 0, array(lit('-'))).otherwise(col('a'))).show()
--- ------ --------
| a| b| c|
--- ------ --------
|[-]|[1, 2]|a string|
--- ------ --------
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/416626.html
標籤:
上一篇:如何在sparkScala中使用日期格式MM/DD/YYYY..中的月份來比較2個資料幀?
下一篇:用影像填充100%的SVG路徑
