S3 选择 Python

2024-10-04 01:31:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个S3对象捕获数据。我使用的是S3 Select功能,如下所示:

boto3版本:1.7.59

import boto3

s3 = boto3.client('s3')
r = s3.select_object_content(
    Bucket="bucket",
    Key="file.json",
    ExpressionType='SQL',
    Expression="select * from s3object S3Object AS s",
    InputSerialization = {
                            'JSON': {
                            'Type': 'LINES'
                            }
                        },
    OutputSerialization = { 'JSON': { 'RecordDelimiter': ',' } },
)


for event in r['Payload']:
    if 'Records' in event:
        records = event['Records']['Payload'].decode('utf-8')
        print(records)
    elif 'Stats' in event:
        statsDetails = event['Stats']['Details']
        print("Stats details bytesScanned: ")
        print(statsDetails['BytesScanned'])
        print("Stats details bytesProcessed: ")
        print(statsDetails['BytesProcessed'])

运行代码后,我得到错误:

Traceback (most recent call last): File "C:/Users/a_urrego/PycharmProjects/DW_FlightHub/S3Select.py", line 48, in OutputSerialization = { 'JSON': { 'RecordDelimiter': ',' } }, File "C:\Users\a_urrego\AppData\Local\Programs\Python\Python36-32\lib\site-packages\botocore\client.py", line 314, in _api_call return self._make_api_call(operation_name, kwargs) File "C:\Users\a_urrego\AppData\Local\Programs\Python\Python36-32\lib\site-packages\botocore\client.py", line 612, in _make_api_call raise error_class(parsed_response, operation_name) botocore.exceptions.ClientError: An error occurred (ParseUnexpectedToken) when calling the SelectObjectContent operation: Unexpected token found AS:as at line 1, column 33.

Process finished with exit code 1


Tags: inpyclienteventjsons3statsline
1条回答
网友
1楼 · 发布于 2024-10-04 01:31:16

您传递的SQL表达式似乎无效:

"select * from s3object S3Object AS s"

一般的SQL语法

^{pr2}$

但看起来你在那里复制了一个表名或其他东西。SQL语句的大写是可选的,但我倾向于喜欢它。在

我还没有使用boto3的这一功能,但这似乎是3分钟的谷歌搜索和阅读错误消息后的问题。在

[编辑]

更新了我上面的模板后发现一个打字错误。同样值得注意的是,在这个用例中表别名是不必要的,因为它是一个非常简单的SELECT语句。在

相关问题 更多 >