【问题标题】:Cassandra altering table to add new columns adds null as textCassandra 更改表以添加新列将 null 添加为文本
【发布时间】:2019-03-29 18:49:47
【问题描述】:

我有一个 cassandra 表,里面有数据。

我将三个新列 country 添加为 textlatlong 作为 double

添加这些列时,会针对表中已经存在的行插入空值。但是,null 将作为 text 插入国家列中,null 作为值插入 lat 和 long 列。

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

【问题讨论】:

    标签: cassandra


    【解决方案1】:

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

    但是您可以在不添加新列的情况下获得相同的行为 - 只是不要为特定的 regular 列插入值(主键列必须具有非空值!)。例如:

    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)
    

    【讨论】:

      猜你喜欢
      • 2015-09-06
      • 2016-07-07
      • 1970-01-01
      • 2018-09-08
      • 2014-10-30
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多