【问题标题】:How to list all databases and tables in AWS Glue Catalog?如何列出 AWS Glue 目录中的所有数据库和表?
【发布时间】:2021-03-18 01:44:55
【问题描述】:

我在 AWS Glue 控制台中创建了一个开发终端节点,现在我可以在gluepyspark 控制台中访问 SparkContext 和 SQLContext。

如何访问目录并列出所有数据库和表?通常的sqlContext.sql("show tables").show() 不起作用。

CatalogConnection Class 可能会有所帮助,但我不知道它在哪个包中。我尝试从 awsglue.context 导入,但没有成功。

【问题讨论】:

    标签: pyspark-sql aws-glue


    【解决方案1】:

    我花了几个小时试图找到有关 CatalogConnection 类的一些信息,但没有找到任何东西。 (即使在 aws-glue-lib 存储库中 https://github.com/awslabs/aws-glue-libs

    就我而言,我需要 Glue Job Script 控制台中的表名

    最后我使用了 boto 库并通过 Glue 客户端检索了数据库和表名:

    import boto3
    
    
    client = boto3.client('glue',region_name='us-east-1')
    
    responseGetDatabases = client.get_databases()
    
    databaseList = responseGetDatabases['DatabaseList']
    
    for databaseDict in databaseList:
    
        databaseName = databaseDict['Name']
        print '\ndatabaseName: ' + databaseName
    
        responseGetTables = client.get_tables( DatabaseName = databaseName )
        tableList = responseGetTables['TableList']
    
        for tableDict in tableList:
    
             tableName = tableDict['Name']
             print '\n-- tableName: '+tableName
    

    重要的是正确设置区域

    参考: get_databases - http://boto3.readthedocs.io/en/latest/reference/services/glue.html#Glue.Client.get_databases

    get_tables - http://boto3.readthedocs.io/en/latest/reference/services/glue.html#Glue.Client.get_tables

    【讨论】:

    • 当我做 get_databases 时,我只得到前 100 个数据库。尝试增加 MaxResults,但没有用。有人遇到过这个问题吗?
    • 请问设置区域的流程是什么? @SaisumanthGopisetty client = boto3.client('glue', region_name='ap-southeast-1') database = client.get_database(Name='my_db_name') 总是为我返回一个 ConnectionTimeout
    • 如何将结果扩展到100多个?
    • @marcin2x4 请检查下面的 Bao Pham 答案 - 您需要使用“NextToken”并将其传递给下一个请求
    【解决方案2】:

    Glue 每个响应返回一页。如果您有超过 100 个表,请确保使用 NextToken 检索所有表。

    def get_glue_tables(database=None):
        next_token = ""
    
        while True:
            response = glue_client.get_tables(
                DatabaseName=database,
                NextToken=next_token
            )
    
            for table in response.get('TableList'):
                print(table.get('Name'))
    
            next_token = response.get('NextToken')
    
            if next_token is None:
                break
    

    【讨论】:

    • 感谢您的回答。我们遇到了这个问题,想知道为什么即使使用MaxResults=1000,我们的响应仍然只有 100 个表。
    • 我认为你最多只能得到 100 个结果。如果超过 100 个,则需要使用下一个令牌。
    • 即使我的集合有 100 多条记录,我也不断获得 100 条记录。
    • 即使设置为100+,一次最多只能检索100条记录
    • 感谢您的回答。您的代码帮助解决了我的问题。
    【解决方案3】:

    boto3 api 也支持分页,因此您可以使用以下代码:

    import boto3
    
    glue = boto3.client('glue')
    paginator = glue.get_paginator('get_tables')
    page_iterator = paginator.paginate(
        DatabaseName='database_name'   
    )
    
    for page in page_iterator:
        print(page['TableList'])
    

    这样您就不必搞乱 while 循环或下一个标记。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-23
      相关资源
      最近更新 更多