Created
June 2, 2025 15:42
-
-
Save divyansh8866/d4943d3e852da1cf07728c10a7a44747 to your computer and use it in GitHub Desktop.
This Python script uses Boto3, the AWS SDK for Python, to rename a table in the AWS Glue Data Catalog. Since Glue does not support direct table renaming, this script programmatically replicates the table and its partitions under a new name.
This file contains hidden or 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
import boto3 | |
import copy | |
glue = boto3.client('glue') | |
def rename_glue(db, old_name, new_name): | |
response = glue.get_table(DatabaseName=db, Name=old_name) | |
old_table = response['Table'] | |
# Deep copy storage descriptor to avoid reference issues | |
storage_descriptor = copy.deepcopy(old_table['StorageDescriptor']) | |
# Remove fields that are not allowed in new table creation | |
for field in ['Location', 'NumberOfBuckets', 'BucketColumns', 'SortColumns', 'SerdeInfo']: | |
storage_descriptor.pop(field, None) | |
# Prepare new table input | |
new_table_input = { | |
'Name': new_name, | |
'StorageDescriptor': old_table['StorageDescriptor'], | |
'PartitionKeys': old_table.get('PartitionKeys', []), | |
'TableType': old_table.get('TableType', 'EXTERNAL_TABLE'), | |
'Parameters': old_table.get('Parameters', {}), | |
} | |
# Optional: Copy owner if exists | |
if 'Owner' in old_table: | |
new_table_input['Owner'] = old_table['Owner'] | |
# Create new table in Glue catalog | |
print(f"Creating new table: {new_name}") | |
glue.create_table(DatabaseName=db, TableInput=new_table_input) | |
print("Table created successfully.") | |
# Paginate through all partitions and copy | |
print("Copying partitions...") | |
paginator = glue.get_paginator('get_partitions') | |
for page in paginator.paginate(DatabaseName=db, TableName=old_name): | |
for partition in page['Partitions']: | |
partition_input = { | |
'Values': partition['Values'], | |
'StorageDescriptor': partition['StorageDescriptor'], | |
'Parameters': partition.get('Parameters', {}) | |
} | |
glue.create_partition(DatabaseName=db, TableName=new_name, PartitionInput=partition_input) | |
print("All partitions copied successfully.") | |
db = "xxx" | |
old_table = "xxx" | |
new_table = "xxx" | |
rename_glue(db, old_table, new_table) | |
print("-"*50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment