萨阿 Select Python 阿拉尔

S3 Select Python error

我正在尝试从 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

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

"select * from s3object S3Object AS s"

一般 SQL 语法将是

"SELECT <columns | *> FROM <table> <alias>"

但您似乎在其中复制了 table 名称或其他内容。 SQL 语句中的大写字母是可选的,但我倾向于喜欢它。

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

[编辑]

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

你也不需要AS,它会引发另一个错误,下面就足够了:

select * from s3object S3Object