跨引擎互操作

更新时间:
复制 MD 格式

OSS Tables 上的 Lance 表以 Lance Namespace 为统一 catalog,同一张表可被多种引擎读写。典型模式是用 Python SDK 或 Ray 跑 AI 数据管道写入 embedding 表,再用 Spark、Trino 通过 SQL 做分析。本文给出一个端到端互操作示例。

场景

  • Python SDK 写入:创建表 doc_interop.embeddings,含标量列(id / category / score)与向量列(embedding,fixed_size_list<float>[8]),写入 1000 行。

  • Spark SQL 读取:对同一张表做聚合、分组与明细查询。

  • Trino SQL 读取:对同一张表做相同查询,结果与 Spark 一致。

三个引擎共用同一个 catalog(osstables.uri 指向同一 Table Bucket),因此看到同一份数据。同一张表同样可被 Ray 追加、被 LanceDB 与 Java SDK 读取,详见文末结论。

步骤一:Python SDK 写入

通过 pylance 建表并写入含向量列的数据:

import lance, lance_namespace
import numpy as np
import osstables_lance_connector  # noqa
import pyarrow as pa
from lance_namespace import CreateNamespaceRequest, DropNamespaceRequest, DropTableRequest

ns = lance_namespace.connect("osstables", {
    "osstables.uri": "https://<tablebucket>-<uid>.cn-beijing-internal.oss-tables.aliyuncs.com/lance",
    "osstables.region": "cn-beijing",
    "osstables.access_key_id": "<控制面AccessKey ID>",
    "osstables.secret_access_key": "<控制面AccessKey Secret>",
})
STORAGE = {
    "access_key_id": "<数据面AccessKey ID>", "access_key_secret": "<数据面AccessKey Secret>",
    "endpoint": "https://oss-cn-beijing-internal.aliyuncs.com", "region": "cn-beijing",
}

DB, DIM, N = "doc_interop", 8, 1000
ns.create_namespace(CreateNamespaceRequest(id=[DB]))

rng = np.random.default_rng(42)
cats = ["cn-north", "cn-south", "cn-east", "cn-west"]
emb = rng.random((N, DIM), dtype=np.float32)
data = pa.table({
    "id": pa.array(range(N), pa.int64()),
    "category": pa.array([cats[i % 4] for i in range(N)], pa.string()),
    "score": pa.array(rng.random(N) * 100, pa.float64()),
    "embedding": pa.FixedSizeListArray.from_arrays(pa.array(emb.reshape(-1), pa.float32()), DIM),
})
lance.write_dataset(data, namespace_client=ns, table_id=[DB, "embeddings"],
                    mode="create", storage_options=STORAGE)

写入后表 schema 为:

id: int64
category: string
score: double
embedding: fixed_size_list<item: float>[8]

步骤二:Spark SQL 读取

在 Spark 中配置指向同一 Table Bucket 的 lance catalog(配置见 通过 Spark 访问 OSS Tables),即可查询:

SELECT count(*) AS cnt, round(avg(score), 3) AS avg_score, min(id) AS min_id, max(id) AS max_id
  FROM lance.doc_interop.embeddings;

SELECT category, count(*) AS c, round(sum(score), 2) AS total
  FROM lance.doc_interop.embeddings GROUP BY category ORDER BY category;

-- 向量列在 Spark 中映射为 array<float>,可用 size() 取维度
SELECT id, category, round(score, 3) AS score, size(embedding) AS dim
  FROM lance.doc_interop.embeddings WHERE id < 3 ORDER BY id;

查询输出:

cnt   avg_score  min_id  max_id
1000  48.594     0       999

category  c    total
cn-east   250  11970.12
cn-north  250  12604.70
cn-south  250  11985.57
cn-west   250  12033.88

id  category  score   dim
0   cn-north  71.610  8
1   cn-south  35.712  8
2   cn-east   85.187  8

步骤三:Trino SQL 读取

在 Trino 中配置指向同一 Table Bucket 的 lance catalog(配置见 通过 Trino 访问 OSS Tables),执行相同查询:

SELECT count(*) AS cnt, round(avg(score), 3) AS avg_score, min(id) AS min_id, max(id) AS max_id
  FROM lance.doc_interop.embeddings;

SELECT category, count(*) AS c, round(sum(score), 2) AS total
  FROM lance.doc_interop.embeddings GROUP BY category ORDER BY category;

-- 向量列在 Trino 中映射为 array(real),可用 cardinality() 取维度
SELECT id, category, round(score, 3) AS score, cardinality(embedding) AS dim
  FROM lance.doc_interop.embeddings WHERE id < 3 ORDER BY id;

查询输出与 Spark 完全一致:

 cnt  | avg_score | min_id | max_id
------+-----------+--------+--------
 1000 |    48.594 |      0 |    999

 category |  c  |  total
----------+-----+----------
 cn-east  | 250 | 11970.12
 cn-north | 250 | 12604.70
 cn-south | 250 | 11985.57
 cn-west  | 250 | 12033.88

 id | category | score  | dim
----+----------+--------+-----
  0 | cn-north | 71.610 |   8
  1 | cn-south | 35.712 |   8
  2 | cn-east  | 85.187 |   8

步骤四:清理示例资源

确认所有引擎已结束读写后,可由 Python SDK 统一清理:

ns.drop_table(DropTableRequest(id=[DB, "embeddings"]))
ns.drop_namespace(DropNamespaceRequest(id=[DB]))
重要

drop_table 会删除数据且不可恢复,请谨慎操作;如需保留底层数据、只注销 catalog 元数据,请改用 deregister_table。

结论

  • 同一张 Lance 表可被 Python SDK、Ray、LanceDB、Java SDK、Spark 和 Trino 读写,跨引擎读取结果一致。

  • 向量列(fixed_size_list<float>)在 Spark 中映射为 array<float>(用 size()),在 Trino 中映射为 array(real)(用 cardinality()),两种 SQL 引擎都能读取。

  • 各引擎只需将 catalog 指向同一 Table Bucket,即可共享数据,无需额外导出/导入。

注意事项

  • 跨引擎读写要求所有引擎的 osstables.uri 指向同一个 Table Bucket,且各自的 catalog 凭证与数据面凭证均有权限。

  • 跨引擎追加数据时须使用与表一致的列类型。

  • 若使用 STS 临时凭证,注意各引擎会话期间凭证的有效期与轮转。