如何使用CQL搜索具有空/空字段的记录?

Hap*_*r86 15 cql cassandra

如何编写查询以查找表中具有空/空字段的所有记录?我尝试过尝试下面的查询,但它没有返回任何内容.

SELECT * FROM book WHERE author = 'null';
Run Code Online (Sandbox Code Playgroud)

小智 29

null 除非您自己添加字段,否则Cassandra中不存在字段.

您可能正在考虑CQL数据模型,它隐藏了某些实现细节,以便拥有更易理解的数据模型.Cassandra是稀疏的,这意味着实际上只存储了所使用的数据.您可以通过CQL向Cassandra添加一些测试数据来实现这一点.

cqlsh> CREATE KEYSPACE test WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1 } ;
cqlsh> use test ;
cqlsh:test> CREATE TABLE foo (name text, age int, pet text, primary key (name)) ;
cqlsh:test> insert into foo (name, age, pet) values ('yves', 81, 'german shepherd') ;
cqlsh:test> insert into foo (name, pet) values ('coco', 'ferret') ;

cqlsh:test> SELECT * FROM foo ;

name | age  | pet
-----+-----+------------------
coco | null | ferret
yves |  81  | german shepherd
Run Code Online (Sandbox Code Playgroud)

因此,即使看起来存在空值,实际值也不存在 - CQL向您显示,null因为这更直观,更直观.

如果你看一下Thrift方面的表格,你可以看到该表格中没有包含coco年龄的值.

$ bin/cassandra-cli
[default@unknown] use test;
[default@test] list foo;
RowKey: coco
=> (name=, value=, timestamp=1389137986090000)
=> (name=age, value=00000083, timestamp=1389137986090000)
-------------------
RowKey: yves
=> (name=, value=, timestamp=1389137973402000)
=> (name=age, value=00000051, timestamp=1389137973402000)
=> (name=pet, value=6765726d616e207368657068657264, timestamp=1389137973402000)
Run Code Online (Sandbox Code Playgroud)

在这里,您可以清楚地看到它yves有两列:age而且pet,coco只有一列:age.


Ike*_*ker 5

据我所知,你不能用NULL.

作为替代方案,您可以使用不同的空值,例如空字符串: ''

在这种情况下,您可以像这样选择所有作者为空的书籍(假设作者列已正确编入索引):

SELECT * FROM book WHERE author = '';
Run Code Online (Sandbox Code Playgroud)