使用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了,当公司有命名规范的时候就只能通过这种方式曲线救国。