在 S3 Select 中按索引查询行

Querying rows by index in S3 Select

使用mysql以下代码:

SELECT * from TABLE limit 5, 10 

将拉取 table 的第 5 行到第 10 行。通过 S3 select 中的 SQL 引擎(我相信是 PrestoDB)执行此操作的等效项是什么?是否有适用于 S3 的 rownumber 构造函数或运算符 select?

根据他们的文档,PrestoDB 支持 LIMIT 子句,但只有一个行数参数。它不支持偏移量的第二个参数。

自己阅读文档:https://prestodb.io/docs/current/sql/select.html

S3 Select 文档位于:SQL Reference for Amazon S3 Select and Amazon Glacier Select - Amazon Glacier

LIMIT 子句记录为:

LIMIT number

The LIMIT clause limits the number of records that you want the query to return based on number.

所以,不可用。

它不能作为一项功能使用,但我们的项目就是这样做的。

在将 CSV 文件上传到 S3 之前,我们刚刚在 CSV 中添加了一个字段 row_index。

row_index, field1
1, a, 
2, b,
3, c,
4, d,
5, d,

所以查询

Select * from Table Limit 1, 4

将替换为

Select * from s3 where row_index >= 1 and row_index <= 4

请记住,如果您的文件太大,扫描成本会增加。但是您可以考虑将文件拆分成更小的块,以便更好地以低成本进行查询。

比如一个100k-rows.csv的文件可以拆分成10个文件

1-10000-rows.csv
10001-20000-rows.csv
...

然后您可以读取 offsetlimit 变量来查找应该为您的行查询哪个文件。例如

to fetch rows 200- 300 - You will query the 1-10000-rows.csv
to fetch rows 12000- 12500  - You will query the 10001-20000-rows.csv