如何编写查询以查找表中具有空/空字段的所有记录?我试过下面的查询,但没有返回任何内容。
SELECT * FROM book WHERE author = 'null';
最佳答案
除非您自己添加,否则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
因此,即使看起来有一个空值,实际值也不存在-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)
在这里,您可以清楚地看到
yves
有两列:age
和pet
,而coco
只有一列:age
。
关于cassandra - 如何使用CQL搜索具有空/空字段的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20981075/