ข้อกำหนดปัญหา
ใช้ไลบรารี Boto3 ใน Python เพื่อดึงรายละเอียดของ Classifier จาก AWS Glue Data Catalog เช่น ดึงรายละเอียด Classifier ชื่อ xml-test
ขั้นตอนการแก้ปัญหา
- ขั้นตอนที่ 1 – นำเข้า
boto3และbotocore.exceptionsเพื่อจัดการข้อยกเว้น - ขั้นตอนที่ 2 – ส่งพารามิเตอร์ classifier_name ที่ต้องการตรวจสอบ
- ขั้นตอนที่ 3 – สร้าง AWS Session ด้วย Boto3 ตรวจสอบ
region_nameในโปรไฟล์เริ่มต้น หากไม่มี ให้ระบุโดยตรง - ขั้นตอนที่ 4 – สร้าง AWS Client สำหรับ Glue
- ขั้นตอนที่ 5 – เรียก get_classifier และส่ง
classifier_nameเป็นพารามิเตอร์ Name - ขั้นตอนที่ 6 – จะดึงรายละเอียดของ Classifier
- ขั้นตอนที่ 7 – จัดการข้อยกเว้นทั่วไปหากเกิดข้อผิดพลาด
ตัวอย่างโค้ด
ใช้โค้ดต่อไปนี้เพื่อดึงรายละเอียด Classifier จาก AWS Glue Data Catalog:
import boto3
from botocore.exceptions import ClientError
def get_classifier_details(classifier_name):
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.get_classifier(Name=classifier_name)
return response
except ClientError as e:
raise Exception("boto3 client error in get_classifier_details: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in get_classifier_details: " + e.__str__())
print(get_classifier_details("xml-test"))
ผลลัพธ์
{
'Classifier': {
'GrokClassifier': {
'Name': 'xml-test',
'Classification': 'xml',
'CreationTime': datetime.datetime(2018, 6, 21, 4, 7, 4, tzinfo=tzlocal()),
'LastUpdated': datetime.datetime(2018, 6, 21, 4, 7, 11, tzinfo=tzlocal()),
'Version': 2,
'GrokPattern': 'SYSLOGTIMESTAMP %{MONTH} +%{MONTHDAY} %{TIME}'
}
},
'ResponseMetadata': {
'RequestId': 'c291cce2-...-3552077ddefd',
'HTTPStatusCode': 200,
'HTTPHeaders': {
'date': 'Sun, 21 Feb 2021 07:58:09 GMT',
'content-type': 'application/x-amz-json-1.1',
'content-length': '218',
'connection': 'keep-alive',
'x-amzn-requestid': 'c291cce2-...-3552077ddefd'
},
'RetryAttempts': 0
}
}