Created
March 2, 2021 18:49
-
-
Save ramondeklein/fb016893c500685c976dd1e89a0b5873 to your computer and use it in GitHub Desktop.
Rename table in AWS glue (including partition scheme)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This GIST can be used to rename a table in AWS Glue and keep the | |
# partitioning scheme intact. | |
import boto3 | |
region = "eu-west-1" | |
database_name = "rdk-test" | |
table_name = "yt_uncompressed" | |
new_table_name = "nyc_taxiset" | |
client = boto3.client("glue", region_name=region) | |
response = client.get_table(DatabaseName=database_name, Name=table_name) | |
table_input = response["Table"] | |
table_input["Name"] = new_table_name | |
table_input.pop("CreatedBy") | |
table_input.pop("CreateTime") | |
table_input.pop("UpdateTime") | |
table_input.pop("DatabaseName") | |
table_input.pop("IsRegisteredWithLakeFormation") | |
table_input.pop("CatalogId") | |
client.create_table(DatabaseName=database_name, TableInput=table_input) | |
response = client.get_table(DatabaseName=database_name, Name=new_table_name) | |
table_input = response["Table"] | |
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']: | |
copy = p.copy() | |
copy.pop("DatabaseName") | |
copy.pop("TableName") | |
copy.pop("CreationTime") | |
copy.pop("CatalogId") | |
partitions.append(copy) | |
client.batch_create_partition( | |
CatalogId=table_input["CatalogId"], | |
DatabaseName=database_name, | |
TableName=new_table_name, | |
PartitionInputList=partitions | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a version to be used as a glue job which is more late-2024 friendly that could be useful for people: