1
votes

Hi i have a columnfamily in cassandra db and when i check the contents of table it is shown differently when used as

./cqlsh -2
select * from table1;

KEY,31281881-1bef-447a-88cf-a227dae821d6 | A,0xaa| Cidr,10.10.12.0/24 | B,0xac | C,0x01 | Ip,10.10.12.1 | D,0x00000000 | E,0xace | F,0x00000000 | G,0x7375626e657431 | H,0x666230363 | I,0x00 | J,0x353839

While output is as this for

./cqlsh -3
select * from table1;

 key                                  | Cidr          | Ip
--------------------------------------+---------------+------------
 31281881-1bef-447a-88cf-a227dae821d6 | 10.10.12.0/24 | 10.10.12.1

This value is inserted using java program running.

I want to suppose update value of coulmn "B" which is only seen when using -2 option manually in database, it gives me error that it is hex value. I am using this command to update but always getting error

cqlsh:sdnctl_db> update table1 SET B='0x7375626e657431' where key='31281881-1bef-447a-88cf-a227dae821d6';

Bad Request: cannot parse '0x7375626e657431' as hex bytes

cqlsh:sdnctl_db> update table1 SET B=0x7375626e657431 where key='31281881-1bef-447a-88cf-a227dae821d6';

Bad Request: line 1:30 no viable alternative at input 'x7375626e657431'

cqlsh:sdnctl_db> update table1 SET B=7375626e657431 where key='31281881-1bef-447a-88cf-a227dae821d6';

Bad Request: line 1:37 mismatched character '6' expecting '-'

I need to insert the hex value only which will be picked by the application but not able to insert.

Kindly help me in correcting the syntax.

1

1 Answers

0
votes

It depends on what data type does column B have. Here is the reference of the CQL data types. The documentation says that blob data is represented as a hexadecimal string, so my assumption is that your column B is also a blob. From this other cassandra question (and answer) you see how to insert strings into blobs.

cqlsh:so> CREATE TABLE test (a blob, b int, PRIMARY KEY (b));
cqlsh:so> INSERT INTO test(a,b) VALUES (textAsBlob('a'), 0);
cqlsh:so> SELECT * FROM test;

b | a
---+------
0 | 0x61
cqlsh:so> UPDATE test SET a = textASBlob('b') WHERE b = 0;
cqlsh:so> SELECT * FROM test;

 b | a
---+------
 0 | 0x62

In your case, you could convert your hex string to a char string in code, and then use the textAsBlob function of cqlsh.

If the data type of column B is int instead, why don't you convert the hex number to int before executing the insert?