簡體   English   中英

Cassandra:無論數據類型如何,如何獲取列數據的字符串值

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

我正在嘗試從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,則row.getString(colIndex)引發異常。 無論字符串是什么數據類型,我如何都能得到它?

此代碼將打印每列的類型和值

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM