如何使用 CQL 搜索具有空/空字段的记录? [英] How Can I Search for Records That Have A Null/Empty Field Using CQL?

查看:23
本文介绍了如何使用 CQL 搜索具有空/空字段的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I write a query to find all records in a table that have a null/empty field? I tried tried the query below, but it doesn't return anything.

SELECT * FROM book WHERE author = 'null';

推荐答案

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

null fields don't exist in Cassandra unless you add them yourself.

您可能会想到 CQL 数据模型,它隐藏了某些实现细节,以便拥有更易于理解的数据模型.Cassandra 是稀疏的,这意味着仅实际存储使用的数据.您可以通过 CQL 向 Cassandra 添加一些测试数据来可视化这一点.

You might be thinking of the CQL data model, which hides certain implementation details in order to have a more understandable data model. Cassandra is sparse, which means that only data that is used is actually stored. You can visualize this by adding in some test data to Cassandra through CQL.

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,因为这更直观,更有意义.

So even it appears that there is a null value, the actual value is nonexistent -- CQL is showing you a null because this makes more sense, intuitively.

如果您从 Thrift 一侧查看该表,您会发现该表不包含 coco 的年龄的值.

If you take a look at the table from the Thrift side, you can see that the table contains no such value for coco's age.

$ 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.

Here, you can clearly see that yves has two columns: age and pet, while coco only has one: age.

这篇关于如何使用 CQL 搜索具有空/空字段的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆