pyspark - AWS Glue PySpark 替换 NULL

标签 pyspark aws-glue

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

最初,它提示某些列中的 NULL 值:

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

在对 SO 进行一些谷歌搜索和阅读之后,我尝试通过将我的 AWS Glue 动态数据帧转换为 Spark 数据帧来替换文件中的 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

How to replace all Null values of a dataframe in Pyspark

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 的新手,并尝试了很多,但无法理解这一点。感谢一些专家的帮助。

我尝试将我的重新转换命令更改为:
custom_df3 = glueContext.create_dynamic_frame.fromDF(frame = custom_df2)

但是还是报错:
AttributeError: 'DynamicFrameReader' object has no attribute 'fromDF'

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

我创建了一个只有 1 个记录,没有 NULL 值的文件,将所有 bool 类型更改为 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

最佳答案

你在错误的类上调用 .fromDF 。它应该是这样的:

from awsglue.dynamicframe import DynamicFrame
DyamicFrame.fromDF(custom_df2, glueContext, 'label')

关于pyspark - AWS Glue PySpark 替换 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47915779/

相关文章:

amazon-s3 - 如何使用 AWS Glue 将许多 CSV 文件转换为 Parquet

python-3.x - 如何通过boto库为EMR集群选项配置 "Use AWS Glue Data Catalog for table metadata"?

amazon-s3 - awsgluecloudformation Parquet 作业

amazon-dynamodb - 如何使用 AWS Glue ETL 加载 dynamodb 表

python - 如何在 PySpark 中读取从 Spark 编写的 Parquet ?

apache-spark - 如何识别 Spark Dataframe 中的离散状态(振荡)?

apache-spark - pyspark 中 parquet writer 保存的文件数量

dataframe - PySpark 数据帧聚合中包含 null 的计数

arrays - 使用数组值合并两个 Spark 数据帧

amazon-web-services - 胶水作业无法写入文件