简体   繁体   English

Cassandra:无论数据类型如何,如何获取列数据的字符串值

[英]Cassandra : How to get the String value of column data irrespective of datatype

I am trying to fetch the results from a com.datastax.driver.core.ResultSet and print it: 我正在尝试从com.datastax.driver.core.ResultSet获取结果并进行打印:

ResultSet results = getSession().execute("Select * from test.table"); //getsession returns a session
int numcols = results.getColumnDefinitions().size();
for ( Row row : results ) {
    for (int colIndex = 0; colIndex < numcols; colIndex++) {
           System.out.println("data: " + row.getString(colIndex));
     }
}

row.getString(colIndex) throws an exception in case of datatypes other than String. 如果数据类型不是String,则row.getString(colIndex)引发异常。 How will I be able to get a string data irrespective of what data type it is? 无论字符串是什么数据类型,我如何都能得到它?

This code will print the type of each column and value 此代码将打印每列的类型和值

ResultSet results = getSession().execute("Select * from test.table");    
int numcols = results.getColumnDefinitions().size();
for ( Row row : results ) {
   for (int colIndex = 0; colIndex < numcols; colIndex++) {
         System.out.println("Type : "+row.getColumnDefinitions().getType(colIndex));
         Class c = row.getColumnDefinitions().getType(colIndex).asJavaClass();
         if(c == Integer.class) {
              System.out.println("Data :" + row.getInt(colIndex));
         }
         //similar for other type
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cassandra/Datastax:如何在 Java 中获取具有列表数据类型的列的值 - Cassandra/Datastax: How to get values of column having list datatype in java 不管数据类型如何,将所有元素都转换为字符串 - Convert all the elements to string irrespective of datatype 如何从Cassandra表中检索数据类型为“list”的列? - How to retrieve the column having datatype as “list” from the table of Cassandra? 如何从多个行键的cassandra中的计数器列获取值? - how to get value from counter Column in cassandra with multiple row keys? 如何将存储的 hh:mm 值更新为 mysql 数据库中的 hh:mm:ss,列数据类型为字符串? - how to update stored hh:mm value to hh:mm:ss in mysql database, the column datatype is String? 如何使用 CQL 更新 Cassandra 中的列值? - How to update a column value in Cassandra Using CQL? Cassandra:为数据类型获取等效的Java类 - Cassandra: Get equivalent Java Class for DataType 如何为Cassandra使用Java连接器以从相关列系列获取数据 - How to use Java connector for Cassandra to get data from dependent column families 无论屏幕分辨率如何,如何获取 map 的坐标 - How to get coordinates of a map irrespective of screen resolution 如何在编写之前检查 Java 字符串是否适合 Cassandra TEXT 列? - How to check if a Java String will fit into Cassandra TEXT column before writing it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM