I have executed a query in cassandra where I have 3 columns as with 3rd column having datatype as List Result of query is in below format:
||Col 1 (varchar)||Col 2 (varchar)||Col 3 (list)||
|value 1.1|value 2.1|{"option 1":"value 1","option 2":"value 2"}
I want to fetch the value of 3 column and store in some variable
I tried .getList option but it is not working and throwing exception and hence not able to find any other approach.
Hence not able to attach the sample code.
Alex makes a good point about your data being in a List format, as opposed to a Map. That being said, I'll post code with the DataStax Java Driver v3.6 showing how to pull back both a List and a Map.
First, I created a sample table with both types of collections, and added some values:
aaron@cqlsh:stackoverflow> CREATE TABLE collections (key TEXT PRIMARY KEY,
testList LIST<TEXT>, testMap MAP<TEXT,TEXT>);
aaron@cqlsh:stackoverflow> INSERT INTO collections (key,testList,testMap)
VALUES ('a',['one','two','three'],{'option 1':'value 1','option 2':'value 2','option 3':'value 3'});
I verified it works with a simple query:
aaron@cqlsh:stackoverflow> SELECT key, testlist, testmap
FROM stackoverflow.collections WHERE key='a';
key | testlist | testmap
-----+-------------------------+-----------------------------------------------------------------------
a | ['one', 'two', 'three'] | {'option 1': 'value 1', 'option 2': 'value 2', 'option 3': 'value 3'}
(1 rows)
I built a simple app, using a custom CassandraConnection
class which handles my cluster and session objects, connects, and runs a simple session.execute(strQuery);
. Then within my Java test app, this works:
String strSELECT2 ="SELECT key, testlist, testmap "
+ "FROM stackoverflow.collections WHERE key='a'";
ResultSet rows2 = conn.query(strSELECT2);
for (Row row : rows2) {
System.out.print(
row.getString("key") + " " +
"\nList:" + row.getList("testlist", String.class).toString() + " " +
"\nMap:" + row.getMap("testmap", String.class, String.class));
}
And produces this result:
a
List:[one, two, three]
Map:{option 1=value 1, option 2=value 2, option 3=value 3}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.