1

我生成一个数据帧,将数据帧作为 CSV 文件写入 S3,然后对 S3 存储桶中的 CSV 执行选择查询。根据查询和数据,我希望看到“4”和“10”打印,但我只看到“4”。由于某种原因,S3 没有看到“10”。

它适用于日期之间的过滤。

import pandas as pd
import s3fs
import boto3

# dataframe
d = {'date':['1990-1-1','1990-1-2','1990-1-3','1999-1-4'], 'speed':[0,10,3,4]}
df = pd.DataFrame(d)

# write csv to s3
bytes_to_write = df.to_csv(index=False).encode()
fs = s3fs.S3FileSystem()
with fs.open('app-storage/test.csv', 'wb') as f:
    f.write(bytes_to_write)

# query csv in s3 bucket
s3 = boto3.client('s3',region_name='us-east-1')

resp = s3.select_object_content(
    Bucket='app-storage',
    Key='test.csv',
    ExpressionType='SQL',
    Expression="SELECT s.\"speed\" FROM s3Object s WHERE s.\"speed\" > '3'",
    InputSerialization = {'CSV': {"FileHeaderInfo": "Use"}},
    OutputSerialization = {'CSV': {}},
)

for event in resp['Payload']:
    if 'Records' in event:
        records = event['Records']['Payload'].decode('utf-8')
        print(records)
4

1 回答 1

1

只需要将字符串转换为在 SQL 语句中浮动。

"SELECT s.\"speed\" FROM s3Object s WHERE cast(s.\"speed\" as float) > 3"

不是它没有问题。

于 2020-04-24T23:48:07.457 回答