【问题标题】:AWS Glue Limit data read from S3 Bucket从 S3 存储桶读取的 AWS Glue 限制数据
【发布时间】:2021-03-30 10:52:38
【问题描述】:

我有一个包含超过 600 万个文件的大存储桶。 我遇到了这个错误Failed to sanitize XML document destined for handler class,我认为这是问题所在:https://github.com/lbroudoux/es-amazon-s3-river/issues/16

有没有办法可以限制在第一次运行时读取的文件数量?

这就是我拥有的DataSource0 = glueContext.create_dynamic_frame.from_catalog(database = "s3-sat-dth-prd", table_name = "datahub_meraki_user_data", transformation_ctx = "DataSource0"),我可以告诉它只读取我存储桶中的一个文件夹吗?里面的每个文件夹都是这样调用的:partition=13/、partition=14/、partition=n/等等。

我该如何解决这个问题?

提前致谢。

【问题讨论】:

  • 是的,您可以指向 S3 前缀以仅从中读取文件。 dynamic_frame.load(context=context, bucket="s3://my_bucket/my_prefix", transformation_ctx="my_job" )
  • @PraneethPeiris 谢谢,但我需要更多信息 - 我是 Glue 新手,发现它非常笨重 - 我应该如何整合你写的内容?我应该导入比默认值更多的东西吗?
  • 我会将其添加为答案。让我知道这是否适合您。

标签: amazon-web-services amazon-s3 pyspark aws-glue


【解决方案1】:

有三种主要方法(据我所知)可以避免这种情况。

1.从前缀加载

要从 AWS Glue 中的特定路径加载文件,您可以使用以下语法。

from awsglue.dynamicframe import DynamicFrame

dynamic_frame = context.create_dynamic_frame_from_options(
        "s3",
        {
            'paths': ['s3://my_bucket_1/my_prefix_1'],
            'recurse': True,
            'groupFiles': 'inPartition',
            'groupSize': '1073741824'
        },
        format='json',
        transformation_ctx='DataSource0'
    )

您可以为paths 放置多个路径,Glue 将从所有这些路径中加载。

2。使用胶水书签。

当您在存储桶中有数百万个文件并且您只想加载新文件(在 Glue 作业运行之间)时,您可以启用 Glue 书签。它将跟踪它在内部索引中读取的文件(我们无权访问)。 您可以在定义作业时将其作为参数传递。


  MyJob:
    Type: AWS::Glue::Job
    Properties:
      ...
      GlueVersion: 2.0
      Command:
        Name: glueetl
        PythonVersion: 3
        ...
      DefaultArguments: {
        "--job-bookmark-option": job-bookmark-enable,
        ...

这将在您加载数据时启用使用 transformation_ctx 的名称定义的书签。是的,AWS 将相同的参数用于多种用途,这令人困惑!

同样重要的是,您一定不要忘记在 Glue 脚本的末尾添加 job.commit(),其中 job 是您的 from awsglue.job import Job 实例。

然后,当您使用具有根前缀和相同transformation_ctx 的相同context.create_dynamic_frame_from_options() 函数时,它只会加载层次结构中前缀中的新文件。它为我们节省了很多寻找新文件的麻烦。阅读docs 了解有关书签的更多信息。

3.避免使用较小的文件。

如果您的文件非常小,AWS Glue 将需要很长时间才能加载文件。因此,如果您可以控制文件大小,则使文件大小至少为 100MB。例如,我们正在从 Firehose 流写入 S3,我们可以调整缓冲区大小以避免较小的文件大小。这大大增加了 Glue 作业的加载时间。

希望这些提示对您有所帮助。如果您需要进一步说明,请随时提出任何问题。

【讨论】:

    【解决方案2】:

    有一种方法可以控制文件的数量,称为 BoundedExecution。它记录在这里:https://docs.aws.amazon.com/glue/latest/dg/bounded-execution.html

    在以下示例中,您将一次加载 200 个文件。请注意,您必须启用 Glue 书签才能正常工作。

    如果你使用 from_options,它看起来像这样:

        DataSource0 = glueContext.create_dynamic_frame.from_options(
            format_options={"withHeader": True, "separator": separator, "quoteChar": quoteChar},
            connection_type="s3",
            format="csv",
            connection_options={"paths": inputFilePath,
                                "boundedFiles": "200", "recurse": True},
            transformation_ctx="DataSource0"
        )
    

    如果您使用 from_catalog,它看起来像这样:

        DataSource0 = glueContext.create_dynamic_frame.from_catalog(
            database = "database-name",
            table_name= "table-name",
            additional_options={"boundedFiles": "200"},
            transformation_ctx="DataSource0"
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 2021-03-12
      • 1970-01-01
      相关资源
      最近更新 更多