我找到了一份需要执行以下任务的工作
这是代码
import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job
## @params: [JOB_NAME]
args = getResolvedOptions(sys.argv, ['JOB_NAME'])
sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)
datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "gluecatalog", table_name = "fcorders", transformation_ctx = "datasource0")
rootTableName = 'orders'
dfc = Relationalize.apply(frame = datasource0, staging_path = "s3://my-bucket/temp/", name = rootTableName, transformation_ctx = "dfc")
dfc.keys()
for df_name in dfc.keys():
m_df = dfc.select(df_name)
print "Writing to Postgre table: ", df_name
if (df_name <> rootTableName):
renamefields4 = m_df.rename_field("SalesDeliveryLines.val.shipped.unitDisplayCode", "shipped_unitDisplayCode")
else:
renamefields4 = RenameField.apply(frame = m_df, old_name = "vehicle.sourceReccordUID", new_name = "vehicle_sourceReccordUID", transformation_ctx = "renamefields4")
renamefields4.printSchema()
printSchema()将架构显示为未更改。如果我写入数据库,字段名称仍然包含&#39;。&#39; s。
如果我在关系化之前使用ApplyMapping.apply()更改字段名称,则会使子表消失。如果我在关系化后使用ApplyMapping.apply(),它只删除名称中包含&#39;。&#39;的所有字段。
最重要的是,无论我尝试什么,我都无法在同一个工作中关联和重命名字段。
我是否遗漏了某些内容,或者这是一个AWS Glue错误?
答案 0 :(得分:2)
确认rename_field()
和RenameField.apply()
的故障是胶水错误。
我到目前为止的解决方法是将DynamicFrame转换为DataFrame - &gt;重命名字段DataFrame - &gt;将其转换回DynamicFrame。
这是代码
new_df = m_df.toDF()
print (type( new_df))
for oldName in new_df.schema.names:
new_df = new_df.withColumnRenamed(oldName, oldName.replace("SalesDeliveryLines.val.","").replace(".","_"))
m_df = m_df.fromDF(new_df, glueContext, "m_df")
答案 1 :(得分:1)
您需要在字段名称周围放置反斜杠:
m_df.rename_field("`SalesDeliveryLines.val.shipped.unitDisplayCode`", "shipped_unitDisplayCode")
上找到更多信息