使用AWS Crawlers建表時,只有一個table_prefix選項可以選擇所建表的表名,無法自定義表名。因此通過ETL-workflow和ETL-Python-Shell來完成自定義表名的工作。
1.創建一個temp_db用來臨時存儲所創建的表
2 創建一個Crawler,將表生成在1的database中
3.創建一個Python-Shell Job,用來複製表到目標數據庫中,並改成自己想要的名字
import boto3
source_db='temp_db'#創建的temp_db
target_db = "target_db"#目標數據庫
table_name="初始表名"
new_table_name = "table_name"#想命名的表名
client = boto3.client("glue")
response = client.get_table(DatabaseName=source_db, Name=table_name)
table_input = response["Table"]
table_input["Name"] = new_table_name
# Delete keys that cause create_table to fail
table_input.pop("CreatedBy")
table_input.pop("CreateTime")
table_input.pop("UpdateTime")
table_input.pop("DatabaseName")
client.create_table(DatabaseName=target_db, TableInput=table_input)
partition_input = client.get_partitions(DatabaseName=source_db, TableName=table_name)["Partitions"]
for p in partition_input:
# Delete keys that cause create_partition to fail
p.pop("TableName")
p.pop("CreationTime")
p.pop("DatabaseName")
client.batch_create_partition(DatabaseName=target_db, TableName=new_table_name, PartitionInputList=partition_input)
4.在aws ETL中創建一個workflow,包括on-demond-trigger(用來trigger crawler),crawler,trigger(在爬蟲successed之後trigger python job),python-job
通過以上四個步驟建成自己想要的表名的表。AWS的Crawler不支持自定義表名太low了,當公司有命名規範的時候就只能通過這種方式曲線救國。