AWS Glue:如何将 S3 存储桶分区为多个红移表

AWS Glue: How to partition S3 Bucket into multiple redshift tables

我设置了一个基本的 AWS Glue 作业,它从具有多个文件夹的 S3 存储桶中读取数据:

S3://mybucket/table1
S3://mybucket/table2
S3://mybucket/table3

等等。这些文件夹中的所有文件都具有完全相同的格式,我希望将它们插入到同一数据库(table1、table2、table3)中的不同 redshift 表中。似乎有一种方法可以自动从 S3 存储桶到 S3 存储桶执行此操作,但我似乎找不到有关如何从 S3 到 Redshift 执行此操作的文档,这完全可能吗?

我目前的代码只是为此作业生成的基本 Glue 模板代码,partition_0 包含文件夹名称的字符串表示形式:

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

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)

datasource0 = glueContext.create_dynamic_frame.from_catalog(database = "test", table_name = "all_data_bucket", transformation_ctx = "datasource0")
applymapping1 = ApplyMapping.apply(frame = datasource0, mappings = [("dataField1", "string", "dataField1", "string"), ("partition_0", "string", "partition_0", "string")], transformation_ctx = "applymapping1")

resolvechoice2 = ResolveChoice.apply(frame = applymapping1, 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": "all_data_table", "database": "dev"}, redshift_tmp_dir = args["TempDir"], transformation_ctx = "datasink4")
job.commit()

1) 将数据爬取为三个单独的表 2) 使用 boto3 列出该数据库中的表 3) 遍历列表并应用胶水代码将数据加载到 redshift