首页 文章

如何通过boto3访问eu-west-1中Athena表的Athena / Glue目录?

提问于
浏览
1

我需要编写一份使用Athena数据目录中数据的工作 . 我正在使用Python和boto3 . 由于Glue已经发布,我似乎通过Glue API使用以下代码访问我的数据目录:

import boto3
from pprint import pprint

glue = boto3.client('glue', region_name='us-east-1')

response = glue.get_tables(
    DatabaseName='default'
)

print(pprint(response['TableList']))

但这对于eu-west-1区域不起作用,我猜是因为Glue还没有得到支持 .

如何访问我在eu-west-1中拥有的Athena数据表的数据目录?我认为这应该是可能的,因为它们出现在UI中!

从AWS论坛交叉发布这个:

https://forums.aws.amazon.com/thread.jspa?threadID=269605

2 回答

  • 1

    如果您需要DDL和数据源信息,我的建议是create a paginator然后遍历所有查询执行 . 从那里,您可以拉出所有 CREATE TABLE 语句并根据您的需要解析它们 .

    蛮力方法示例(Python 2):

    import boto3
    
    # Get a list of Query Execution IDs
    client = boto3.client('athena', region_name = 'us-east-1')
    
    paginator = client.get_paginator('list_query_executions')
    
    id_list = [] # List for holding query exection ids
    
    for page in paginator.paginate():
        for id in page['QueryExecutionIds']:
            id_list.append(id)  
    
    # Get a list of query execution metadata
    meta_list = []
    for id in id_list:
        execution_event = client.get_query_execution(QueryExecutionId = id)
        meta_list.append(execution)
    

    您现在可以在查询字符串上过滤元数据列表(重点关注以 CREATE EXTERNAL TABLE 开头的语句 . 您还可以通过 contextCompletionDateTime 组织数据 .

    如果只需要列名和类型,则可以运行 DESCRIBE database_name.table_name; 并获取列名和类型 .

  • 1

    只是为了结束这个 . AWS Glue刚刚发布了eu-west-1(2017-12-19),所以这不再是一个问题 .

    对于尚未获得AWS Glue支持的地区的用户,Zerodf的答案可能仍然是最佳选择

相关问题