代码之家  ›  专栏  ›  技术社区  ›  Andres Urrego Angel

S3选择Python错误

  •  0
  • Andres Urrego Angel  · 技术社区  · 6 年前

    我试图从s3对象中捕获数据。我使用的S3选择功能如下:

    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'])
    

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

    回溯(最近的最后一次调用):文件 “c:/users/a_urrego/pycharmprojects/dw_flighthub/s3select.py”,第48行, 在里面 OutputSerialization={'JSON':{'RecordDelimiter':','}},文件 “C:\users\a urrego\appdata\local\programs\python\python36-32\lib\site packages\botocore\client.py”, 314行,在API调用中 返回self._make_api_call(operation_name,kwargs)file“c:\用户\用户\应用程序数据\本地\程序\ python\python36-32\lib\site packages\botocore\client.py”, 612行,在make_api_调用中 引发错误类(已分析的响应,操作名)botocore.exceptions.clienterror:发生错误 (parseUnexpectedToken)调用selectobjectcontent操作时: 意外的标记发现为:在第1行第33列。

    进程已完成,退出代码为1

    1 回复  |  直到 6 年前
        1
  •  0
  •   BowlingHawk95    6 年前

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

    "select * from s3object S3Object AS s"
    

    一般的sql语法是

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

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

    我还没有使用过boto3的这个特性,但这似乎是经过3分钟的搜索和阅读错误信息后出现的问题。

    [编辑]

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