How Can I Search for Records That Have A Null/Empty Field Using CQL?

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

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

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.

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)

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


If your_column_name in your_table is a text data type then following should work,

SELECT * FROM your_table WHERE your_column_name >= '' ALLOW FILTERING;

As far as I know you cannot do this with NULL.

As an alternative, you could use a different empty value, for example the empty string: ''

In that case you could select all books with an empty author like this (assuming the author column is appropriately indexed):

SELECT * FROM book WHERE author = '';

Tags:

Cassandra

Cql