Amazon's S3 Select允许用户针对 S3 对象编写 SQL,但关于支持哪些标准 SQL 功能的文档不足。
根据documentation , Amazon S3 Select 支持 AVG
、COUNT
、MAX
、MIN
和 SUM
.但是当我运行任何聚合查询时,我会收到类似
botocore.exceptions.ClientError: An error occurred (UnsupportedSqlOperation) when calling the SelectObjectContent operation: Unsupported SQL operation GROUP BY. Please check the service documentation for supported operations.
这里有一些要重现的代码:
import boto3
client = boto3.client('s3')
response = client.select_object_content(Bucket='my-bucket', Key='object.csv',
ExpressionType='SQL', Expression="select ID, count(*) from s3object group by s.ID ",
InputSerialization = {'CSV':{"FileHeaderInfo": "Use"}, 'CompressionType': 'GZIP'},
OutputSerialization = {'CSV':{}})
event_stream = response['Payload']
with open('output', 'wb') as f:
for event in event_stream:
if 'Records' in event:
data = event['Records']['Payload']
f.write(data)
最佳答案
根据 John 的评论,只有在没有 GROUP BY
的情况下才支持聚合函数。
这里有一些注意事项和指示
- 显然,您可以使用
WHERE
或LIMIT
子句过滤要执行聚合的集合。 - 一切都是字符串,所以任何数字操作都需要一个
CAST
,例如sum(cast(my_col as float))
- 使用
MAX
和MIN
将数字列解释为字符串的混合结果,即没有转换;它最初是成功的,但经过进一步处理后我无法重现该成功 - 无法在与非聚合函数相同的查询中执行聚合函数,并且错误消息含糊不清:
查询表达式中提供的列名或路径不存在
关于amazon-s3 - S3 Select 中的聚合函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50218467/