如何使用 Boto3 对 AWS Glue 中存在的所有爬虫进行分页

awsboto3pythonserver side programmingprogramming更新于 2023/12/24 16:25:00

在本文中,我们将了解如何对 AWS Glue 中存在的所有爬虫进行分页。

示例

对账户中创建的 AWS Glue 数据目录中的所有爬虫进行分页。

问题陈述:使用 Python 中的 boto3 库对账户中创建的 AWS Glue 数据目录中的所有爬虫进行分页

解决此问题的方法/算法

  • 步骤 1:导入 boto3botocore 异常来处理异常。

  • 步骤2: ma​​x_itemspage_sizestarting_token 是此函数的参数。

    • ma​​x_items 表示要返回的记录总数。如果可用记录数 > ma​​x_items,则响应中将提供 NextToken 以恢复分页。

    • page_size 表示每页的大小。

    • Starting_token 有助于分页,它使用来自上一个响应的 NextToken

  • 步骤 3: 使用 boto3 lib 创建 AWS 会话。确保默认配置文件中提到了region_name。如果没有提及,则在创建会话时明确传递 region_name

  • 步骤 4:glue 创建 AWS 客户端。

  • 步骤 5: 使用 get_crawlers 创建包含所有爬虫程序详细信息的 paginator 对象。

  • 步骤 6: 调用 paginate 函数并传递 ma​​x_itemspage_sizestarting_token 作为 PaginationConfig 参数。

  • 步骤 7: 根据 max_size 和 page_size 返回记录数。

  • 步骤8:如果分页时出现错误,处理通用异常。

示例代码

使用以下代码对用户帐户中创建的所有爬虫进行分页 −

import boto3
from botocore.exceptions import ClientError

def paginate_through_crawlers(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_crawlers')
      response = paginator.paginate(PaginationConfig={
         'MaxItems':max_items,
         'PageSize':page_size,
         'StartingToken':starting_token}
      )
return response
   except ClientError as e:
      raise Exception("boto3 client error in paginate_through_crawlers: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in paginate_through_crawlers: " + e.__str__())

#1st Run
a = paginate_through_crawlers(3,5)
print(*a)
#2nd Run
for items in a:
next_token = (items['NextToken'])
b = paginate_through_crawlers(3,5,next_token)

输出

#1st Run
{'Crawlers':
[{'Name': 'DailyTest_v1.01', 'Role': 'ds-dev', 'Targets': {'S3Targets': [{'Path': 's3://**************/UIT_Raw/', 'Exclusions': []}], 'JdbcTargets': [], 'DynamoDBTargets': [], 'CatalogTargets': []}, 'DatabaseName': 'default', 'Classifiers': [], 'SchemaChangePolicy': {'UpdateBehavior': 'UPDATE_IN_DATABASE', 'DeleteBehavior': 'DEPRECATE_IN_DATABASE'}, 'State': 'READY', 'TablePrefix': 'test_uit_', 'CrawlElapsedTime': 0, 'CreationTime': datetime.datetime(2020, 11, 23, 17, 50, 20, tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2020, 12, 11, 18, 22, 34, tzinfo=tzlocal()), 'LastCrawl': {'Status': 'SUCCEEDED', 'LogGroup': '/aws-glue/crawlers', 'LogStream': '01_DailySalesAsOff_v1.01', 'MessagePrefix': '71fc0485-4755-42ca-a208-0654dd84d011', 'StartTime': datetime.datetime(2020, 12, 11, 18, 54, 46, tzinfo=tzlocal())}, 'Version': 10},
{'Name': 'Client_list', 'Role': 'ds-dev', 'Targets': {'S3Targets': [{'Path': 's3://************Client_list_01072021/', 'Exclusions': []}], 'JdbcTargets': [], 'DynamoDBTargets': [], 'CatalogTargets': []}, 'DatabaseName': 'ds_adhoc', 'Classifiers': [], 'SchemaChangePolicy': {'UpdateBehavior': 'UPDATE_IN_DATABASE', 'DeleteBehavior': 'DEPRECATE_IN_DATABASE'}, 'State': 'READY', 'CrawlElapsedTime': 0, 'CreationTime': datetime.datetime(2021, 1, 8, 3, 52, 27, tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2021, 1, 8, 3, 52, 27, tzinfo=tzlocal()), 'LastCrawl': {'Status': 'SUCCEEDED', 'LogGroup': '/aws-glue/crawlers', 'LogStream': 'Client_list_01072021', 'MessagePrefix': '41733a73-8946-4906-969c-f9581237b833', 'StartTime': datetime.datetime(2021, 1, 8, 3, 52, 45, tzinfo=tzlocal())}, 'Version': 1},
{'Name': 'Data Dimension', 'Role': 'qa-edl-glue-role', 'Targets': {'S3Targets': [{'Path': 's3://**************/data_dimension', 'Exclusions': []}], 'JdbcTargets': [], 'DynamoDBTargets': [], 'CatalogTargets': []}, 'DatabaseName': 'qa_edl_glue_database', 'Classifiers': [], 'SchemaChangePolicy': {'UpdateBehavior': 'UPDATE_IN_DATABASE', 'DeleteBehavior': 'DEPRECATE_IN_DATABASE'}, 'State': 'READY', 'CrawlElapsedTime': 0, 'CreationTime': datetime.datetime(2020, 8, 12, 0, 36, 21, tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2021, 3, 28, 13, 21, 19, tzinfo=tzlocal()), 'LastCrawl': {'Status': 'SUCCEEDED', 'LogGroup': '/aws-glue/crawlers', 'LogStream': 'Data Dimension', 'MessagePrefix': 'ee09c0ac-b778-467e-a941-c86c37edde47', 'StartTime': datetime.datetime(2021, 3, 28, 14, 1, 50, tzinfo=tzlocal())}, 'Version': 11}],
'NextToken': 'crawlr-wells',
'ResponseMetadata': {'RequestId': '8a6114ec-************d66', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Fri, 02 Apr 2021 11:00:17 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '86627', 'connection': 'keep-alive', 'x-amzn-requestid': '8a6114ec-*****************66'}, 'RetryAttempts': 0}}

#2nd Run
{'Crawlers': [
{'Name': 'crwlr-cw-etf', 'Role': 'dev-ds-glue-role', 'Targets': {'S3Targets': [{'Path': 's3://***********CW_2020Q3', 'Exclusions': []}], 'JdbcTargets': [], 'DynamoDBTargets': [], 'CatalogTargets': []}, 'DatabaseName': 'ivz-dev-ds-data-packs', 'Description': 'Data pack crawlers', 'Classifiers': [], 'SchemaChangePolicy': {'UpdateBehavior': 'UPDATE_IN_DATABASE', 'DeleteBehavior': 'DEPRECATE_IN_DATABASE'}, 'State': 'READY', 'CrawlElapsedTime': 0, 'CreationTime': datetime.datetime(2020, 10, 28, 17, 30, 41, tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2020, 11, 17, 12, 47, 21, tzinfo=tzlocal()), 'LastCrawl': {'Status': 'SUCCEEDED', 'LogGroup': '/aws-glue/crawlers', 'LogStream': crwlr-cw-etf', 'MessagePrefix': '49cb001f-3005-43ef-96f7-a1d45917c808', 'StartTime': datetime.datetime(2020, 11, 17, 17, 41, 16, tzinfo=tzlocal())}, 'Version': 5, 'Configuration': '{"Version":1.0,"Grouping":{"TableGroupingPolicy":"CombineCompatibleSchemas"}}'},
{'Name': 'crwlr-data-packs', 'Role': 'dev-ds-glue-role', 'Targets': {'S3Targets': [{'Path': 's3://*****************raw-parquet', 'Exclusions': []}], 'JdbcTargets': [], 'DynamoDBTargets': [], 'CatalogTargets': []}, 'DatabaseName': 'ivz-dev-ds-data-packs', 'Classifiers': [], 'SchemaChangePolicy': {'UpdateBehavior': 'UPDATE_IN_DATABASE', 'DeleteBehavior': 'DEPRECATE_IN_DATABASE'}, 'State': 'READY', 'CrawlElapsedTime': 0, 'CreationTime': datetime.datetime(2020, 4, 21, 20, 49, 6, tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2020, 4, 21, 20, 49, 6, tzinfo=tzlocal()), 'LastCrawl': {'Status': 'SUCCEEDED', 'LogGroup': '/aws-glue/crawlers', 'LogStream': 'crwlr-data-packs-mstr', 'MessagePrefix': '26aedfe8-f631-41e3-acd5-35877d71be1b', 'StartTime': datetime.datetime(2020, 4, 21, 20, 49, 10, tzinfo=tzlocal())}, 'Version': 1, 'Configuration': '{"Version":1.0,"Grouping":{"TableGroupingPolicy":"CombineCompatibleSchemas"}}'},
{'Name': 'crwlr-data-packs', 'Role': 'dev-ds-glue-role', 'Targets': {'S3Targets': [{'Path': 's3://******ubs-raw-parquet', 'Exclusions': []}], 'JdbcTargets': [], 'DynamoDBTargets': [], 'CatalogTargets': []}, 'DatabaseName': 'ivz-dev-ds-data-packs', 'Classifiers': [], 'SchemaChangePolicy': {'UpdateBehavior': 'UPDATE_IN_DATABASE', 'DeleteBehavior': 'DEPRECATE_IN_DATABASE'}, 'State': 'READY', 'CrawlElapsedTime': 0, 'CreationTime': datetime.datetime(2020, 4, 14, 2, 31, 25, tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2020, 5, 28, 21, 52, 4, tzinfo=tzlocal()), 'LastCrawl': {'Status': 'SUCCEEDED', 'LogGroup': '/aws-glue/crawlers', 'LogStream': 'ivz-dev-ds-crwlr-data-packs-ubs', 'MessagePrefix': '6c00dc7b-181e-4eb2-8d6d-d195f97b03ce', 'StartTime': datetime.datetime(2020, 6, 4, 16, 13, 18, tzinfo=tzlocal())}, 'Version': 5, 'Configuration': '{"Version":1.0,"Grouping":{"TableGroupingPolicy":"CombineCompatibleSchemas"}}'}],
'NextToken': 'discovery_rep',
'ResponseMetadata': {'RequestId': '43e0b162-***********, 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Fri, 02 Apr 2021 11:00:18 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '89110', 'connection': 'keep-alive', 'x-amzn-requestid': '43e0b162-********}, 'RetryAttempts': 0}}

相关文章