S3 select 查询无法识别数据
S3 select query not recognizing data
我生成一个数据帧,将数据帧作为 CSV 文件写入 S3,然后对 S3 存储桶中的 CSV 执行 select 查询。根据查询和数据,我希望看到打印出“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)
只需要在 SQL 语句中将字符串转换为浮点数。
"SELECT s.\"speed\" FROM s3Object s WHERE cast(s.\"speed\" as float) > 3"
不是没有问题。
我生成一个数据帧,将数据帧作为 CSV 文件写入 S3,然后对 S3 存储桶中的 CSV 执行 select 查询。根据查询和数据,我希望看到打印出“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)
只需要在 SQL 语句中将字符串转换为浮点数。
"SELECT s.\"speed\" FROM s3Object s WHERE cast(s.\"speed\" as float) > 3"
不是没有问题。