0

将 CQL 语法与 cassandra-jdbc 驱动程序一起使用....这不起作用,只需获取空字符串:

      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);
      ...   

显然我必须每行使用 unwrap 或类似的东西......当你不知道列名时,请发布完整的例子。

4

1 回答 1

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);

请注意,为了正确显示键、列名和值,我必须定义一个列族,如下所示:

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}
     ];
于 2012-05-10T20:09:32.693 回答