4

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

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

pyspark.sql.utils.IllegalArgumentException: u"Can't get JDBC type for null"

在对 SO 进行一些谷歌搜索和阅读之后,我尝试通过将我的 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

参考:

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

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

我尝试将我的 reconvert 命令更改为:

custom_df3 = glueContext.create_dynamic_frame.fromDF(frame = custom_df2)

但仍然得到错误:

AttributeError: 'DynamicFrameReader' object has no attribute 'fromDF'

更新:我怀疑这与 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"

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

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

4

2 回答 2

1

对于此错误,pyspark.sql.utils.IllegalArgumentException: u"Can't get JDBC type for null" 您应该使用删除 Null 列。

在加载到 Redshift DB Tables 时,我遇到了类似的错误。使用以下命令后,问题得到解决

Loading= DropNullFields.apply(frame = resolvechoice3, transformation_ctx = "Loading")
于 2018-07-30T09:38:26.023 回答
1

您在错误的类上调用 .fromDF 。它应该如下所示:

from awsglue.dynamicframe import DynamicFrame
DyamicFrame.fromDF(custom_df2, glueContext, 'label')
于 2018-04-26T14:48:27.490 回答