【问题标题】:How to add commentaries to Glue on an AWS EMR using Pyspark如何使用 Pyspark 在 AWS EMR 上向 Glue 添加评论
【发布时间】:2021-10-29 11:37:31
【问题描述】:
我遇到了一个问题,我找不到使用 Pyspark 保存胶水元数据注释的方法。
目前我使用以下方法创建新表:
df.write \
.saveAsTable(
'db_temp.tb_temp',
format='parquet',
path='s3://datalake-123/table/df/',
mode='overwrite'
)
因此,如果可能的话,我想使用代码将 cmets 添加到胶水中,如下图所示:
【问题讨论】:
标签:
apache-spark
pyspark
amazon-emr
aws-glue
【解决方案1】:
您需要通过添加所需的注释来修改数据框的现有架构。架构修改后,使用修改后的架构创建新的数据框,并将数据框写入表格。
df = spark.createDataFrame([(1, 'abc'), (2, 'def')], ["id", "name"])
schema = StructType([StructField("id", IntegerType(), False, {"comment": "This is ID"}),
StructField("name", StringType(), True, {"comment": "This is name"})])
df_with_comment = spark.createDataFrame(df.rdd, schema)
df_with_comment.write.format('parquet').saveAsTable('mytable')
spark.sql('describe mytable').show()
+--------+---------+------------+
|col_name|data_type| comment|
+--------+---------+------------+
| id| int| This is ID|
| name| string|This is name|
+--------+---------+------------+