OSS Tables 相容 Apache Iceberg REST Catalog 協議。可以通過 Trino 的原生 S3 檔案系統(fs.native-s3)對接 OSS Tables,使用標準 SQL 查詢和寫入 Table Bucket 中的資料。該方式需要對 Trino 源碼進行少量修改以支援 oss:// 協議,適合對編譯環境有掌控的情境。
由於 Trino 社區尚未原生支援 oss:// 檔案系統協議,如需使用 Trino 訪問 OSS Tables,需要修改 Trino 源碼並重新編譯兩個模組,產生 jar 包後在 Trino 環境中替換並重啟。
步驟一:修改Trino源碼
OSS Tables 返回的資料路徑使用 oss:// 首碼,而 Trino 原生 S3 檔案系統僅識別 s3://、s3a://、s3n:// 協議。需要在以下兩個模組中註冊 oss 協議。
修改 trino-filesystem-s3
找到 S3 Location 校正邏輯,將 oss 添加到允許的 scheme 集合中:
// 修改前
checkArgument(Set.of("s3", "s3a", "s3n").contains(location.scheme().get()), "Wrong scheme for S3 location: %s", location);
// 修改後
checkArgument(Set.of("s3", "s3a", "s3n", "oss").contains(location.scheme().get()), "Wrong scheme for S3 location: %s", location);修改 trino-filesystem-manager
在檔案系統工廠綁定處添加 oss scheme 的映射:
factories.addBinding("s3").to(Key.get(TrinoFileSystemFactory.class, FileSystemS3.class));
factories.addBinding("s3a").to(Key.get(TrinoFileSystemFactory.class, FileSystemS3.class));
factories.addBinding("s3n").to(Key.get(TrinoFileSystemFactory.class, FileSystemS3.class));
// 在已有的 s3/s3a/s3n 綁定後添加:
factories.addBinding("oss").to(Key.get(TrinoFileSystemFactory.class, FileSystemS3.class));編譯並替換
編譯上述兩個模組後,將產生的 JAR 包替換 Trino 部署目錄中對應的原始 JAR,然後重啟 Trino 服務。
# 編譯 trino-filesystem-s3
mvn -pl lib/trino-filesystem-s3 -am package -DskipTests
# 編譯 trino-filesystem-manager
mvn -pl lib/trino-filesystem-manager -am package -DskipTests
# 替換 JAR(以 Trino 安裝目錄 /opt/trino 為例)
cp lib/trino-filesystem-s3/target/trino-filesystem-s3-*.jar /opt/trino/lib/
cp lib/trino-filesystem-manager/target/trino-filesystem-manager-*.jar /opt/trino/lib/步驟二:建立Table Bucket
在開始寫入資料之前,需要建立 Table Bucket 和 Namespace。可以使用 ossutil 或 AWS CLI 建立。
方式一:使用ossutil
1. 安裝或升級 ossutil
請安裝ossutil 2.3.0以上版本,如已安裝 ossutil,可執行以下命令升級到最新版本:
ossutil update -f2. 配置憑證
執行 ossutil config 命令,按提示輸入 AccessKey ID、AccessKey Secret 和 Region。
3. 建立 Table Bucket
ossutil tables-api create-table-bucket --name {table bucket名稱} --endpoint http://{endpint} --region {region}命令執行成功後,返回結果中包含 Table Bucket ARN,請記錄該值。
4. 建立 Namespace
ossutil tables-api create-namespace --table-bucket-arn {Table Bucket ARN} --namespace {Namespace名稱} --endpoint http://{endpint}Namespace 和 Table 名稱不能包含連字號(-),可使用底線(_),這是因為名稱會用於 SQL 陳述式中的標識符。
5. 建立 Table
您可以選擇以下任一方式建立 Iceberg 表:
通過其他計算引擎建立(如 Spark)。
通過 ossutil 建立:先將表 schema 儲存為 JSON 檔案,再調用
create-table。以下樣本的 schema 檔案
schema.json定義了 3 個欄位:{ "iceberg": { "schema": { "fields": [ {"name": "event_id", "type": "string", "required": true}, {"name": "event_time", "type": "string"}, {"name": "event_type", "type": "string"} ] } } }基於 schema 檔案建立 Table:
ossutil tables-api create-table --table-bucket-arn {bucketArn} --namespace {namespace名稱} --name {表名稱} --format ICEBERG --metadata file://{檔案路徑} --endpoint --endpoint http://{endpint}
方式二:使用AWS CLI
OSS Tables 相容 S3 Tables API,也可以使用 AWS CLI 管理 Table Bucket。
1. 安裝 AWS CLI
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install2. 配置憑證
執行 aws configure 命令,按提示輸入 AccessKey ID、AccessKey Secret 和 Region。
3. 建立 Table Bucket
aws s3tables --endpoint http://{endpint} create-table-bucket --region {region} --name {table bucket名稱}命令執行成功後,返回結果中包含 Table Bucket ARN。
4. 建立 Namespace
aws s3tables --endpoint http://{endpoint} create-namespace --table-bucket-arn {Table Bucket ARN} --namespace {namespace名稱}5. 建立 Table
通過其他計算引擎(如 Spark)建立表
使用 AWS CLI 建立。使用 AWS CLI 時,先將完整的入參儲存為 JSON 檔案
create-table.json,再調用create-table。{ "tableBucketARN": "{BucketArn}", "namespace": "{namespace名稱}", "name": "{表明}", "format": "ICEBERG", "metadata": { "iceberg": { "schema": { "fields": [ {"name": "event_id", "type": "string","required": true}, {"name": "event_time", "type": "string"}, {"name": "event_type", "type": "string"} ] } } } }aws s3tables --endpoint http://{endpoint} create-table --cli-input-json file://{檔案路徑}
6. 管理後台維護任務
OSS Tables 支援自動執行 Iceberg 表的後台維護(如檔案清理、檔案合并等),通過 AWS CLI 可以查詢和配置維護任務。
查詢 Table 維護任務狀態:
aws s3tables get-table-maintenance-job-status \
--table-bucket-arn="{bucketArn}" \
--namespace="{namespace名稱}" \
--name="{表名}" 配置 Bucket 級維護策略(檔案清理):
aws s3tables put-table-bucket-maintenance-configuration \
--table-bucket-arn {tableArn} \
--type icebergUnreferencedFileRemoval \
--value '{"status":"enabled","settings":{"icebergUnreferencedFileRemoval":{"unreferencedDays":4,"nonCurrentDays":10}}}' 配置 Table 級維護策略(小檔案合并):
aws s3tables put-table-maintenance-configuration \
--table-bucket-arn {bucketArn} \
--type icebergCompaction \
--namespace {namespace名稱} \
--name {表名} \
步驟三:配置Trino Catalog
OSS Tables 提供 Iceberg REST Catalog 端點,Trino 通過 Iceberg Connector 的 REST Catalog 模式串連。Endpoint格式如下:
內網:
https://{region}-internal.oss-tables.aliyuncs.com/iceberg外網:
https://{region}.oss-tables.aliyuncs.com/iceberg
OSS Tables 提供S3FileIO訪問OSS資料面使用的訪問端點,Spark 通過該端點訪問表資料。Endpoint格式如下:
內網:
https://oss-{region}-internal.aliyuncs.com外網:
https://oss-{region}.aliyuncs.com
建立 Catalog 設定檔
在 Trino 的 etc/catalog/ 目錄下建立設定檔(例如 oss_tables.properties):
connector.name=iceberg
iceberg.catalog.type=rest
iceberg.rest-catalog.uri=https://{region}-internal.oss-tables.aliyuncs.com/iceberg
iceberg.rest-catalog.warehouse=<Table Bucket ARN>
iceberg.rest-catalog.security=SIGV4
iceberg.rest-catalog.signing-name=osstables
iceberg.rest-catalog.view-endpoints-enabled=false
fs.hadoop.enabled=false
fs.native-s3.enabled=true
s3.endpoint=https://oss-{region}-internal.aliyuncs.com
s3.region=<Region>
s3.aws-access-key=<AccessKey ID>
s3.aws-secret-key=<AccessKey Secret>
s3.path-style-access=true配置參數說明
參數 | 是否必填 | 說明 |
| 是 | 固定為 |
| 是 | 固定為 |
| 是 | REST Catalog 端點 URL。格式:
|
| 是 | Table Bucket ARN。格式: |
| 是 | 固定為 |
| 是 | 固定為 |
| 是 | 固定為 |
| 是 | 固定為 |
| 是 | OSS 資料面端點。格式:
|
| 是 | 固定為 |
步驟四:使用SQL操作資料
配置完成並重啟 Trino 後,您可以使用 Trino CLI 或 JDBC 用戶端串連,通過標準 SQL 操作 OSS Tables 中的資料。
建表示例
CREATE TABLE ${catalog}.${namespace}.orders (
order_id BIGINT,
customer VARCHAR,
amount DECIMAL(10,2),
order_date DATE,
created_at TIMESTAMP(6)
)
WITH (
format = 'PARQUET',
partitioning = ARRAY['day(order_date)'],
);查詢樣本
SELECT * FROM ${catalog}.${namespace}.orders limit 10;許可權配置
使用 RAM 使用者或 STS 臨時憑證訪問 OSS Tables 時,需確保對應身份具備所需的操作許可權。
資源定義
Table Bucket ARN:
acs:osstables:<Region>:<阿里雲帳號ID>:bucket/<bucket_name>Table ARN:
acs:osstables:<Region>:<阿里雲帳號ID>:bucket/<bucket_name>/table/<table_id>
Action 定義
下表列出 OSS Tables 支援的 Action,及其是否支援跨帳號授權:
分類 | Action | 跨帳號訪問 |
Table Bucket 層級 |
| 不允許 |
| 允許 | |
| 不允許 | |
| 允許 | |
| 允許 | |
| 允許 | |
| 允許 | |
| 允許 | |
| 不允許 | |
| 不允許 | |
| 不允許 | |
| 允許 | |
| 允許 | |
| 不允許 | |
| 不允許 | |
| 不允許 | |
Table 層級 |
| 允許 |
| 允許 | |
| 不允許 | |
| 不允許 | |
| 不允許 | |
| 允許 | |
| 允許 | |
| 允許 | |
| 允許 | |
| 允許 | |
| 允許 | |
| 允許 | |
| 允許 | |
| 不允許 | |
| 不允許 | |
| 允許 |
Iceberg REST操作與許可權映射
下表列出 Iceberg REST Catalog 各操作所需的 OSS Action:
Iceberg REST 操作 | 所需 OSS Action |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|