如何使用 Boto3 從 AWS Glue 資料目錄獲取資料庫的表定義?
問題陳述 − 使用 Python 中的 boto3 庫檢索資料庫的表定義。
示例 − 檢索名為“QA-test”的資料庫和名為“security”的表的表定義。
解決此問題的方法/演算法
步驟 1 − 匯入 boto3 和 botocore 異常以處理異常。
步驟 2 − database_name 和 table_name 是必需引數。它獲取給定表的定義。
步驟 3 − 使用 boto3 庫建立 AWS 會話。確保在預設配置檔案中提到了region_name。如果未提及,則在建立會話時顯式傳遞region_name。
步驟 4 − 為 glue 建立 AWS 客戶端。
步驟 5 − 現在使用 get_table 函式並將database_name作為 DatabaseName 引數和table_name作為 Name 引數傳遞。
步驟 6 − 它返回給定表的定義。如果表具有多個版本,它始終從表的當前/最新版本獲取詳細資訊。
步驟 7 − 如果在檢查作業時出現錯誤,請處理通用異常。
示例
使用以下程式碼檢索資料庫的表定義:
import boto3
from botocore.exceptions import ClientError
def retrieves_table_details(database_name, table_name)
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.get_table(DatabaseName = database_name, Name = table_name)
return response
except ClientError as e:
raise Exception("boto3 client error in retrieves_table_details: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in retrieves_table_details: " + e.__str__())
print(retrieves_table_details('QA-test', 'security'))輸出
{'Table': {'Name': 'security', 'DatabaseName': 'QA-test', 'Owner':
'owner', 'CreateTime': datetime.datetime(2020, 9, 10, 22, 27, 24,
tzinfo=tzlocal()), 'UpdateTime': datetime.datetime(2021, 2, 28, 10, 37,
33, tzinfo=tzlocal()), 'LastAccessTime': datetime.datetime(2020, 9, 10,
22, 27, 24, tzinfo=tzlocal()), 'Retention': 0, 'StorageDescriptor':
{'Columns': [{'Name': 'assettypecode', 'Type': 'string'}, {'Name':
'industrysector', 'Type': 'string'}, {'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'},
'ResponseMetadata': {'RequestId': '4c108dd5-***************76ac',
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Mon, 01 Mar 2021
06:04:58 GMT', 'content-type': 'application/x-amz-json-1.1', 'contentlength': '3882', 'connection': 'keep-alive', 'x-amzn-requestid':
'4c108dd5-*********************676ac'}, 'RetryAttempts': 0}}
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP