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

วิธีใช้ Boto3 แบ่งหน้า (Paginate) ผ่าน Table Versions ใน AWS Glue

ข้อกำหนดปัญหา: ใช้ไลบรารี boto3 ใน Python เพื่อแบ่งหน้าผ่าน table versions ของ table จาก AWS Glue Data Catalog

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

  • ขั้นตอน 1: นำเข้า boto3 และ botocore.exceptions

  • ขั้นตอน 2: พารามิเตอร์: database_name, table_name (บังคับ), max_items, page_size, starting_token (เลือกได้)

    • max_items: จำนวนระเบียนสูงสุด
    • page_size: ขนาดแต่ละหน้า
    • starting_token: ใช้ NextToken จาก response ก่อนหน้า

  • ขั้นตอน 3: สร้าง AWS session ด้วย boto3 พร้อมระบุ region_name

  • ขั้นตอน 4: สร้าง AWS client สำหรับ glue

  • ขั้นตอน 5: สร้าง paginator object ด้วย get_table_versions

  • ขั้นตอน 6: เรียก paginate พร้อมส่ง DatabaseName, TableName, และ PaginationConfig

  • ขั้นตอน 7: คืนค่าระเบียนตาม max_items และ page_size

  • ขั้นตอน 8: จัดการข้อยกเว้นทั่วไป

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

import boto3
from botocore.exceptions import ClientError

def paginate_through_table_versions(database_name, table_name, max_items=None, page_size=None, starting_token=None):
    session = boto3.session.Session()
    glue_client = session.client('glue')
    try:
        paginator = glue_client.get_paginator('get_table_versions')
        response = paginator.paginate(
            DatabaseName=database_name,
            TableName=table_name,
            PaginationConfig={
                'MaxItems': max_items,
                'PageSize': page_size,
                'StartingToken': starting_token
            }
        )
        return response
    except ClientError as e:
        raise Exception("boto3 client error in paginate_through_table_versions: " + e.__str__())
    except Exception as e:
        raise Exception("Unexpected error in paginate_through_table_versions: " + e.__str__())

a = paginate_through_table_versions("test_db", "qa_table", 2, 5)
print(*a)

ผลลัพธ์

{'TableVersions': [{'Table': {'Name': 'qa_table', 'DatabaseName': 'test_db', 'Owner': 'owner', 'CreateTime': datetime.datetime(2018, 9, 24, 20, 43, 3, tzinfo=tzlocal()), 'UpdateTime': datetime.datetime(2018, 9, 24, 20, 43, 3, tzinfo=tzlocal()), 'LastAccessTime': datetime.datetime(2018, 9, 24, 20, 43, 3, tzinfo=tzlocal()), 'Retention': 0, 'StorageDescriptor': {'Columns': [{'Name': 'accounnumber', 'Type': 'int'}, {'Name': 'country', 'Type': 'string'}, {'Name': 'currency', 'Type': 'string'}, {'Name': 'date', 'Type': 'bigint'}, {'Name': 'securitynumber', 'Type': 'string'}, {'Name': 'crossxreferencexcode', 'Type': 'string'}, {'Name': 'securityxdescriptionxxlongx1x', 'Type': 'string'}], 'Location': 's3://qa/success/', 'InputFormat': 'org.apache.hadoop.hive.ql.io.avro.AvroContainerInputFormat', 'OutputFormat': 'org.apache.hadoop.hive.ql.io.avro.AvroContainerOutputFormat', 'Compressed': False, 'NumberOfBuckets': -1, 'SerdeInfo': {'SerializationLibrary': ''}}, 'VersionId': '0'}], 'ResponseMetadata': {'RequestId': '931c99f8-c473-4421-9f0f-1ca33b313e67', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Fri, 02 Apr 2021 13:29:04 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '16718', 'connection': 'keep-alive', 'x-amzn-requestid': '931c99f8-c473-4421-9f0f-1ca33b313e67'}, 'RetryAttempts': 0}}

ฟีเจอร์ Table Versions ใน AWS Glue ช่วยให้สามารถดูประวัติการเปลี่ยนแปลง schema ของ table ได้ (versioning) เหมาะสำหรับ audit และ rollback