python - AWS Glue PySpark:将表示为字符串的字典拆分为多行
问题描述
我正在处理大型数据集,其中我的记录具有以下形式
uniqueId col1 col2 col3 Levels
1 A1 A2 A3 {"2019-01-01":1.1 ,"2019-01-02":2.1 ,"2019-01-03":3.1}
2 B1 B2 B3 {"2019-01-01":1.2 ,"2019-01-03":3.2}
3 C1 C2 C3 {"2019-01-04":4.3}
'Levels' 存储为字符串类型。
我正在尝试拆分Levels
为行,以便获得如下输出:
uniqueId col1 col2 col3 date value
1 A1 A2 A3 2019-01-01 1.2
1 A1 A2 A3 2019-01-02 2.1
1 A1 A2 A3 2019-01-03 3.1
2 B1 B2 B3 2019-01-01 1.2
2 B1 B2 B3 2019-01-03 3.2
3 C1 C2 C3 2019-01-04 4.3
我正在尝试按照此处提出的解决方案在 Pyspark 中的 AWS Glue 上应用脚本,
@udf("map<string, string>")
def parse(s):
try:
return json.loads(s)
except json.JSONDecodeError:
pass
parse_udf = udf(parse)
datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "database", table_name = "table", transformation_ctx = "datasource0")
sparkDF = datasource0.toDF()
sparkDF2 = sparkDF.select("unique_id","col1","col2", "col3", explode(parse("levels")).alias("date", "value"))
GlueDF_tmp = DynamicFrame.fromDF(sparkDF2, glueContext, 'GlueDF_tmp')
GlueDF = GlueDF_tmp.apply_mapping([("unique_id", "string", "unique_id", "string"),
("col1", "string", "col1", "string"),
("col2", "string", "col2", "string"),
("col3", "string", "col3", "string"),
("date", "timestamp", "date", "timestamp"),
("value", "double", "value", "double")])
glueContext.write_dynamic_frame.from_options(frame = GlueDF, connection_type = "s3",
connection_options = {"path": "s3://..."},
format = "parquet",
transformation_ctx = "datasink0")
但是我遇到了这种类型的内存问题 AWS Glue - 无法设置 spark.yarn.executor.memoryOverhead
进行拆分的更好/更有效的方法是什么?
解决方案
推荐阅读
- c# - 英特尔 Fortran 模块数据互操作与 C#
- python - 有没有办法选择一些列的行值是预期的
- javascript - JS 中的语法错误
- reactjs - 意外的获取数据挂钩行为
- javascript - Vue JS如何绑定复选框?
- ruby-on-rails - rake db:setup 显示 rake 已中止!KeyError:找不到密钥:“URL_HOST”错误
- sqlite - 在 Xamarin Forms 中注入 CRUD 服务
- tomcat - 将tomcat服务器添加到Netbeans时出现问题
- html - 移动屏幕显示菜单项之间的大间隙
- php - Mysql 查询在本地主机上运行良好,但在服务器上运行良好