DynamoDB表:
+------------------+---------+---------------------+
| id |Column A | Column B|
+------------------+---------+---------------------+
|1 | "155.2"| 400 |
|2 | 100| 200 |
|3 | "455.2"| 305.5 |
|4 | "312.3"| 350 |
+------------------+---------+---------------------+
字符串
注意,在A列中,除了id = 2之外,我们将数字存储为字符串。
以下代码用于将表内容读入动态帧:
def create_dynamic_frame(table_name):
ddb_s3_bucket = <some-s3-bucket>
ddb_table_arn = <some-table-arn>
connection_options = {
"dynamodb.export": "ddb",
"dynamodb.unnestDDBJson": True,
"dynamodb.tableArn": ddb_table_arn,
"dynamodb.s3.bucket": ddb_s3_bucket,
"dynamodb.s3.prefix": 'temporary/ddbexport/'
}
dynamic_frame = glueContext.create_dynamic_frame.from_options(
connection_type="dynamodb",
connection_options=connection_options,
transformation_ctx="dynamic_frame",
)
return dynamic_frame
dyf = create_dynamic_frame('test-table')
型
在创建的动态帧上显示的输出:dyf.toDf().show()
+------------------+---------+---------------------+
| id |Column A | Column B|
+------------------+---------+---------------------+
|1 | null| 400 |
|2 | 100 | 200 |
|3 | null| 305.5 |
|4 | null| 350 |
+------------------+---------+---------------------+
型dyf.toDf().printSchema()
的输出:
root
|-- id: string (nullable = true)
|-- Column A: string (nullable = true)
|-- Column B: string (nullable = true)
型
注意,A列中的字符串值是null
。我的印象是,Glue将这两种类型都保留在列中,然后您可以使用resolveChoice
转换为您想要的任何类型。
有没有一种方法可以解析GST-DDB连接器中的类型?
我尝试使用resolveChoice
解析类型:
resolved_dyf = dyf.resolveChoice(specs = [("Column A", "cast:string")])
型
这不起作用,因为dyf
本身的值是null
2条答案
按热度按时间bprjcwpo1#
当使用“unnestDDBJson”时,它被迫解析和扁平化模式,我认为你需要在你的情况下避免这种情况,并在解析类型后自己做。
8qgya5xd2#
这似乎是“connectionType”:“dynamodb”的一个限制,其中AWS Glue DynamoDB导出连接器作为源
此外,如果我们使用
unnestDDBJson
参数,Glue被迫评估列的模式。如果我不使用unnestDDBJson
参数,所有列值都保持为struct
,我找不到资源来解析类型。一种解决方法是使用“connectionType”:“dynamodb”和ETL连接器作为源