AWS Glue - 如何使用 BOTO3 更改 Glue Catalog table 中的列名?

AWS Glue - how to change column names in Glue Catalog table using BOTO3?

我正在使用 AWS Glue 爬虫从 S3 zip 文件中读取(没有 header)并填充 Glue 目录。

列默认命名为:col_0col_1...

如何使用例如更改这些列名称python boto3 模块并直接与 AWS Glue 目录交互?

是否有执行此操作的示例代码段?

谢谢。

您可以尝试提取 table 并更新名称。这是我会做的一个例子。

首先我们将尝试检索 table:

    database_name = 'ENTER TABLE NAME'
    table_name = 'ENTER TABLE NAME'
    response = self.glue_client.get_table(DatabaseName=database_name,table_name=Name)
    old_table = response['Table']

接下来我们将使用我们想要更改的值更新 table。我们创建的新 table 只能有某些字段,以便 update_table 接受它。因此,我们将执行以下操作。

    field_names = [
      "Name",
      "Description",
      "Owner",
      "LastAccessTime",
      "LastAnalyzedTime",
      "Retention",
      "StorageDescriptor",
      "PartitionKeys",
      "ViewOriginalText",
      "ViewExpandedText",
      "TableType",
      "Parameters"
    ]
    new_table = dict()
    for key in field_names:
     if key in old_table:
      new_table[key] = old_table[key]

现在我们有了更新的 table,我们可以操作列名了。这是将 'col_0' 更改为 'new_col'

的示例
    for col in new_table['StorageDescriptor']['Columns']:
      if col['Name'] == 'col_0':
        col['Name'] = 'new_col' 
    response=self.glue_client.update_table(DatabaseName=database_name,TableInput=new_table)

希望这对您有所帮助!