【问题标题】:Cassandra : How to get the String value of column data irrespective of datatypeCassandra:无论数据类型如何,如何获取列数据的字符串值
【发布时间】:2016-02-27 06:44:09
【问题描述】:

我正在尝试从 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) 在 String 以外的数据类型的情况下抛出异常。无论它是什么数据类型,我将如何获取字符串数据?

【问题讨论】:

  • 是否有可以返回列类型的元数据检索调用?如果不是,你可能不走运,但如果有,你可以编写代码来处理所有的可能性。

标签: java cassandra-2.0 datastax-java-driver


【解决方案1】:

此代码将打印每列的类型和值

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
   }
}

【讨论】:

    猜你喜欢
    • 2022-01-10
    • 2015-01-24
    • 2020-12-24
    • 2019-07-03
    • 2021-07-02
    • 2019-11-18
    • 2022-08-23
    • 1970-01-01
    • 2021-05-24
    相关资源
    最近更新 更多