0
votes

I am new to AWS Glue ETL. I am trying to perform a simple calculation and add the derived column to target table list. When I query, i can see the data, but I am struggling to add it to my final data set. Please help me on the same at the earliest. Thanks

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")

I added new derived column as - ("us_unit_conv", "double", "US_Units", "double") . Please refer below

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")
1

1 Answers

0
votes

I think you need to read a bit more about apply mapping: link.

  1. You are specifying the wrong frame, you specify datasource0, but it should be your new frame us_unit_conv. Since this is the frame you create which have inlcudes your new variable.
  2. The mapping is also a bit wrong. ("us_unit_conv", "double", "US_Units", "double"), this should be ("input_name", "input_type", "output_name", "output_type"). So in your case I guess it would be ("new_unit", "double", "US_Units", "double"). But you also need to pass on the rest of the variable with a 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")