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

标签 cassandra cql

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

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有两列:agepet,而coco只有一列:age

关于cassandra - 如何使用CQL搜索具有空/空字段的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20981075/

相关文章:

cassandra - CQL 检查记录是否存在

Cassandra - 新 ColumnFamily 创建时出现 NullPointerException

python - 使用 cqlengine 在 cassandra 中插入和更新大量行的最快和最有效的方法

python - 使用 shell 和 python 驱动程序导入 Cassandra 数据的时间

cassandra - 基于 token 范围的分区键查询的性能?

cassandra - 如何为 murmur3 分区器生成多数据中心 token

cassandra - Cassandra世界里的只读副本相当于什么?

java - Cassandra-Java-driver : com. datastax.driver.core.exceptions.InvalidTypeException : Invalid type, 列是一个列表,但提供了类 java.lang.String

Cassandra Upsert 不适用于每列

cassandra - Apache Nifi/Cassandra - 如何将 CSV 加载到 Cassandra 表中