Search code examples
javascalacassandracql

How to retrieve the column having datatype as "list" from the table of Cassandra?


I am using Scala to connect with Cassandra and applying my queries in it, I have created a simple table in Cassandra which has two columns row_id and row_values. row_id has datatype as "varchar" and row_values stores the List of elements. I inserted some random values in the Table and want to retrieve these. For creating table:

CREATE TABLE Matrix1(row_id VARCHAR PRIMARY KEY, row_values LIST<VARCHAR>);

For Inserting Into the table:

INSERT INTO Matrix1(row_id, row_values) VALUES ('abcd3', ['dsf23', 'fsf1','dsdf1']);

Now I want to retrieve values and print them using Scala, I am using a code to save values from query

val results: ResultSet = session.execute("SELECT * FROM Matrix1 where row_id = 'abcd3'") 

Now I want to print the "row_id" and "row_values"

var rowid: String = null
var rowval: List[String] = null
for (row <- results) {
      rowid = row.getString("row_id")
      rowval = row.getList("row_values")
      }   

with this code I am getting the values in "rowid" because it is a String, but there is an error for "rowval". How can I retrieve the column (List type) from the ResultSet?


Solution

  • Try to add class to getList method:

    row.getList("row_values", classOf[String])