避免Athena查询的元数据进入S3桶(Python脚本)。
创始人
2024-12-16 01:00:07
0

可以使用以下代码示例来避免Athena查询的元数据进入S3桶。

import boto3

s3 = boto3.resource('s3')

# Create Athena client
athena = boto3.client('athena')

# Set query parameters
database = 'mydatabase'
query = 'SELECT * FROM mytable'

# Execute query
response = athena.start_query_execution(
    QueryString=query,
    QueryExecutionContext={
        'Database': database
    },
    ResultConfiguration={
        'OutputLocation': 's3://mybucket/myquery/'
    }
)

# Check if query executed successfully
execution_id = response['QueryExecutionId']
status = athena.get_query_execution(QueryExecutionId=execution_id)['QueryExecution']['Status']['State']
while status in ['QUEUED', 'RUNNING']:
    status = athena.get_query_execution(QueryExecutionId=execution_id)['QueryExecution']['Status']['State']
    print(f"Query: {query} is {status}")
    time.sleep(10)

# Download query result from S3 bucket
if status == 'SUCCEEDED':
    results_key = response['QueryExecution']['ResultConfiguration']['OutputLocation'].split('/')[-1]
    results_object = s3.Object('mybucket', f'myquery/{results_key}')
    results_content = results_object.get()['Body'].read().decode('utf-8')
    print(results_content)

# Remove metadata from S3 bucket
if status == 'SUCCEEDED':
    del_response = s3.Object('mybucket', f'myquery/{results_key}.metadata').delete()
    print(del_response)

在这个示例中,我们首先创建了一个S3资源和一个Athena客户端。然后,我们设置了一个database和一个query,并通过Athena.start_query_execution()执行了查询。在执行查询后,我们检查查询是否成功执行,然后从S3桶中下载查询结果。最后,我们使用S3.Object().delete()方法删除查询结果中的元数据。

相关内容

热门资讯

科技介绍(wepOkE)外挂透... 科技介绍(wepOkE)外挂透明挂辅助器(智能ai代打)果真真的有挂(2023已更新)(哔哩哔哩)1...
指导大家(WepOke)外挂透... 指导大家(WepOke)外挂透明挂辅助黑科技(透视)总是真的有挂(2022已更新)(小红书)1、让任...
热点推荐(AaPOKER)外挂... 热点推荐(AaPOKER)外挂透明挂辅助工具(软件透明挂)总是真的有挂(2021已更新)(小红书)辅...
玩家攻略推荐(fishpoke... 玩家攻略推荐(fishpoker俱乐部)外挂透明挂辅助脚本(透视)一般真的有挂(2024已更新)(哔...
如何分辨真伪(菠萝德州)外挂透... 如何分辨真伪(菠萝德州)外挂透明挂辅助软件(智能ai代打)一贯真的有挂(2022已更新)(百度贴吧)...
重大通报(pokerx)外挂透... 重大通报(pokerx)外挂透明挂辅助器(辅助挂)一贯真的有挂(2023已更新)(今日头条)1、构建...
2024教程(WepoKe)外... 2024教程(WepoKe)外挂透明挂辅助黑科技(透视)果然真的有挂(2024已更新)(今日头条)1...
玩家爆料(aAPOKER)外挂... 玩家爆料(aAPOKER)外挂透明挂辅助黑科技(智能ai代打)果然真的有挂(2022已更新)(微博热...
详细说明(AApoker)外挂... 详细说明(AApoker)外挂透明挂辅助挂(软件透明挂)一直真的有挂(2022已更新)(百度)软件透...
重大通报(we辅助poker)... 重大通报(we辅助poker)外挂透明挂辅助神器(软件透明挂)其实真的有挂(2021已更新)(小红书...