2012-05-08 61 views
0

使用與卡桑德拉-JDBC驅動程序的CQL語法.... 這並不工作,只是得到空字符串:如何從cassandra-jdbc ResultSet獲取列名?

 PreparedStatement pstmt = conn.prepareStatement("select * from myCF"); 
     ResultSet rset = pstmt.executeQuery(); 
     ResultSetMetaData rsmd = rset.getMetaData(); 
     int cols = rsmd.getColumnCount(); 
     pset.next(); 
     print rsmd.getColumnName(0); 
     print pset.getString(0); 
     ... 

顯然,我必須使用解包或類似的東西每行... 郵報滿例如,當你不知道列名時。

回答

1

確定這是什麼工作:

ResultSet res = pstmt.executeQuery(); 
    CassandraResultSet crs = res.unwrap(CassandraResultSet.class); 
    crs.next() ; 
    ResultSetMetaData rsmd = crs.getMetaData(); 
     cols = rsmd.getColumnCount(); 
     for(int i=1 ; i <= cols ; i++) { 
      String colNm = rsmd.getColumnName(i); 
      String colVal = null; 
      String colType = rsmd.getColumnTypeName(i); 
      if (colType.equals("JdbcLong")) { 
       colVal = "" + crs.getLong(i); 
      } else if (colType.equals("JdbcInteger")) { 
       colVal = "" + crs.getInt(i); 
      } else { 
       colVal = crs.getString(i); 

注意,得到鑰匙,COLUMNNAMES和值的正確顯示,我不得不定義象下面這樣柱族:

NOTE: you wont be able to understand your column names and values unless 
you set the Cassandra Type hints via cassandra-cli. To have a schema like below 
where all un-referenced column types are UTF8Type: 
    CREATE COLUMN FAMILY MyColFam WITH key_validation_class=UTF8Type 
    AND default_validation_class=UTF8Type AND comparator=UTF8Type 
    AND column_metadata = [ 
      {column_name: an_integer_column, validation_class: IntegerType} 
     {column_name: a_long_column,  validation_class: LongType} 
    ];