如何使用Boto3从AWS Glue Data Catalog获取特定版本表定义的数据库?
问题陈述 − 使用Python中的boto3库检索数据库的表定义。
例子 − 获取数据库“QA-test”的表定义,并为版本2获取“security”的表。
更多Python相关文章,请阅读:Python 教程
解决这个问题的方法/算法
步骤1 − 导入boto3和botocore异常,以处理异常。
步骤2 − 数据库名称,表名称 和 版本ID 是必填参数。它为指定版本的给定表提取定义。
步骤3 − 使用boto3库创建一个AWS会话。确保在默认配置文件中提供了 region_name 。如果没有提供,则在创建会话时明确传递 region_name 。
步骤4 − 创建一个glue的AWS客户端。
步骤5 − 现在使用 get_table_version 函数,并将 数据库名称 作为DatabaseName, 表名称 作为TableName,以及 版本ID 作为VersionId参数。请注意,version_id是字符串,因此应将整数值作为字符串传递在引号中。
步骤6 − 它返回特定版本的给定表的定义。
步骤7 − 如果在检查作业时出现问题,请处理通用异常。
例子
使用以下代码检索指定版本的表定义 −
import boto3
from botocore.exceptions import ClientError
def retrieves_table_version_details(database_name, table_name, version_id)
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.get_table_version(DatabaseName = database_name, TableName = table_name, VersionId = version_id)
return response
except ClientError as e:
raise Exception("boto3 client error in retrieves_table_version_details: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in retrieves_table_version_details: " + e.__str__())
print(retrieves_table_version_details('QA-test', 'security', '2'))
输出
{'TableVersion': {'Table': {'Name': 'security', 'DatabaseName': 'QAtest', 'Owner': 'owner', 'CreateTime': datetime.datetime(2020, 9, 10,
22, 27, 24, tzinfo=tzlocal()), 'UpdateTime': datetime.datetime(2021, 3,
1, 11, 43, 49, tzinfo=tzlocal()), 'LastAccessTime':
datetime.datetime(2020, 9, 10, 22, 27, 24, tzinfo=tzlocal()),
'Retention': 0, 'StorageDescriptor': {'Columns': [{'Name':
'assettypecode', 'Type': 'string'}, {'Name': 'industrysector', 'Type':
'varchar'}, {'Name': 'securitycode', 'Type': 'char'}, {'Name':
'contractsize', 'Type': 'string'}, {'Name': 'conversionperiodenddate',
'Type': 'string'}, {'Name': 'conversionperiodstartdate', 'Type':
'string'}, {'Name': 'expirationdate', 'Type': 'string'}, {'Name':
'issuercountrycode', 'Type': 'string'}, {'Name': 'issuercountrydesc',
'Type': 'string'}, {'Name': 'originalissuedate', 'Type': 'string'},
{'Name': 'securitynamelong', 'Type': 'string'}, {'Name':
'issueshortname', 'Type': 'string'}, {'Name': 'gicssector', 'Type':
'string'}, {'Name': 'maturitydate', 'Type': 'string'}, {'Name':
'optioncode', 'Type': 'string'}, {'Name': 'optiontypename', 'Type':
'string'}, {'Name': 'paramount', 'Type': 'string'}, {'Name':
'priceindex', 'Type': 'string'}, {'Name': 'countrycoderisk', 'Type':
'string'}, {'Name': 'countrydescrisk', 'Type': 'string'}, {'Name':
'countrycode', 'Type': 'string'}], 'Location': 's3://test/security/',
'InputFormat':
'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat',
'OutputFormat':
'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat',
'Compressed': False, 'NumberOfBuckets': -1, 'SerdeInfo':
{'SerializationLibrary':
'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe',
'Parameters': {'serialization.format': '1'}}, 'BucketColumns': [],
'SortColumns': [], 'Parameters': {'CrawlerSchemaDeserializerVersion':
'1.0', 'CrawlerSchemaSerializerVersion': '1.0', 'UPDATED_BY_CRAWLER':
'security', 'averageRecordSize': '181', 'classification': 'parquet',
'compressionType': 'none', 'objectCount': '5', 'recordCount': '154800',
'sizeKey': '20337230', 'typeOfData': 'file'}, 'StoredAsSubDirectories':
False}, 'PartitionKeys': [], 'TableType': 'EXTERNAL_TABLE',
'Parameters': {'CrawlerSchemaDeserializerVersion': '1.0',
'CrawlerSchemaSerializerVersion': '1.0', 'UPDATED_BY_CRAWLER':
'security', 'averageRecordSize': '181', 'classification': 'parquet',
'compressionType': 'none', 'objectCount': '5', 'recordCount': '154800',
'sizeKey': '20337230', 'typeOfData': 'file'}, 'CreatedBy':
'arn:aws:sts::*********:assumed-role/glue-role/AWS-Crawler'},
'VersionId': '2'}, 'ResponseMetadata': {'RequestId': '431db171-
*******************0', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date':
'Mon, 01 Mar 2021 06:15:30 GMT', 'content-type': 'application/x-amzjson-1.1', 'content-length': '3916', 'connection': 'keep-alive', 'xamzn-requestid': '431db171-*****************0'}, 'RetryAttempts': 0}}
极客教程