Cassandra:如何获取与数据类型无关的列数据的String值

3mpgtkmj  于 2022-09-27  发布在  Cassandra
关注(0)|答案(1)|浏览(212)

我正在尝试从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));
     }
}

如果数据类型不是String,e1d1e将抛出异常。如何获取字符串数据,而不管它是什么数据类型?

3ks5zfa0

3ks5zfa01#

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

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

相关问题