Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีใช้ Boto3 ดึงรายละเอียด Classifier ทั้งหมดจาก AWS Glue Data Catalog


ข้อกำหนดปัญหา

ใช้ไลบรารี Boto3 ใน Python เพื่อดึงรายละเอียดของ Classifier ทั้งหมดที่มีอยู่ใน AWS Glue Data Catalog เช่น ดึงรายละเอียด Classifier ทั้งหมดจากบัญชีผู้ใช้

ขั้นตอนการแก้ปัญหา

  1. ขั้นตอนที่ 1 – นำเข้า boto3 และ botocore.exceptions เพื่อจัดการข้อยกเว้น
  2. ขั้นตอนที่ 2 – ไม่มีพารามิเตอร์ที่ต้องส่ง
  3. ขั้นตอนที่ 3 – สร้าง AWS Session ด้วย Boto3 ตรวจสอบ region_name ในโปรไฟล์เริ่มต้น หากไม่มี ให้ระบุโดยตรง
  4. ขั้นตอนที่ 4 – สร้าง AWS Client สำหรับ Glue
  5. ขั้นตอนที่ 5 – เรียก get_classifiers
  6. ขั้นตอนที่ 6 – จะดึงรายละเอียด Classifier ทั้งหมดที่มีใน AWS Glue Data Catalog
  7. ขั้นตอนที่ 7 – จัดการข้อยกเว้นทั่วไปหากเกิดข้อผิดพลาด

ตัวอย่างโค้ด

ใช้โค้ดต่อไปนี้เพื่อดึงรายละเอียด Classifier ทั้งหมดใน AWS Glue Data Catalog:

import boto3
from botocore.exceptions import ClientError

def get_all_classifier_details():
    session = boto3.session.Session()
    glue_client = session.client('glue')
    try:
        response = glue_client.get_classifiers()
        return response
    except ClientError as e:
        raise Exception("boto3 client error in get_all_classifier_details: " + e.__str__())
    except Exception as e:
        raise Exception("Unexpected error in get_all_classifier_details: " + e.__str__())

print(get_all_classifier_details())

ผลลัพธ์

{
    'Classifiers': [
        {'XMLClassifier': {'Name': 'aiml-linkup', 'Classification': 'xml', 'CreationTime': datetime.datetime(2020, 4, 17, 13, 26, 50, tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2020, 4, 17, 13, 26, 50, tzinfo=tzlocal()), 'Version': 1, 'RowTag': 'job'}},
        {'XMLClassifier': {'Name': 'aiml-test1', 'Classification': 'xml', 'CreationTime': datetime.datetime(2019, 10, 7, 20, 48, 44, tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2019, 10, 7, 20, 48, 44, tzinfo=tzlocal()), 'Version': 1, 'RowTag': 'nitf'}},
        {'GrokClassifier': {'Name': 'classifier1', 'Classification': 'classifier1', '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}'}},
        {'CsvClassifier': {'Name': 'csvquotes', 'CreationTime': datetime.datetime(2020, 9, 10, 5, 6, 29, tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2020, 9, 10, 5, 6, 29, tzinfo=tzlocal()), 'Version': 1, 'Delimiter': ',', 'QuoteSymbol': '"', 'ContainsHeader': 'UNKNOWN', 'DisableValueTrimming': False, 'AllowSingleColumn': False}},
        {'XMLClassifier': {'Name': 'xml-test', 'Classification': 'xml', 'CreationTime': datetime.datetime(2020, 4, 10, 18, 26, 50, tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2020, 4, 15, 0, 3, 8, tzinfo=tzlocal()), 'Version': 2, 'RowTag': 'job'}}
    ],
    'ResponseMetadata': {
        'RequestId': '7fa7a78e-...e4261bfd1',
        'HTTPStatusCode': 200,
        'HTTPHeaders': {
            'date': 'Sun, 21 Feb 2021 08:02:30 GMT',
            'content-type': 'application/x-amz-json-1.1',
            'contentlength': '885',
            'connection': 'keep-alive',
            'x-amzn-requestid': '7fa7a78e-...e4261bfd1'
        },
        'RetryAttempts': 0
    }
}