我正在尝试将AWS GLUE数据目录整合到我正在构建的数据湖中。我正在使用几个不同的数据库,并希望为其中几个表中的列添加注释。这些数据库包括Redshift和MySql。我通常通过如下方式将注释添加到该列中
COMMENT ON COLUMN table.column_name IS 'This is the comment';
现在我知道Glue有一个在GUI中显示的注释字段。有没有办法将Glue中的评论字段与我添加到数据库列中的评论同步?
发布于 2019-12-04 17:32:42
为了更新已在AWS Glue Data Catalog中定义的表的一些元信息,您需要结合使用get_table()
和update_table()
方法,例如boto3
。
下面是最天真的方法:
import boto3
from pprint import pprint
glue_client = boto3.client('glue')
database_name = "__SOME_DATABASE__"
table_name = "__SOME_TABLE__"
response = glue_client.get_table(
DatabaseName=database_name,
Name=table_name
)
original_table = response['Table']
在这里,original_table
遵循由get_table()
定义的响应语法。但是,我们需要从它中删除一些字段,以便在使用update_table()
时它可以通过验证。可以通过将original_table
直接传递给update_table()
来获得允许的密钥列表,而不会有任何更改
allowed_keys = [
"Name",
"Description",
"Owner",
"LastAccessTime",
"LastAnalyzedTime",
"Retention",
"StorageDescriptor",
"PartitionKeys",
"ViewOriginalText",
"ViewExpandedText",
"TableType",
"Parameters"
]
updated_table = dict()
for key in allowed_keys:
if key in original_table:
updated_table[key] = original_table[key]
为简单起见,我们将更改表中第一列的注释
new_comment = "Foo Bar"
updated_table['StorageDescriptor']['Columns'][0]['Comment'] = new_comment
response = glue_client.update_table(
DatabaseName=database_name,
TableInput=updated_table
)
pprint(response)
显然,如果要向特定列添加注释,则需要将其扩展为
new_comment = "Targeted Foo Bar"
target_column_name = "__SOME_COLUMN_NAME__"
for col in updated_table['StorageDescriptor']['Columns']:
if col['Name'] == target_column_name:
col['Comment'] = new_comment
response = glue_client.update_table(
DatabaseName=database_name,
TableInput=updated_table
)
pprint(response)
https://stackoverflow.com/questions/59166027
复制