Cassandra更改表以添加新列将null作为文本添加

时间:2019-03-29 07:55:05

标签: cassandra

我有一个带有数据的cassandra表。

我添加三个新列accountId作为countrytextlat作为long

添加这些列后,将针对表中已存在的行插入空值。但是,在“国家/地区”列中将double插入为null,在“经度”和“长”列中将text作为值插入。

enter image description here

enter image description here

这是默认行为吗,我可以在新创建的文本列下添加null作为值吗?

1 个答案:

答案 0 :(得分:2)

Cassandra使用null表示缺少值,而不是明确插入该值。在您的情况下,当您添加新列时-它们仅被添加到存储在Cassandra中的表规范中-现有数据(存储在SSTables中)不会被修改,因此当Cassandra读取旧数据时,它不会在SSTable中找到该列的值,而是输出null。

但是您可以具有相同的行为而无需添加新列-只是不要为特定的常规列插入值(主键列必须具有非空值!)。例如:

cqlsh> create table test.abc (id int primary key, t1 text, t2 text);
cqlsh> insert into test.abc (id, t1, t2) values (1, 't1-1', 't2-1');
cqlsh> insert into test.abc (id, t1) values (2, 't1-2');
cqlsh> insert into test.abc (id, t2) values (3, 't3-3');
cqlsh> SELECT * from test.abc;

 id | t1   | t2
----+------+------
  1 | t1-1 | t2-1
  2 | t1-2 | null
  3 | null | t3-3

(3 rows)
相关问题