【问题标题】:how can aws glue job upload several tables in redshiftaws粘合作业如何在redshift中上传几个表
【发布时间】:2018-10-31 18:13:12
【问题描述】:

是否可以使用 AWS Glue 作业在 Redshift 中加载多个表?

这些是我遵循的步骤。

  1. 从 S3 爬取 json,数据已转换为数据目录表。
  2. 我创建了一个作业,它将在 redshift 中上传数据目录表,但它只限制我为每个作业上传一个表。在作业属性中(添加作业时),我选择的此作业运行选项是:AWS Glue 生成的建议脚本。

我不熟悉 python,而且我是 AWS 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: [TempDir, JOB_NAME]
args = getResolvedOptions(sys.argv, ['TempDir','JOB_NAME'])

sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)
job.init(args['JOB_NAME'], args)
## @type: DataSource
## @args: [database = "sampledb", table_name = "abs", transformation_ctx = "datasource0"]
## @return: datasource0
## @inputs: []
datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "sampledb", table_name = "abs", transformation_ctx = "datasource0")
## @type: ApplyMapping
## @args: [mapping = [("value", "int", "value", "int"), ("sex", "string", "sex", "string"), ("age", "string", "age", "string"), ("highest year of school completed", "string", "highest year of school completed", "string"), ("state", "string", "state", "string"), ("region type", "string", "region type", "string"), ("lga 2011", "string", "lga 2011", "string"), ("frequency", "string", "frequency", "string"), ("time", "string", "time", "string")], transformation_ctx = "applymapping1"]
## @return: applymapping1
## @inputs: [frame = datasource0]
applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [("value", "int", "value", "int"), ("sex", "string", "sex", "string"), ("age", "string", "age", "string"), ("highest year of school completed", "string", "highest year of school completed", "string"), ("state", "string", "state", "string"), ("region type", "string", "region type", "string"), ("lga 2011", "string", "lga 2011", "string"), ("frequency", "string", "frequency", "string"), ("time", "string", "time", "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 = "redshift", connection_options = {"dbtable": "abs", "database": "dbmla"}, redshift_tmp_dir = TempDir, transformation_ctx = "datasink4"]
## @return: datasink4
## @inputs: [frame = dropnullfields3]
datasink4 = glueContext.write_dynamic_frame.from_jdbc_conf(frame = dropnullfields3, catalog_connection = "redshift", connection_options = {"dbtable": "abs", "database": "dbmla"}, redshift_tmp_dir = args["TempDir"], transformation_ctx = "datasink4")
job.commit()

aws 胶水数据库:sampledb
aws 胶水中的表名:abs
红移数据库:dbmla

请提供有关如何上传它们的示例。谢谢!

【问题讨论】:

  • 您是否尝试编辑生成的代码,根据表的数量添加更多数据源?并尝试做一份工作?您可以在运行作业之前修改生成的 python 脚本。我还没有尝试过,但希望它有效。
  • 非常感谢尤瓦。你真的很有帮助。我也会尝试这种方式,并会让你知道输出。
  • 但是,我正在考虑不在脚本中输入特定的表名。我的计划也是调出每个数据目录表。爬虫将以每小时的速度运行,因此传入的表可能包含新的数据集或只是旧表的更新。进行此设置后,将很难在脚本中输入已用胶水抓取的每个表名,因为该作业将不包含新表。我在想是否有办法让作业加载一组表,只需重写现有的,然后上传新的数据目录表。
  • 我认为胶水生成的代码不可能开箱即用。您可能需要编写自己的自定义代码,使用 AWS Glue API 获取表列表,并循环遍历列表、执行转换等。我没有尝试过,但您可以尝试一下。请参阅此处了解更多信息:docs.aws.amazon.com/glue/latest/webapi/API_GetTables.html.
  • dbbest.com/blog/aws-glue-etl-service这里的解决方法和我上面说的一样,我刚看到,所以分享给大家参考

标签: amazon-redshift aws-glue


【解决方案1】:

根据 AWS Glue 常见问题,您可以修改生成的代码并运行作业。

问:如何自定义 AWS Glue 生成的 ETL 代码?

AWS Glue 的 ETL 脚本推荐系统生成 Scala 或 Python 代码。它利用 Glue 的自定义 ETL 库来简化对 数据源以及管理作业执行。你可以找到更多 我们的文档中有关该库的详细信息。您可以编写 ETL 代码 使用 AWS Glue 的自定义库或在 Scala 中编写任意代码或 Python 通过 AWS Glue 控制台脚本编辑器使用内联编辑, 下载自动生成的代码,并在您自己的 IDE 中进行编辑。 您也可以从我们的 Github 中托管的众多示例之一开始 存储库并自定义该代码。

所以请尝试将附加表的代码 sn-ps 添加到相同的脚本中,如下所示,

datasource1 = glueContext.create_dynamic_frame.from_catalog(database = "sampledb", table_name = "abs2", transformation_ctx = "datasource1")
applymapping2 = ApplyMapping.apply(.. transformation_ctx = "applymapping2")
resolvechoice2 = ResolveChoice.apply(frame = applymapping2, choice = "make_cols", transformation_ctx = "resolvechoice2")
dropnullfields3 = DropNullFields.apply(frame = resolvechoice2, transformation_ctx = "dropnullfields3")
datasink4 = glueContext.write_dynamic_frame.from_jdbc_conf(frame = dropnullfields3, catalog_connection = "redshift", connection_options = {"dbtable": "abs2", "database": "dbmla"}, redshift_tmp_dir = args["TempDir"], transformation_ctx = "datasink4")

datasource2 = glueContext.create_dynamic_frame.from_catalog(database = "sampledb", table_name = "abs2", transformation_ctx = "datasource1")
applymapping2 = ApplyMapping.apply(.. transformation_ctx = "applymapping2")
resolvechoice2 = ResolveChoice.apply(frame = applymapping2, choice = "make_cols", transformation_ctx = "resolvechoice2")
dropnullfields3 = DropNullFields.apply(frame = resolvechoice2, transformation_ctx = "dropnullfields3")
datasink4 = glueContext.write_dynamic_frame.from_jdbc_conf(frame = dropnullfields3, catalog_connection = "redshift", connection_options = {"dbtable": "abs2", "database": "dbmla"}, redshift_tmp_dir = args["TempDir"], transformation_ctx = "datasink4")

datasource3 = glueContext.create_dynamic_frame.from_catalog(database = "sampledb", table_name = "abs2", transformation_ctx = "datasource1")
applymapping2 = ApplyMapping.apply(.. transformation_ctx = "applymapping2")
resolvechoice2 = ResolveChoice.apply(frame = applymapping2, choice = "make_cols", transformation_ctx = "resolvechoice2")
dropnullfields3 = DropNullFields.apply(frame = resolvechoice2, transformation_ctx = "dropnullfields3")
datasink4 = glueContext.write_dynamic_frame.from_jdbc_conf(frame = dropnullfields3, catalog_connection = "redshift", connection_options = {"dbtable": "abs2", "database": "dbmla"}, redshift_tmp_dir = args["TempDir"], transformation_ctx = "datasink4")

job.commit()

相应地更改变量名称以使其唯一。谢谢

【讨论】:

  • 在这种情况下,作业书签是否适用于每个表?
  • 作业会并行运行吗?
  • 对我来说这是行不通的。我在 ApplyMapping.apply 行中遇到错误。问题是双点。不确定这对您有什么作用?
  • user2768132 请参阅 AWS Glue API 以正确使用 ApplyMapping.apply()。上面提供的代码 sn-p 只是一个模板,希望您在我的答案中看到最后一行以相应地更新变量名称。谢谢
  • 或者请查看原帖示例代码。谢谢
猜你喜欢
  • 2022-07-08
  • 1970-01-01
  • 2023-03-28
  • 2023-03-25
  • 2021-05-01
  • 1970-01-01
  • 2020-05-26
  • 1970-01-01
  • 2018-01-30
相关资源
最近更新 更多