在 AWS Glue 中读取分区的 Avro 文件



>例如,我有一个存储桶,其中有很多数据在 Avro 中以"hive"样式分区

s3://my-bucket/year=2018/month=03/day=25/file-name.avro

我正在尝试在 Glue 中访问这些数据,如下所示:

val predicate = "year=2018 and month=03"
val opts = JsonOptions("""{ "paths": ["s3://my-bucket/"], "recurse": true }""")
val src = glueContext.getSource(connectionType = "s3"
, connectionOptions = opts
, pushDownPredicate = predicate
).withFormat("avro")

但是此表达式失败并出现异常:

com.amazonaws.services.glue.util.NonFatalException: User's pushdown predicate: year=2018 and month=03 can not be resolved against partition columns: []

我试过这样的事情:

val predicate = "year=2018 and month=3"
val opts = JsonOptions("""{ "paths": ["s3://my-bucket/"], "recurse": true }""")
val src = glueContext.getSourceWithFormat(connectionType = "s3", format="avro", options = opts, pushDownPredicate = predicate)

但它根本不接受下推谓词:

error: unknown parameter name: pushDownPredicate

我也尝试添加

"partitionKeys": ["year", "month", "day"]

JsonOptions,也不成功。

如何在没有爬网程序的情况下在 Glue 中读取配置单元分区的 Avro 序列化数据?

目前无法在getSource()getSourceWithFormat()中使用下推谓词,因为它在内部验证表达式中的字段是否实际上是分区。在getCatalogSource()中,它从Glue Catalog加载此信息并传递给验证器。对于getSource()getSourceWithFormat(),无法传递用于验证的数据分区的自定义列表,因此无法使用下推谓词。

作为一种解决方法,您可以生成包含数据分区的路径,并通过optionsgetSourceWithFormat()传递它。例如,如果要加载year=2018 and (month=03 or month=04)的数据,则代码应如下所示:

val paths = Array(
"s3://bucket/data/year=2018/month=03",
"s3://bucket/data/year=2018/month=04"
)
val source = glueContext.getSourceWithFormat(
connectionType = "s3",
format = "avro",
options = JsonOptions(Map(
"paths" -> paths,
"recurse": true
))).getDynamicFrame()

请注意,sourceDynamicFrame 不包含分区列yearmonth因此您可能需要手动添加它们。

最好的选择是运行爬虫my_bucket然后使用

glue_context.create_dynamic_frame.from_catalog( 数据库 = "my_S3_data_set", table_name = "catalog_data_table", push_down_predicate = 谓词(

最新更新