AWS Glue PySpark 替换 NULL

use*_*122 7 pyspark aws-glue

我正在运行 AWS Glue 作业,使用 Glue 自动生成的 PySpark 脚本将 S3 上的管道分隔文件加载到 RDS Postgres 实例中。

最初,它抱怨某些列中存在 NULL 值:

pyspark.sql.utils.IllegalArgumentException: u"Can't get JDBC type for null"
Run Code Online (Sandbox Code Playgroud)

经过一番谷歌搜索和阅读后,我尝试通过将 AWS Glue 动态数据帧转换为 Spark 数据帧、执行函数fillna()并重新转换回动态数据帧来替换文件中的 NULL。

datasource0 = glueContext.create_dynamic_frame.from_catalog(database = 
"xyz_catalog", table_name = "xyz_staging_files", transformation_ctx = 
"datasource0")
custom_df = datasource0.toDF()
custom_df2 = custom_df.fillna(-1)
custom_df3 = custom_df2.fromDF()

applymapping1 = ApplyMapping.apply(frame = custom_df3, mappings = [("id", 
"string", "id", "int"),........more code
Run Code Online (Sandbox Code Playgroud)

参考:

https://github.com/awslabs/aws-glue-samples/blob/master/FAQ_and_How_to.md#3-there-are-some-transforms-that-i-cannot-figure-out

如何替换 Pyspark 中数据帧的所有 Null 值

http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.DataFrame.fillna

现在,当我运行我的作业时,它会抛出以下错误:

Log Contents:
Traceback (most recent call last):
File "script_2017-12-20-22-02-13.py", line 23, in <module>
custom_df3 = custom_df2.fromDF()
AttributeError: 'DataFrame' object has no attribute 'fromDF'
End of LogType:stdout
Run Code Online (Sandbox Code Playgroud)

我是 Python 和 Spark 的新手,并且尝试了很多,但无法理解这一点。感谢一些专家对此的帮助。

我尝试将我的重新转换命令更改为:

custom_df3 = glueContext.create_dynamic_frame.fromDF(frame = custom_df2)
Run Code Online (Sandbox Code Playgroud)

但仍然出现错误:

AttributeError: 'DynamicFrameReader' object has no attribute 'fromDF'
Run Code Online (Sandbox Code Playgroud)

更新:我怀疑这与 NULL 值无关。消息“Can't get JDBC type for null”似乎不是指 NULL 值,而是指 JDBC 无法破译的某些数据/类型。

我创建了一个只有 1 条记录、没有 NULL 值的文件,将所有布尔类型更改为 INT(并将值替换为 0 和 1),但仍然出现相同的错误:

pyspark.sql.utils.IllegalArgumentException: u"Can't get JDBC type for null"
Run Code Online (Sandbox Code Playgroud)

更新:确保导入 DynamicFrame(from awsglue.context import DynamicFrame),因为 fromDF / toDF 是 DynamicFrame 的一部分。

请参阅https://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-crawler-pyspark-extensions-dynamic-frame.html

dou*_*oug 2

您在错误的类上调用 .fromDF 。它应该看起来像这样:

from awsglue.dynamicframe import DynamicFrame
DyamicFrame.fromDF(custom_df2, glueContext, 'label')
Run Code Online (Sandbox Code Playgroud)