【问题标题】:How to change name of a table created by AWS Glue crawler using boto3如何使用 boto3 更改 AWS Glue 爬虫创建的表的名称
【发布时间】:2020-03-09 14:20:21
【问题描述】:

我正在尝试更改 AWS Crawler 使用 boto3 创建的表名。代码如下:

import boto3

database_name = "eventbus"
table_name = "enrollment_user_enroll_cancel_1_0_0"
new_table_name = "enrollment_user_enroll_cancel"

client = boto3.client("glue", region_name='us-west-1')
response = client.get_table(DatabaseName=database_name, Name=table_name)
table_input = response["Table"]
table_input["Name"] = new_table_name
print(table_input)
print(table_input["Name"])

table_input.pop("CreatedBy")
table_input.pop("CreateTime")
table_input.pop("UpdateTime")
client.create_table(DatabaseName=database_name, TableInput=table_input)

得到以下错误:

botocore.exceptions.ParamValidationError: Parameter validation failed:
Unknown parameter in TableInput: "DatabaseName", must be one of: Name, Description, Owner, LastAccessTime, LastAnalyzedTime, Retention, StorageDescriptor, PartitionKeys, ViewOriginalText, ViewExpandedText, TableType, Parameters
Unknown parameter in TableInput: "IsRegisteredWithLakeFormation", must be one of: Name, Description, Owner, LastAccessTime, LastAnalyzedTime, Retention, StorageDescriptor, PartitionKeys, ViewOriginalText, ViewExpandedText, TableType, Parameters

您能否告诉我此问题的解决方案?谢谢!

【问题讨论】:

  • 我能够解决这些参数问题并更改了爬虫名称。但是现有的爬虫有 AWS Crawler 附加的分区。新更改的爬虫名称未附加分区。如何在更改后的爬虫中附加分区?谢谢!

标签: amazon-web-services amazon-s3 aws-sdk boto3 aws-glue-data-catalog


【解决方案1】:

要摆脱client.create_table抛出的botocore.exceptions.ParamValidationError,您需要像处理CreatedBy等一样,从table_input中删除相应的项目

...

table_input.pop("DatabaseName")
table_input.pop("IsRegisteredWithLakeFormation")

client.create_table(DatabaseName=database_name, TableInput=table_input)

如果您的原始表有分区,想要添加到新表中,您需要使用类似的方法。首先,您需要使用以下任一方式检索有关这些分区的元信息:

注意:根据您选择的那个,您需要传递不同的参数。您可以在单个请求中检索多少个分区是有限制的。如果我没记错的话,大概是200左右。最重要的是,您可能需要使用page paginator 列出所有可用分区。当您的表有超过 400 个分区时就是这种情况。

一般来说,我会建议:

paginator = client.get_paginator('get_partitions')
response = paginator.paginate(
    DatabaseName=database_name,
    TableName=table_name
)

partitions = list()
for page in response:
    for p in page['Partitions']:
        partitions.append(p.copy())

# Here you need to remove "DatabaseName", "TableName", "CreationTime" from 
# every partition

现在您可以使用以下任一方法将检索到的分区添加到新表中:

我建议使用batch_create_partition(),但是它限制了单个请求可以创建多少个分区。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-21
    • 1970-01-01
    • 2018-05-01
    • 2018-05-14
    • 1970-01-01
    相关资源
    最近更新 更多