AWS Glue 从两个现有列创建新列,第一个值不为空
AWS Glue create new column from two existing columns with first value that is not null
我收到了 AWS Glue 作业请求。我们在每条记录中有两列,goal_name
和 description
。该请求是创建一个新列,如果该记录具有 description
或 goal_name
如果该记录没有 description
,则该列将由 description
填充。
我是 AWS Glue 的新手,所以我不知道如何完成这样的事情。我目前在 Glue 脚本中有以下内容:
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 = "pixel_data", table_name = "pixel_data_nicheresearch_internal", transformation_ctx = "datasource0"]
## @return: datasource0
## @inputs: []
datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "pixel_data", table_name = "pixel_data_nicheresearch_internal", transformation_ctx = "datasource0")
## @type: ApplyMapping
## @args: [mapping = [("conv_dttm", "string", "conv_dttm", "string"), ("imp_click_dttm", "string", "imp_click_dttm", "string"), ("cid", "string", "cid", "string"), ("imp_click_campaign_id", "long", "imp_click_campaign_id", "long"), ("campaign_name", "string", "campaign_name", "string"), ("imp_click_partner_id", "long", "imp_click_partner_id", "long"), ("partner_name", "string", "partner_name", "string"), ("imp_click_placement_id", "long", "imp_click_placement_id", "long"), ("placement_name", "string", "placement_name", "string"), ("custom_reference_id", "string", "custom_reference_id", "string"), ("imp_click_creative_id", "string", "imp_click_creative_id", "string"), ("goal_name", "string", "goal_name", "string"), ("description", "string", "description", "string"), ("conversion_type", "string", "conversion_type", "string"), ("conv_ip_address", "string", "conv_ip_address", "string"), ("conv_user_agent", "string", "conv_user_agent", "string"), ("custom_var_1", "string", "custom_var_1", "string"), ("custom_var_2", "string", "custom_var_2", "string"), ("custom_var_3", "string", "custom_var_3", "string"), ("custom_var_4", "string", "custom_var_4", "string"), ("conv_revenue", "double", "conv_revenue", "double"), ("idfa", "string", "idfa", "string"), ("sec_diff_utc", "long", "sec_diff_utc", "long"), ("deterministic_match", "long", "deterministic_match", "long"), ("high_probabilistic_match", "long", "high_probabilistic_match", "long"), ("probabilitstic_match", "long", "probabilitstic_match", "long"), ("ip_match", "long", "ip_match", "long"), ("conv_device_type", "string", "conv_device_type", "string"), ("placement_platform", "string", "placement_platform", "string"), ("hhid_match", "long", "hhid_match", "long"), ("click_custom_var_1", "string", "click_custom_var_1", "string"), ("click_custom_var_2", "string", "click_custom_var_2", "string"), ("click_custom_var_3", "string", "click_custom_var_3", "string"), ("click_custom_var_4", "string", "click_custom_var_4", "string"), ("imp_click_custom_var_1", "string", "imp_click_custom_var_1", "string"), ("imp_click_custom_var_2", "string", "imp_click_custom_var_2", "string"), ("imp_click_custom_var_3", "string", "imp_click_custom_var_3", "string"), ("imp_click_custom_var_4", "string", "imp_click_custom_var_4", "string"), ("partition_0", "string", "partition_0", "string")], transformation_ctx = "applymapping1"]
## @return: applymapping1
## @inputs: [frame = datasource0]
applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [("conv_dttm", "string", "conv_dttm", "string"), ("imp_click_dttm", "string", "imp_click_dttm", "string"), ("cid", "string", "cid", "string"), ("imp_click_campaign_id", "long", "imp_click_campaign_id", "long"), ("campaign_name", "string", "campaign_name", "string"), ("imp_click_partner_id", "long", "imp_click_partner_id", "long"), ("partner_name", "string", "partner_name", "string"), ("imp_click_placement_id", "long", "imp_click_placement_id", "long"), ("placement_name", "string", "placement_name", "string"), ("custom_reference_id", "string", "custom_reference_id", "string"), ("imp_click_creative_id", "string", "imp_click_creative_id", "string"), ("goal_name", "string", "goal_name", "string"), ("description", "string", "description", "string"), ("conversion_type", "string", "conversion_type", "string"), ("conv_ip_address", "string", "conv_ip_address", "string"), ("conv_user_agent", "string", "conv_user_agent", "string"), ("custom_var_1", "string", "custom_var_1", "string"), ("custom_var_2", "string", "custom_var_2", "string"), ("custom_var_3", "string", "custom_var_3", "string"), ("custom_var_4", "string", "custom_var_4", "string"), ("conv_revenue", "double", "conv_revenue", "double"), ("idfa", "string", "idfa", "string"), ("sec_diff_utc", "long", "sec_diff_utc", "long"), ("deterministic_match", "long", "deterministic_match", "long"), ("high_probabilistic_match", "long", "high_probabilistic_match", "long"), ("probabilitstic_match", "long", "probabilitstic_match", "long"), ("ip_match", "long", "ip_match", "long"), ("conv_device_type", "string", "conv_device_type", "string"), ("placement_platform", "string", "placement_platform", "string"), ("hhid_match", "long", "hhid_match", "long"), ("click_custom_var_1", "string", "click_custom_var_1", "string"), ("click_custom_var_2", "string", "click_custom_var_2", "string"), ("click_custom_var_3", "string", "click_custom_var_3", "string"), ("click_custom_var_4", "string", "click_custom_var_4", "string"), ("imp_click_custom_var_1", "string", "imp_click_custom_var_1", "string"), ("imp_click_custom_var_2", "string", "imp_click_custom_var_2", "string"), ("imp_click_custom_var_3", "string", "imp_click_custom_var_3", "string"), ("imp_click_custom_var_4", "string", "imp_click_custom_var_4", "string"), ("partition_0", "string", "partition_0", "string")], transformation_ctx = "applymapping1")
## @type: ResolveChoice
## @args: [choice = "make_cols", transformation_ctx = "resolvechoice2"]
## @return: resolvechoice2
## @inputs: [frame = applymapping1]
resolvechoice2 = ResolveChoice.apply(frame = applymapping1, choice = "make_cols", transformation_ctx = "resolvechoice2")
## @type: DropNullFields
## @args: [transformation_ctx = "dropnullfields3"]
## @return: dropnullfields3
## @inputs: [frame = resolvechoice2]
dropnullfields3 = DropNullFields.apply(frame = resolvechoice2, transformation_ctx = "dropnullfields3")
## @type: DataSink
## @args: [catalog_connection = "product-analytics-test", connection_options = {"dbtable": "conversions", "database": "pixel_data"}, transformation_ctx = "datasink4"]
## @return: datasink4
## @inputs: [frame = dropnullfields3]
datasink4 = glueContext.write_dynamic_frame.from_jdbc_conf(frame = dropnullfields3, catalog_connection = "product-analytics-test", connection_options = {"dbtable": "conversions", "database": "pixel_data"}, transformation_ctx = "datasink4")
job.commit()
您必须将 Glue DynamicFrame 转换为原生 Spark DataFrame。然后你可以使用来自 Spark 的 coalesce function。
import pyspark.sql.functions as F
from awsglue import DynamicFrame
new_data_frame = DynamicFrame.fromDF(dropnullfields3.toDF().withColumn('new_column_name', F.coalesce(F.col('description'), F.col('goal_name'))), glueContext, 'df')
我收到了 AWS Glue 作业请求。我们在每条记录中有两列,goal_name
和 description
。该请求是创建一个新列,如果该记录具有 description
或 goal_name
如果该记录没有 description
,则该列将由 description
填充。
我是 AWS Glue 的新手,所以我不知道如何完成这样的事情。我目前在 Glue 脚本中有以下内容:
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 = "pixel_data", table_name = "pixel_data_nicheresearch_internal", transformation_ctx = "datasource0"]
## @return: datasource0
## @inputs: []
datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "pixel_data", table_name = "pixel_data_nicheresearch_internal", transformation_ctx = "datasource0")
## @type: ApplyMapping
## @args: [mapping = [("conv_dttm", "string", "conv_dttm", "string"), ("imp_click_dttm", "string", "imp_click_dttm", "string"), ("cid", "string", "cid", "string"), ("imp_click_campaign_id", "long", "imp_click_campaign_id", "long"), ("campaign_name", "string", "campaign_name", "string"), ("imp_click_partner_id", "long", "imp_click_partner_id", "long"), ("partner_name", "string", "partner_name", "string"), ("imp_click_placement_id", "long", "imp_click_placement_id", "long"), ("placement_name", "string", "placement_name", "string"), ("custom_reference_id", "string", "custom_reference_id", "string"), ("imp_click_creative_id", "string", "imp_click_creative_id", "string"), ("goal_name", "string", "goal_name", "string"), ("description", "string", "description", "string"), ("conversion_type", "string", "conversion_type", "string"), ("conv_ip_address", "string", "conv_ip_address", "string"), ("conv_user_agent", "string", "conv_user_agent", "string"), ("custom_var_1", "string", "custom_var_1", "string"), ("custom_var_2", "string", "custom_var_2", "string"), ("custom_var_3", "string", "custom_var_3", "string"), ("custom_var_4", "string", "custom_var_4", "string"), ("conv_revenue", "double", "conv_revenue", "double"), ("idfa", "string", "idfa", "string"), ("sec_diff_utc", "long", "sec_diff_utc", "long"), ("deterministic_match", "long", "deterministic_match", "long"), ("high_probabilistic_match", "long", "high_probabilistic_match", "long"), ("probabilitstic_match", "long", "probabilitstic_match", "long"), ("ip_match", "long", "ip_match", "long"), ("conv_device_type", "string", "conv_device_type", "string"), ("placement_platform", "string", "placement_platform", "string"), ("hhid_match", "long", "hhid_match", "long"), ("click_custom_var_1", "string", "click_custom_var_1", "string"), ("click_custom_var_2", "string", "click_custom_var_2", "string"), ("click_custom_var_3", "string", "click_custom_var_3", "string"), ("click_custom_var_4", "string", "click_custom_var_4", "string"), ("imp_click_custom_var_1", "string", "imp_click_custom_var_1", "string"), ("imp_click_custom_var_2", "string", "imp_click_custom_var_2", "string"), ("imp_click_custom_var_3", "string", "imp_click_custom_var_3", "string"), ("imp_click_custom_var_4", "string", "imp_click_custom_var_4", "string"), ("partition_0", "string", "partition_0", "string")], transformation_ctx = "applymapping1"]
## @return: applymapping1
## @inputs: [frame = datasource0]
applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [("conv_dttm", "string", "conv_dttm", "string"), ("imp_click_dttm", "string", "imp_click_dttm", "string"), ("cid", "string", "cid", "string"), ("imp_click_campaign_id", "long", "imp_click_campaign_id", "long"), ("campaign_name", "string", "campaign_name", "string"), ("imp_click_partner_id", "long", "imp_click_partner_id", "long"), ("partner_name", "string", "partner_name", "string"), ("imp_click_placement_id", "long", "imp_click_placement_id", "long"), ("placement_name", "string", "placement_name", "string"), ("custom_reference_id", "string", "custom_reference_id", "string"), ("imp_click_creative_id", "string", "imp_click_creative_id", "string"), ("goal_name", "string", "goal_name", "string"), ("description", "string", "description", "string"), ("conversion_type", "string", "conversion_type", "string"), ("conv_ip_address", "string", "conv_ip_address", "string"), ("conv_user_agent", "string", "conv_user_agent", "string"), ("custom_var_1", "string", "custom_var_1", "string"), ("custom_var_2", "string", "custom_var_2", "string"), ("custom_var_3", "string", "custom_var_3", "string"), ("custom_var_4", "string", "custom_var_4", "string"), ("conv_revenue", "double", "conv_revenue", "double"), ("idfa", "string", "idfa", "string"), ("sec_diff_utc", "long", "sec_diff_utc", "long"), ("deterministic_match", "long", "deterministic_match", "long"), ("high_probabilistic_match", "long", "high_probabilistic_match", "long"), ("probabilitstic_match", "long", "probabilitstic_match", "long"), ("ip_match", "long", "ip_match", "long"), ("conv_device_type", "string", "conv_device_type", "string"), ("placement_platform", "string", "placement_platform", "string"), ("hhid_match", "long", "hhid_match", "long"), ("click_custom_var_1", "string", "click_custom_var_1", "string"), ("click_custom_var_2", "string", "click_custom_var_2", "string"), ("click_custom_var_3", "string", "click_custom_var_3", "string"), ("click_custom_var_4", "string", "click_custom_var_4", "string"), ("imp_click_custom_var_1", "string", "imp_click_custom_var_1", "string"), ("imp_click_custom_var_2", "string", "imp_click_custom_var_2", "string"), ("imp_click_custom_var_3", "string", "imp_click_custom_var_3", "string"), ("imp_click_custom_var_4", "string", "imp_click_custom_var_4", "string"), ("partition_0", "string", "partition_0", "string")], transformation_ctx = "applymapping1")
## @type: ResolveChoice
## @args: [choice = "make_cols", transformation_ctx = "resolvechoice2"]
## @return: resolvechoice2
## @inputs: [frame = applymapping1]
resolvechoice2 = ResolveChoice.apply(frame = applymapping1, choice = "make_cols", transformation_ctx = "resolvechoice2")
## @type: DropNullFields
## @args: [transformation_ctx = "dropnullfields3"]
## @return: dropnullfields3
## @inputs: [frame = resolvechoice2]
dropnullfields3 = DropNullFields.apply(frame = resolvechoice2, transformation_ctx = "dropnullfields3")
## @type: DataSink
## @args: [catalog_connection = "product-analytics-test", connection_options = {"dbtable": "conversions", "database": "pixel_data"}, transformation_ctx = "datasink4"]
## @return: datasink4
## @inputs: [frame = dropnullfields3]
datasink4 = glueContext.write_dynamic_frame.from_jdbc_conf(frame = dropnullfields3, catalog_connection = "product-analytics-test", connection_options = {"dbtable": "conversions", "database": "pixel_data"}, transformation_ctx = "datasink4")
job.commit()
您必须将 Glue DynamicFrame 转换为原生 Spark DataFrame。然后你可以使用来自 Spark 的 coalesce function。
import pyspark.sql.functions as F
from awsglue import DynamicFrame
new_data_frame = DynamicFrame.fromDF(dropnullfields3.toDF().withColumn('new_column_name', F.coalesce(F.col('description'), F.col('goal_name'))), glueContext, 'df')