7

For my Cassandra Database, I need to set a value in column for all rows in my table.

I see in SQL, we can do :

UPDATE table SET column1= XXX;

but in CQL (in cqlsh), It doesn't work ! I don't want to update row by row until 9500 rows.

Do you have any suggestion ?

Thank you :)

Quentin DESBOIS
  • 123
  • 1
  • 1
  • 9
  • 1
    There is something like static column that can be shared by many rows on the same partition. http://docs.datastax.com/en/cql/3.1/cql/cql_reference/refStaticCol.html Usable if your rows are on the same partition. – Robert Wadowski Aug 05 '15 at 14:26

3 Answers3

4

You can use update query with IN clause instead of executing 9500 query. At first select primary_key from your table and then copy values to this query:

UPDATE table SET column1 = XXX WHERE primary_key IN (p1, p2, p3, ...);
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
hamid147
  • 59
  • 1
  • 6
1

I just added a new column to a table (+60000 rows), and I looked the way to initialize all the values of the column with something (not null), and I found nothing. Is not the same asked here, but if you drop and add the column my solution will solve it. So, this is what I did:

cqlsh> COPY tablename (primary_key, newcolumn) TO 'FILE.txt'

Open FILE.TXT on notepad++ and press Ctrl+H (Replace option), and replace all the \r\n with 'something\r\n'

And finally,

cqlsh> COPY tablename (primary_key, newcolumn) FROM 'FILE.txt'

Note1: You should be carefull if you primary_key contains \r\n.

Note2: May be in your SO the lines doesn't ends with \r\n.

Eduardo Pascual Aseff
  • 1,149
  • 2
  • 13
  • 26
  • 1
    Actually, cqlsh allows exporting null values with custom string using _WITH_ keyword. ` cqlsh> COPY tablename (primary_key, newcolumn) TO 'FILE.txt WITH NULL='''` Where __ is your custom null value. – Reodont Jul 28 '20 at 06:58
0

As you are finding out, CQL != SQL. There is no way to do what you're asking in CQL, short of iterating through each row in your table.

Robert's suggestion about redefining column1 to be a static column may help. But static columns are tied to their partition key, so you would still need to specify that:

aploetz@cqlsh:stackoverflow2> UPDATE t SET s='XXX' WHERE k='k';

Also, it sounds like you only want to be able to set a column value for all rows. A static column won't work for you if you want that column value to be different for CQL rows within a partition (from the example in the DataStax docs):

aploetz@cqlsh:stackoverflow2> INSERT INTO t (k, s, i) VALUES ('k', 'I''m shared', 0);
aploetz@cqlsh:stackoverflow2> INSERT INTO t (k, s, i) VALUES ('k', 'I''m still shared', 1);
aploetz@cqlsh:stackoverflow2> SELECT * FROM t;

 k | i | s
---+---+------------------
 k | 0 | I'm still shared
 k | 1 | I'm still shared

(2 rows)

Note that the value of column s is the same across all CQL rows under partition key k. Just so you understand how that works.

Aaron
  • 55,518
  • 11
  • 116
  • 132