我是AWS Glue ETL的新手。我正在尝试执行简单的计算,并将派生的列添加到目标表列表中。当我查询时,我可以看到数据,但是我正在努力将其添加到最终数据集中。请尽快帮助我。谢谢
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)
## @type: DataSource
## @args: [database = "stg", table_name = "xyz", transformation_ctx = "datasource0"]
## @return: datasource0
## @inputs: []
datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "stg", table_name = "wind_gust", transformation_ctx = "datasource0")
## ==== Transformation ======
datasource0.toDF().createOrReplaceTempView("view_dyf")
sqlDF = spark.sql("select * from view_dyf").show()
## convert units from EU to US units
us_unit_conv =spark.sql("""SELECT IF (mesurement_type = 'm s-1', round(units * 1.151,2),
IF (mesurement_type = 'm', round(units / 1609.344,2),
IF (mesurement_type = 'Pa', round(units /6894.757,2),0) )
)as new_unit
from view_dyf""")
applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [("time", "string", "Time", "string"), ("latitude", "double", "Latitude", "double"), ("longitude", "double", "Longitude", "double"), ("units", "double", "EU_Units", "double"), ("mesurement_type", "string", "EU_Unit_Type", "string"), ("variable_name", "string", "Variable_Name", "string")], transformation_ctx = "applymapping1")
我将新的派生列添加为-(“ us_unit_conv”,“ double”,“ US_Units”,“ double”)。请参考以下
applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [("time", "string", "Time", "string"), ("latitude", "double", "Latitude", "double"), ("longitude", "double", "Longitude", "double"), ("units", "double", "EU_Units", "double"), ("mesurement_type", "string", "EU_Unit_Type", "string"), ("us_unit_conv", "double", "US_Units", "double"), ("variable_name", "string", "Variable_Name", "string")], transformation_ctx = "applymapping1")
答案 0 :(得分:0)
我认为您需要阅读更多有关Apply mapping:链接。
datasource0
,但是它应该是您的新框架us_unit_conv
。由于这是您创建的框架,其中包含新变量。("us_unit_conv", "double", "US_Units", "double")
,应为("input_name", "input_type", "output_name", "output_type")
。因此,在您的情况下,我猜应该是("new_unit", "double", "US_Units", "double")
。但是您还需要使用SELECT *
传递变量的其余部分。s_unit_conv =spark.sql("""SELECT *,IF (mesurement_type = 'm s-1', round(units * 1.151,2),
IF (mesurement_type = 'm', round(units / 1609.344,2),
IF (mesurement_type = 'Pa', round(units /6894.757,2),0) )
)as new_unit
from view_dyf""")
applymapping1 = ApplyMapping.apply(frame = s_unit_conv, mappings = [("new_unit", "double", "US_Units", "double"),("time", "string", "Time", "string"), ("latitude", "double", "Latitude", "double"), ("longitude", "double", "Longitude", "double"), ("units", "double", "EU_Units", "double"), ("mesurement_type", "string", "EU_Unit_Type", "string"), ("variable_name", "string", "Variable_Name", "string")], transformation_ctx = "applymapping1")