简体   繁体   中英

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?

尝试将类添加到getList方法:

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

Quick elaboration: You specify the java class. eg,

val driverDerivedRow = MyRow(row.getString("s"), 
    row.getInt("i"), 
    row.getList("l", classOf[java.lang.Long])
    .asScala.toList.map(Long.unbox)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM