UDT 的字段在表system_schema.types 中描述。当您添加一个新字段时,该类型的条目会在 Cassandra 中更新,但不会发生磁盘上的数据更改(SSTables 是不可变的)。相反,当 Cassandra 读取数据时,它会检查字段是否存在,如果不存在(因为它没有设置,或者它是 UDT 的新字段),那么它将为该值返回 null,但不修改磁盘上的数据。
例如,如果我有以下类型和使用它的表:
CREATE TYPE test.udt (
id int,
t1 int
);
CREATE TABLE test.u2 (
id int PRIMARY KEY,
u udt
)
我在表中有一些数据,所以我得到:
cqlsh> select * from test.u2; id | u ----+---------------- 5 | {id: 1, t1: 3}
如果我使用 alter type test.udt add t2 int; 将字段添加到 UDT,我会立即看到 null 作为新 UDT 字段的值:
cqlsh> select * from test.u2;
id | u
----+--------------------------
5 | {id: 1, t1: 3, t2: null}
如果我在 SSTable 上执行sstabledump,我可以看到它只包含旧数据:
[
{
"partition" : {
"key" : [ "5" ],
"position" : 0
},
"rows" : [
{
"type" : "row",
"position" : 46,
"liveness_info" : { "tstamp" : "2019-07-28T09:33:12.019Z" },
"cells" : [
{ "name" : "u", "path" : [ "id" ], "value" : 1 },
{ "name" : "u", "path" : [ "t1" ], "value" : 3 }
]
}
]
}
]
另见my answer about adding/removing columns