সমস্যা: পাইথনে boto3 লাইব্রেরি ব্যবহার করে আপনার অ্যাকাউন্টে তৈরি AWS Glue Data Catalog-এ থেকে সব টেবিল পেজিনেট করা।
পদ্ধতি/অ্যালগরিদম
ধাপ ১: boto3 এবং botocore এক্সেপশন ইম্পোর্ট করুন এক্সেপশন হ্যান্ডল করার জন্য।
ধাপ ২: max_items, page_size এবং starting_token এই ফাংশনের জন্য অপশনাল প্যারামিটার, তবে database_name আবশ্যিক।
max_items নির্দেশ করে মোট কতগুলো রেকর্ড রিটার্ন করতে হবে। যদি উপলব্ধ রেকর্ড max_items-এর চেয়ে বেশি হয়, তবে পেজিনেশন রিজিউম করার জন্য রেসপন্সে NextToken দেওয়া হবে।
page_size প্রতিটি পেজের সাইজ নির্দেশ করে।
starting_token পেজিনেশনে সাহায্য করে এবং এটি পূর্বের রেসপন্স থেকে NextToken ব্যবহার করে।
ধাপ ৩: boto3 লাইব্রেরি ব্যবহার করে AWS সেশন তৈরি করুন। নিশ্চিত করুন region_name ডিফল্ট প্রোফাইলে আছে। না থাকলে সেশন তৈরি করার সময় সিদ্ধা region_name পাস করুন।
ধাপ ৪: glue-এর জন্য AWS ক্লায়েন্ট তৈরি করুন।
ধাপ ৫: get_tables ব্যবহার করে সকল টেবিলের ডিটেইলস ধারণকারী একটি paginator অবজেক্ট তৈরি করুন।
ধাপ ৫: paginate ফাংশন কল করুন এবং database_name কে DatabaseName, max_items, page_size এবং starting_token কে PaginationConfig হিসেবে পাস করুন।
ধাপ ৬: এটি max_size এবং page_size অনুযায়ী রেকর্ডের সংখ্যা রিটার্ন করে।
ধাপ ৭: পেজিনেশনের সময় যদি কোনো সমস্যা হয় তবে জেনেরিক এক্সেপশন হ্যান্ডল করুন।
উদাহরণ কোড
ইউজার অ্যাকাউন্টে তৈরি সকল টেবিল পেজিনেট করতে নিচের কোডটি ব্যবহার করুন:
import boto3
from botocore.exceptions import ClientError
def paginate_through_tables(database_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_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}}