数据类型
Python 客户端使用 PyArrow 类型进行 schema 定义:
| PyArrow 类型 | Fluss 类型 | Python 类型 |
|---|---|---|
pa.bool_() | Boolean | bool |
pa.int8() / int16() / int32() / int64() | TinyInt / SmallInt / Int / BigInt | int |
pa.float32() / float64() | Float / Double | float |
pa.string() | String | str |
pa.binary() | Bytes | bytes |
pa.binary(n) | Binary(n) | bytes |
pa.date32() | Date | datetime.date |
pa.time32("ms") | Time | datetime.time |
pa.timestamp("us") | Timestamp (NTZ) | datetime.datetime |
pa.timestamp("us", tz="UTC") | TimestampLTZ | datetime.datetime |
pa.decimal128(precision, scale) | Decimal | decimal.Decimal |
pa.list_(type) | Array | list |
pa.map_(key_type, value_type) | Map | list[(key, value)] |
pa.struct([(name, type), ...]) | Row | dict |
通过 dict 追加行时,所有 Python 原生类型(date、time、datetime、Decimal)都可使用。
可空性
构造 Fluss schema 时会保留 PyArrow 字段的可空性。默认情况下字段是可空的。在 pa.field() 上使用 nullable=False 可创建 NOT NULL 列:
schema = pa.schema([
pa.field("id", pa.int32(), nullable=False),
pa.field("name", pa.string()), # nullable by default
])
fluss_schema = fluss.Schema(schema)
fluss_schema.get_column_types() # ["int NOT NULL", "string"]无论 PyArrow 字段如何设置,主键列都会自动强制为 NOT NULL。
对于嵌套类型,元素的可空性同样会被保留:
schema = pa.schema([
pa.field("tags", pa.list_(pa.field("item", pa.string(), nullable=False))),
])
fluss_schema = fluss.Schema(schema)
fluss_schema.get_column_types() # ["array<string NOT NULL>"]写入数据
行可以是 dict、list 或 tuple:
from datetime import date, time, datetime
from decimal import Decimal
row = {
"user_id": 1,
"name": "Alice",
"active": True,
"score": 95.5,
"balance": Decimal("1234.56"),
"birth_date": date(1990, 3, 15),
"login_time": time(9, 30, 0),
"created_at": datetime(2024, 1, 1, 0, 0, 0),
"nickname": None, # null value
"tags": ["active", "premium"], # Array of strings
"scores": [10, None, 30], # Array with null values
}
handle = writer.append(row)当一行以 dict 形式写入时,可空列可以省略——它会默认为 null。不可空列(包括主键)必须存在,否则写入会被拒绝并给出明确的错误。同样的规则适用于 ROW 值的字段。
List 和 tuple 必须按列顺序取值:
row = [1, "Alice", True, 95.5, Decimal("1234.56"), date(1990, 3, 15), time(9, 30, 0), datetime(2024, 1, 1), None]
handle = writer.append(row)写入 Arrow 批次
你的批次必须包含表的列,使用表的列名,并遵循表的顺序。同样的规则适用于 ROW 列的字段。任何不符都会被拒绝,并在错误中指明列名。
列类型也必须是表的类型,因为 write_arrow_batch 会按原样写入你的 buffer,而读取方会使用表的 schema 进行解码——将 timestamp[ns] 列写入 TIMESTAMP(3) 列后,读回时会变成毫秒。仅在存储相同值的方式上存在差异的类型会为你自动转换:large_string、large_binary、large_list、view 类型,以及字典编码。任何会改变值的差异,例如不同的 TIMESTAMP 单位或 DECIMAL scale,都会被拒绝。
table.arrow_schema 是表期望的 schema。基于它构建可以完全避免转换,对 pandas 来说不会产生额外开销:
batch = pa.Table.from_pandas(df, schema=table.arrow_schema).to_batches()[0]
writer.write_arrow_batch(batch)你 schema 上的可空标记不必匹配——实际的 null 会单独与表的 NOT NULL 列进行校验。
读取数据
records = await scanner.poll(timeout_ms=1000)
for record in records:
row = record.row # dict[str, Any]
print(row["user_id"]) # int
print(row["name"]) # str
print(row["balance"]) # decimal.Decimal
print(row["birth_date"]) # datetime.date
print(row["created_at"]) # datetime.datetime
if row["nickname"] is None:
print("nickname is null")复杂类型(Array、Map、Row)
ARRAY、MAP 和 ROW 列可以任意嵌套(例如 array<map<string, row<...>>>)。读取时它们会物化为原生 Python 对象;写入时接受以下形式:
| Fluss 类型 | 读取值 | 接受的写入输入 |
|---|---|---|
ARRAY<T> | list | list / tuple |
MAP<K, V> | (key, value) tuple 的 list | dict,或 (key, value) 对的序列 |
ROW<...> | 以字段名为键的 dict | dict(按名称)或 list/tuple(按位置) |
MAP 的读取形态与 pyarrow 的 MapArray.to_pylist() 一致(它会保留重复的键和顺序);ROW 与 StructArray.to_pylist() 一致。
Array
schema = pa.schema([
pa.field("id", pa.int32()),
pa.field("tags", pa.list_(pa.string())),
pa.field("matrix", pa.list_(pa.list_(pa.int32()))), # nested
])
writer.append({"id": 1, "tags": ["a", "b"], "matrix": [[1, 2], [3, 4]]})
row = await lookuper.lookup({"id": 1})
row["tags"] # ["a", "b"]
row["matrix"] # [[1, 2], [3, 4]]Map
使用 pa.map_(key_type, value_type)。写入 dict 或 (key, value) 对的列表;读取返回 (key, value) tuple 的列表(可用 dict(...) 包装以按键访问)。Map 的 key 必须为非 null。
schema = pa.schema([
pa.field("id", pa.int32()),
pa.field("attrs", pa.map_(pa.string(), pa.int32())),
])
writer.append({"id": 1, "attrs": {"a": 1, "b": None}}) # dict input
# or a sequence of pairs: {"id": 2, "attrs": [("a", 1), ("b", None)]}
row = await lookuper.lookup({"id": 1})
row["attrs"] # [("a", 1), ("b", None)]
dict(row["attrs"]) # {"a": 1, "b": None}Row
使用 pa.struct([...])。写入以字段名为键的 dict(或按字段顺序的 list/tuple);读取返回 dict。
schema = pa.schema([
pa.field("id", pa.int32()),
pa.field("profile", pa.struct([("age", pa.int32()), ("city", pa.string())])),
])
writer.append({"id": 1, "profile": {"age": 30, "city": "NYC"}})
row = await lookuper.lookup({"id": 1})
row["profile"] # {"age": 30, "city": "NYC"}约束
ARRAY、MAP 和 ROW 可用作行值并相互嵌套,但不能作为主键列或 bucket key 列——服务端会拒绝复杂键类型。
批量(Arrow)读取
上面的逐行路径(append/upsert 和基于记录的 scanner 的 record.row dict、点查 lookup)会将每个值物化为一个 Python 对象。对于高吞吐扫描,建议使用 Arrow 路径——record-batch scanner 的 to_arrow() / poll_arrow() 会将嵌套列作为原生 pyarrow 的 ListArray / MapArray / StructArray 返回,无需逐元素转换。