How can I have null column value for a composite key column in CQL3

巧了我就是萌 提交于 2020-01-03 13:38:23

问题


This may sound silly as there are no null values in SQL's composite primary key. But just want to confirm if we can have the same in CQL3?
So, we have a table like this to store wide rows:

CREATE TABLE keyspace12.colFamily1 
(
  id text, 
  colname text,
  colvalue blob,
  PRIMARY KEY (id,colname, colvalue)
) WITH COMPACT STORAGE

And we have some cases where colname is null. Can I do that? If yes, then how? If NO, then what are the ways to store wide columns rows where we can have some null in first part of composite column of cassandra(As per Thrift's convention)?

The related questions are: CQL3 and millions of columns composite key use case and Cassandra -How to create composite column name (not key) using cqlsh


回答1:


Say i have a column family

CREATE TABLE cnt_test (time_slot text , comp1 text, comp2 text, field1 counter, field2 counter, PRIMARY KEY(time_slot,comp1, comp2));

Now i am trying to insert some data in it

UPDATE cnt_test SET field1=field1+1, field2=field2+2 WHERE time_slot='20130924' AND comp1='' AND comp2='ABC';
UPDATE cnt_test SET field1=field1+1, field2=field2+2 WHERE time_slot='20130924' AND comp1='' AND comp2='ABC';
UPDATE cnt_test SET field1=field1+1, field2=field2+2 WHERE time_slot='20130924' AND comp1='' AND comp2='ABC';
UPDATE cnt_test SET field1=field1+1, field2=field2+2 WHERE time_slot='20130924' AND comp1='' AND comp2='XYZ';
UPDATE cnt_test SET field1=field1+1, field2=field2+2 WHERE time_slot='20130924' AND comp1='PQR' AND comp2='';

As you can see in the above statements i have inserted some empty values in the compound key part, just instead of null i am putting the blank character.

I can even query on the same

 SELECT * FROM cnt_test WHERE time_slot='20130924' AND comp1='' AND comp2='ABC';

 time_slot | comp1 | comp2 | field1 | field2
-----------+-------+-------+--------+--------
  20130924 |       |   ABC |      4 |      8

(1 rows)

SELECT * FROM cnt_test WHERE time_slot='20130924' AND comp1='PQR' AND comp2='';

 time_slot | comp1 | comp2 | field1 | field2
-----------+-------+-------+--------+--------
  20130924 |   PQR |       |      1 |      2

(1 rows)

So to summarize every thing just replace the null column value with empty column i.e ''




回答2:


And we have some cases where colname is null. Can I do that?

Response is no, as expected

If NO, then what are the ways to store wide columns rows where we can have some null in first part of composite column of cassandra(As per Thrift's convention)?

You can do in in Thrift with cassandra-cli. It's not possible with CQL3 to set a primary key component to null




回答3:


I needed to do the same in order to model parent/child relationships in a single table. So when I want to insert a parent then 'id' is populated and your 'colname' would be set to nil.

To do this the solution was really simple literally insert the 'colname' as empty string. I can see my select return null returned.



来源:https://stackoverflow.com/questions/18963248/how-can-i-have-null-column-value-for-a-composite-key-column-in-cql3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!