3.5. S3 选择操作(技术预览)


重要

S3 选择操作只是一个技术预览功能。红帽产品服务级别协议(SLA)不支持技术预览功能,且其功能可能并不完善,因此红帽不建议在生产环境中使用它们。这些技术预览功能可以使用户提早试用新的功能,并有机会在开发阶段提供反馈意见。如需了解更多详细信息,请参阅红帽技术预览功能的支持范围

作为开发者,您可以运行 S3 来加快吞吐量。用户可以在没有介质器的情况下直接运行 S3 选择查询。

有两个 S3 选择工作流,一个用于 CSV,一个用于 Apache Parquet (Parquet),它为 CSV 和 Parquet 对象提供 S3 选择操作。

  • CSV 文件以纯文本格式存储表格数据。文件的每一行都是数据记录。
  • Parquet 是一个开源、面向列的数据文件格式,专为高效数据存储和检索而设计。它提供了高效的数据压缩和编码方案,以增强性能以批量处理复杂数据。

例如,带有几 GB 数据的 CSV 或 Parquet S3 对象允许用户提取单个列,该列由另一个列过滤:

示例

select customerid from s3Object where age>30 and age<65;
Copy to Clipboard Toggle word wrap

目前,S3 对象必须通过 Ceph 对象网关从 Ceph OSD 检索数据,然后才能过滤和提取数据。当对象较大且查询更具体时,性能会提高性能。与 CSV 相比,Parquet 格式可以更有效地处理。

先决条件

  • 一个正在运行的 Red Hat Ceph Storage 集群。
  • RESTful 客户端。
  • 创建的用户具有访问权限的 S3 用户。

3.5.1. S3 从对象中选择内容

选择的对象内容 API 通过结构化查询语言(SQL)过滤对象的内容。如需清单对象中应驻留的内容的说明,请参阅 AWS 系统管理器用户指南中的清单收集的元数据部分。清单内容会影响针对该清单运行的查询类型。可能会提供基本信息的 SQL 语句数量较大,但 S3 选择是一个类似于 SQL 的实用程序,因此不支持某些运算符,如 group-byjoin

对于 CSV,您必须将数据序列化格式指定为对象的逗号分隔值,以检索指定的内容。Parquet 没有分隔符,因为它采用二进制格式。Amazon Web Services (AWS)命令行界面(CLI)选择对象内容使用 CSV 或 Parquet 格式将对象数据解析到记录中,仅返回查询中指定的记录。

您必须为响应指定数据序列化格式。这个操作必须具有 s3:GetObject 权限。

注意
  • InputSerialization 元素描述了正在查询的对象中数据的格式。对象可以是 CSV 或 Parquet 格式。
  • OutputSerialization 元素是 AWS-CLI 用户客户端的一部分,并描述了如何格式化输出数据。Ceph 为 AWS-CLI 实施服务器客户端,因此根据 OutputSerialization (当前只是 CSV)提供相同的输出。
  • InputSerialization 的格式不需要与 OutputSerialization 的格式匹配。例如,您可以在 InputSerialization 中指定 Parquet,在 OutputSerialization 中指定 CSV。

语法

POST /BUCKET/KEY?select&select-type=2 HTTP/1.1\r\n
Copy to Clipboard Toggle word wrap

示例

POST /testbucket/sample1csv?select&select-type=2 HTTP/1.1\r\n
POST /testbucket/sample1parquet?select&select-type=2 HTTP/1.1\r\n
Copy to Clipboard Toggle word wrap

请求实体

Bucket
描述
要从中选择对象内容的存储桶。
Type
字符串
必需
描述
对象密钥。
长度限制
最小长度为 1.
Type
字符串
必需
SelectObjectContentRequest
描述
选择对象内容请求参数的根级别标签。
Type
字符串
必需
表达式
描述
用于查询对象的表达式。
Type
字符串
必需
ExpressionType
描述
示例 SQL 提供的表达式的类型。
Type
字符串
有效值
SQL
必需
InputSerialization
描述
描述正在查询的对象中数据的格式。
Type
字符串
必需
OutputSerialization
描述
以逗号分隔符和新行返回的数据格式。
Type
字符串
必需

响应实体

如果操作成功,服务会返回 HTTP 200 响应。服务以 XML 格式返回数据:

payload
描述
有效负载参数的根级别标签。
Type
字符串
必需
Records
描述
记录事件。
Type
base64 编码的二进制数据对象
必需
Stats
描述
统计事件。
Type
Long
必需

Ceph 对象网关支持以下响应:

示例

{:event-type,records} {:content-type,application/octet-stream} :message-type,event}
Copy to Clipboard Toggle word wrap

语法(用于 CSV)

aws --endpoint-URL http://localhost:80 s3api select-object-content
 --bucket BUCKET_NAME
 --expression-type 'SQL'
 --input-serialization
 '{"CSV": {"FieldDelimiter": "," , "QuoteCharacter": "\"" , "RecordDelimiter" : "\n" , "QuoteEscapeCharacter" : "\\" , "FileHeaderInfo": "USE" }, "CompressionType": "NONE"}'
 --output-serialization '{"CSV": {}}'
 --key OBJECT_NAME
 --expression "select count(0) from s3object where int(_1)<10;" output.csv
Copy to Clipboard Toggle word wrap

示例(用于 CSV)

aws --endpoint-url http://localhost:80 s3api select-object-content
 --bucket testbucket
 --expression-type 'SQL'
 --input-serialization
 '{"CSV": {"FieldDelimiter": "," , "QuoteCharacter": "\"" , "RecordDelimiter" : "\n" , "QuoteEscapeCharacter" : "\\" , "FileHeaderInfo": "USE" }, "CompressionType": "NONE"}'
 --output-serialization '{"CSV": {}}'
 --key testobject
 --expression "select count(0) from s3object where int(_1)<10;" output.csv
Copy to Clipboard Toggle word wrap

语法(用于 Parquet)

aws --endpoint-URL http://localhost:80 s3api select-object-content
 --bucket BUCKET_NAME
 --expression-type 'SQL'
 --input-serialization
 '{"Parquet": {}, {"CompressionType": "NONE"}'
 --output-serialization '{"CSV": {}}'
 --key OBJECT_NAME.parquet
 --expression "select count(0) from s3object where int(_1)<10;" output.csv
Copy to Clipboard Toggle word wrap

示例(用于 Parquet)

aws --endpoint-url http://localhost:80 s3api select-object-content
 --bucket testbucket
 --expression-type 'SQL'
 --input-serialization
 '{"Parquet": {}, {"CompressionType": "NONE"}'
 --output-serialization '{"CSV": {}}'
 --key testobject.parquet
 --expression "select count(0) from s3object where int(_1)<10;" output.csv
Copy to Clipboard Toggle word wrap

支持的功能

目前只支持 AWS s3 选择命令的一部分:

Expand
功能详情描述示例

算术运算符

^ * % / + - ( )

 

select (int(_1)+int(_2))*int(_9) from s3object;

算术运算符

% modulo

 

select count(*) from s3object where cast(_1 as int)%2 == 0;

算术运算符

^ power-of

 

从 s3object 选择 cast(2^10,为 int);

比较运算符

> < >= ⇐ == !=

 

select _1,_2 from s3object where (int(_1)+int(_3))>int(_5);

逻辑运算符

AND 或 NOT

 

select count(*) from s3object where not (int(1)>123 and int(_5)<200);

逻辑运算符

is null

为表达式中的 null 返回 true/false

 

逻辑运算符和 NULL

is not null

为表达式中的 null 返回 true/false

 

逻辑运算符和 NULL

未知状态

查看 null-handle,并观察使用 NULL 的逻辑操作的结果。查询返回 0

select count(*) from s3object where null and (3>2);

带有 NULL 的算术运算符

未知状态

查看 null-handle,并观察使用 NULL 的二进制操作的结果。查询返回 0

select count(*) from s3object where (null+1) and (3>2);

与 NULL 进行比较

未知状态

回顾空客户端并观察与 NULL 比较操作的结果。查询返回 0

select count(*) from s3object where (null*1.5) != 3;

缺少列

未知状态

 

select count(*) from s3object where _1 is null;

projection 列

与 if or then or else 类似

选择条件

when (1+1==(2+1)*3) then ‘case_1’ when 4*3)==(12 then ‘case_2’ else ‘case_else’ end, age*2 from s3object;

逻辑运算符

 

coalesce 返回第一个非null 参数

select coalesce(nullif(5,5),nullif(1,1.0),age+12) from s3object;

逻辑运算符

 

如果两个参数都相等,则 nullif 返回 null,否则第一个参数为nullif (1,1)=NULL nullif (null,1)=NULL nullif (2,1)=2

select nullif(cast(_1 as int),cast(_2 as int)) from s3object;

逻辑运算符

 

{expression} in ( .. {expression} ..)

select count(*) from s3object where ‘ben’ in (trim(_5),substring(_1,char_length(_1)-3,3),last_name);

逻辑运算符

 

{expression} 和 {expression} 之间的 {expression}

select countprincipal from stdin where substring (_3,char_length (_3),1) between "x" and trim (_1) and substring (_3,char_length (_3)-1,1)== ":";

逻辑运算符

 

{expression} like {match-pattern}

select count() from s3object where first_name like ‘%de_’; select count() from s3object where _1 like "%a[r-s];

casting operator

  

select cast(123 as int)%2 from s3object;

casting operator

  

select cast(123.456 as float)%2 from s3object;

casting operator

  

select cast(‘ABC0-9’ as string),cast(substr(‘ab12cd’,3,2) as int)*4 from s3object;

casting operator

  

select cast(substring(‘publish on 2007-01-01’,12,10) as timestamp) from s3object;

非 AWS casting operator

  

select int(_1),int( 1.2 + 3.4) from s3object;

非 AWS casting operator

  

select float(1.2) from s3object;

非 AWS casting operator

  

select timestamp(‘1999:10:10-12:23:44’) from s3object;

聚合功能

sun

 

select sum(int(_1)) from s3object;

聚合功能

avg

 

select avg(cast(_1 a float) + cast(_2 as int)) from s3object;

聚合功能

分钟

 

select avg(cast(_1 a float) + cast(_2 as int)) from s3object;

聚合功能

max

 

select max(float(_1)),min(int(_5)) from s3object;

聚合功能

数量

 

select count(*) from s3object where (int(1)+int(_3))>int(_5);

时间戳功能

extract

 

select count(*) from s3object where extract(‘year’,timestamp(_2)) > 1950 and extract(‘year’,timestamp(_1)) < 1960;

时间戳功能

dateadd

 

select count(0) from s3object where datediff(‘year’,timestamp(_1),dateadd(‘day’,366,timestamp(_1))) == 1;

时间戳功能

datediff

 

select count(0) from s3object where datediff(‘month’,timestamp(_1),timestamp(_2))) == 2;

时间戳功能

utcnow

 

select count(0) from s3object where datediff(‘hours’,utcnow(),dateadd(‘day’,1,utcnow())) == 24

字符串函数

子字符串

 

select count(0) from s3object where int(substring(_1,1,4))>1950 and int(substring(_1,1,4))<1960;

字符串函数

trim

 

select trim(‘ foobar ‘) from s3object;

字符串函数

trim

 

select trim(trailing from ‘ foobar ‘) from s3object;

字符串函数

trim

 

select trim(leading from ‘ foobar ‘) from s3object;

字符串函数

trim

 

select trim(both ‘12’ from ‘1112211foobar22211122’) from s3objects;

字符串函数

lower 或 upper

 

select trim(both ‘12’ from ‘1112211foobar22211122’) from s3objects;

字符串函数

char_length, character_length

 

select count(*) from s3object where char_length(_3)==3;

复杂的查询

  

select sum(cast(_1 as int)),max(cast(_3 as int)), substring(‘abcdefghijklm’, (2-1)*3+sum(cast(_1 as int))/sum(cast(_1 as int))+1, (count() + count(0))/count(0)) from s3object;

别名支持

  

select int(_1) as a1, int(_2) as a2 , (a1+a2) as a3 from s3object where a3>100 and a3<300;

3.5.2. S3 支持的选择功能

S3 选择支持以下功能:.Timestamp

timestamp(string)
描述
将字符串转换为基本时间戳类型。
支持
目前它转换:yyyy:mm:dd hh:mi:dd
extract(date-part,timestamp)
描述
根据 date-part 从输入时间戳中提取的整数。
支持
date-part: year、month、week、day.
dateadd(date-part ,integer,timestamp)
描述
返回时间戳,根据输入时间戳和 date-part 的结果进行计算。
支持
日期部分: year,month,day.
datediff(date-part,timestamp,timestamp)
描述
返回整数,根据 date-part 在两个时间戳之间计算的结果。
支持
日期部分: year、month、day、hours.
utcnow()
描述
返回当前时间的时间戳。

聚合

count()
描述
如果出现某个条件,则根据与条件匹配的行数返回整数。
sum(expression)
描述
如果出现某个条件,则每行上返回表达式摘要。
avg(expression)
描述
如果出现某个条件,则每行上返回一个平均表达式。
max(expression)
描述
如果出现某个条件,则返回与条件匹配的所有表达式的最大结果。
min(expression)
描述
如果出现某个条件,则返回与条件匹配的所有表达式的最小结果。

字符串

substring(string,from,to)
描述
返回字符串根据 from 和 输入从输入中提取。
Char_length
描述
返回字符串中的多个字符。Character_length 也实现相同的目的。
Trim
描述
从目标字符串中修剪前导或尾随字符,默认为空白字符。
Upper\lower
描述
将字符转换为大写或小写。

NULL

NULL 值缺失或未知,即 NULL 无法为任何算术操作生成一个值。这同样适用于算术比较,与 NULL 的任何比较都是 unknown 为 NULL

Expand
表 3.4. NULL 用例
A is NULLResult(NULL=UNKNOWN)

Not A

NULL

A 或 False

NULL

A or True

true

A 或 A

NULL

A 和 False

False

A 和 True

NULL

A 和 A

NULL

3.5.3. S3 别名编程结构

别名编程结构是 s3 选择语言的重要组成部分,因为它可以为包含许多列或复杂查询的对象启用更好的编程。当解析带有别名构造的语句时,它将别名替换为对右侧投射列的引用并在查询执行时,像任何其他表达式一样评估引用。别名维护结果-缓存(如果别名使用多次),则不会评估同一表达式,并返回相同的结果,因为使用了缓存的结果。目前,红帽支持列别名。

示例

select int(_1) as a1, int(_2) as a2 , (a1+a2) as a3 from s3object where a3>100 and a3<300;")
Copy to Clipboard Toggle word wrap

3.5.4. S3 CSV 解析解释

带有输入序列化的 CSV 定义使用以下默认值:

  • {\n}' 用于 row-delimiter。
  • 使用 {"} 括起内容。
  • 使用 {\} 转义字符。

在 AWS-CLI 中出现 USE 时解析 csv-header-info ;这是包含 schema 的输入对象中的第一行。目前,不支持输出序列化和压缩类型。S3 选择引擎具有 CSV 解析器,它解析 S3-objects:

  • 每行以 row-delimiter 结尾。
  • field-separator 会分离相邻的列。
  • successive 字段分隔符定义 NULL 列。
  • quote-character 覆盖了 field-separator;即,字段分隔符是引号之间的任何字符。
  • 转义字符禁用任何特殊字符,但行分隔符除外。

以下是 CSV 解析规则的示例:

Expand
表 3.5. CSV 解析
功能描述Input (Tokens)

NULL

successive 字段分隔符

,,1,,2, =⇒ {null}{null}{1}{null}{2}{null}

QUOTE

quote 字符覆盖字段分隔符。

11,22,”a,b,c,d”,last =⇒ {11}{22}{“a,b,c,d”}{last}

Escape

转义字符覆盖元字符。

对象所有者 IDDisplayName 的容器

row delimiter

没有闭引号;行分隔符是右行。

11,22,a=”str,44,55,66 =⇒ {11}{22}{a=”str,44,55,66}

csv header info

FileHeaderInfo 标签

USE 值表示第一行中的每个令牌都是列名称;IGNORE 值意味着跳过第一行。

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat