toDF() 方法的文档指定我们可以将选项参数传递给该方法。但它没有指定这些选项是什么 (https://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-crawler-pyspark-extensions-dynamic-frame.html)。有谁知道是否有关于此的进一步文档? 我对从 DynamicFrame 创建 DataFrame 时传递架构特别感兴趣。
不幸的是,没有太多可用的文档,但对 dynamicframe 源代码的研发和分析表明以下内容:
中提供的
specs
,也提到了 ChoiceType。
specs
、toDF实现和来自spark的toDF后,我的理解是,我们在从DynamicFrame创建DataFrame时无法传递模式,但只能进行较小的列操作。 说到这里,一种可能的方法是从动态框架中获取数据帧,然后对其进行操作以更改其模式。
以下是在 python 中传递给动态帧 toDF() 的内容的示例:
from awsglue.dynamicframe import DynamicFrame
from awsglue.gluetypes import *
from collections import namedtuple
#any other imports you need..
# Define a named tuple called ResolveOption with attributes 'path', 'action', and 'target'
ResolveOption = namedtuple('ResolveOption', ['path', 'action', 'target'])
#Create an array of ResolveOption tuples
#(Good for when converting to a DataFrame and you need to project the data types for your schema so you don't end up with unresolved JSON values like {int:111, double:null} etc)
#action must be one of KeepAsStruct and Project
#target should be types such as (for example) StringType(), DoubleType(), etc..
ResolveOptions = [
ResolveOption(path="columnname", action="Project", target=StringType()),
....
]
#Assuming you created a dynamic frame named YourDynamicFrame earlier
YourDataFrame = YourDynamicFrame.toDF(ResolveOptions)
希望这有帮助