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

วิธีใช้ Boto3 เพื่อแบ่งหน้าในตารางทั้งหมดที่มีอยู่ใน AWS Glue


คำชี้แจงปัญหา:ใช้ boto3 ไลบรารีใน Python เพื่อแบ่งหน้าในตารางทั้งหมดจาก AWS Glue Data Catalog ที่สร้างขึ้นในบัญชีของคุณ

แนวทาง/อัลกอริทึมในการแก้ปัญหานี้

  • ขั้นตอนที่ 1: นำเข้า boto3 และ botcore ข้อยกเว้นในการจัดการข้อยกเว้น

  • ขั้นตอนที่ 2: max_items , page_size และ starting_token เป็นพารามิเตอร์ทางเลือกสำหรับฟังก์ชันนี้ ในขณะที่จำเป็นต้องมี database_name

    • max_items หมายถึงจำนวนระเบียนที่จะส่งคืนทั้งหมด หากจำนวนระเบียนที่มีอยู่> max_items จากนั้น NextToken จะถูกจัดเตรียมในการตอบสนองต่อการแบ่งหน้าต่อ

    • page_size หมายถึงขนาดของแต่ละหน้า

    • starting_token ช่วยในการแบ่งหน้า และใช้ NextToken จากการตอบครั้งก่อน

  • ขั้นตอนที่ 3: สร้างเซสชัน AWS โดยใช้ boto3 lib . ตรวจสอบให้แน่ใจว่า region_name ถูกกล่าวถึงในโปรไฟล์เริ่มต้น หากไม่กล่าวถึง ให้ส่ง region_name . อย่างชัดแจ้ง ขณะสร้างเซสชัน

  • ขั้นตอนที่ 4: สร้างไคลเอนต์ AWS สำหรับกาว

  • ขั้นตอนที่ 5: สร้าง ตัวแบ่งหน้า วัตถุที่มีรายละเอียดของตารางทั้งหมดโดยใช้ get_tables

  • ขั้นตอนที่ 5: เรียก แบ่งหน้า และส่งผ่าน database_name เป็นชื่อฐานข้อมูล max_items , page_size และ starting_token เป็น PaginationConfig

  • ขั้นตอนที่ 6: ส่งกลับจำนวนระเบียนตาม max_size และ page_size .

  • ขั้นตอนที่ 7: จัดการกับข้อยกเว้นทั่วไปหากมีสิ่งผิดปกติเกิดขึ้นขณะทำการแบ่งหน้า

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

ใช้รหัสต่อไปนี้เพื่อแบ่งหน้าในตารางทั้งหมดที่สร้างในบัญชีผู้ใช้ -

import boto3
from botocore.exceptions import ClientError

def paginate_through_tables(database_name, max_items=None:int,page_size=None:int, starting_token=None:string):
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
   paginator = glue_client.get_paginator('get_tables')
      response = paginator.paginate(DatabaseName=database_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_tables: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in paginate_through_tables: " + e.__str__())
a = paginate_through_tables("test_db",2,5)
print(*a)

ผลลัพธ์

{'TableList': [
{'Name': 'temp_table', 'DatabaseName': 'test_db', 'Owner': 'abc', 'CreateTime': datetime.datetime(2020, 9, 10, 20, 44, 29, tzinfo=tzlocal()), 'UpdateTime': datetime.datetime(2020, 9, 10, 20, 44, 29, tzinfo=tzlocal()), 'LastAccessTime': datetime.datetime(1970, 1, 1, 5, 30, tzinfo=tzlocal()), 'Retention': 0, 'StorageDescriptor':
{'Columns': [{'Name': 'keyname', 'Type': 'string', 'Comment': ''}, {'Name': 'amount', 'Type': 'string', 'Comment': ''}, {'Name': 'effectivedate', 'Type': 'string', 'Comment': ''}, {'Name': 'clientname', 'Type': 'string', 'Comment': ''}, {'Name': 'accoutname', 'Type': 'varchar(5)', 'Comment': ''}, {'Name': 'clientid', 'Type': 'varchar(6)', 'Comment': ''}], 'Location': 's3://test/', 'InputFormat': 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat', 'OutputFormat': 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat', 'Compressed': False, 'NumberOfBuckets': 0, 'SerdeInfo': {'Name': 'test', 'SerializationLibrary': 'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe', 'Parameters': {}}, 'BucketColumns': [], 'SortColumns': [], 'Parameters': {}, 'StoredAsSubDirectories': False}, 'PartitionKeys': [], 'ViewOriginalText': '', 'ViewExpandedText': '', 'TableType': 'EXTERNAL_TABLE', 'Parameters': {'EXTERNAL': 'TRUE', 'has_encrypted_data': 'false', 'parquet.compression': 'SNAPPY'}, 'CreatedBy': 'arn:aws:sts::782258485841:assumed-role/IVZ-ADFS-NorthBayLead/Hari.Porandla@invesco.com'},
{'Name': 'test_3', 'DatabaseName': 'test_db', 'Owner': 'abc', 'CreateTime': datetime.datetime(2020, 9, 10, 21, 54, 39, tzinfo=tzlocal()), 'UpdateTime': datetime.datetime(2020, 9, 10, 21, 54, 39, tzinfo=tzlocal()), 'LastAccessTime': datetime.datetime(1970, 1, 1, 5, 30, tzinfo=tzlocal()), 'Retention': 0, 'StorageDescriptor': {'Columns': [{'Name': 'keyname', 'Type': 'string', 'Comment': ''}, {'Name': 'amount', 'Type': 'string', 'Comment': ''}, {'Name': 'effectivedate', 'Type': 'string', 'Comment': ''}, {'Name': 'clientname', 'Type': 'string', 'Comment': ''}, {'Name': 'accoutname', 'Type': 'varchar(5)', 'Comment': ''}, {'Name': 'clientid', 'Type': 'varchar(6)', 'Comment': ''}], 'Location': 's3://test3/', 'InputFormat': 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat', 'OutputFormat': 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat', 'Compressed': False, 'NumberOfBuckets': 0, 'SerdeInfo': {'Name': test_3', 'SerializationLibrary': 'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe', 'Parameters': {}}, 'BucketColumns': [], 'SortColumns': [], 'Parameters': {}, 'StoredAsSubDirectories': False}, 'PartitionKeys': [], 'ViewOriginalText': '', 'ViewExpandedText': '', 'TableType': 'EXTERNAL_TABLE', 'CreatedBy': 'arn:aws:sts::***********:assumed-role/abc'}], 'ResponseMetadata': {'RequestId': 'dd35e6c5-*********************1', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Fri, 02 Apr 2021 13:42:48 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '10301', 'connection': 'keep-alive', 'x-amzn-requestid': *******************}, 'RetryAttempts': 0}}