2012-02-24 68 views
1

Im用hector插入數據並用JDBC恢復數據。將cassandra和JDBC連接起來得到奇數十六進制

的數據是沒有SENCE除了索引列,即時通訊意識到,這是因爲卡桑德拉存儲數據的字節,然後將其轉換時,其讀取(客戶端將其轉換)

有誰知道如何做到這一點JDBC for cassandra?。

我的代碼:

public static void main(String[] args) throws SQLException 
    { 
     Connection con = null; 
     String _KS = "StringMsg"; 
     String _CFDef = "Tweets"; 

     try 
     { 
      Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver"); 
      con = DriverManager.getConnection("jdbc:cassandra://localhost:9160/" + _KS); 

      String query = "SELECT username, email, msg, id FROM " + _CFDef + " WHERE 'username'='LTodorov'"; 

      Statement stmt = con.createStatement(); 
      ResultSet result = stmt.executeQuery(query); 

      System.out.println("hi"); 
      int i=0; 
      while (result.next()) 
      { 
       System.out.println(result.getString("id")); 
       System.out.println(result.getString("username")); 
       System.out.println(result.getString("msg")); 
       System.out.println(result.getString("email")); 
       System.out.print("count:" + i + "\n\n");i++; 
      } 
     } 
     catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 
     catch (SQLException e) { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      if (con != null) 
      { 
       try 
       { 
        con.close(); 
       } 
       catch (SQLException e) 
       { 
        e.printStackTrace(); 
       } 
       con = null; 
      } 
     } 
    } 

結果:

hi 
java.nio.HeapByteBuffer[pos=237 lim=258 cap=798] 
LTodorov 
java.nio.HeapByteBuffer[pos=184 lim=209 cap=798] 
java.nio.HeapByteBuffer[pos=144 lim=155 cap=798] 
count:0 

Cli時結果:

GET Tweets Where username=LTodorov; 
------------------- 
RowKey: LTodorov1330045080096 
=> (column=email, value=6c5f6c74404162762e6267, timestamp=1330045080214000) 
=> (column=id, value=4c546f646f726f7631333330303435303830303936, timestamp=13300 
45080098000) 
=> (column=msg, value=54686973206973206d616b696e67206d652073616466616365, timest 
amp=1330045080210000) 
=> (column=username, value=LTodorov, timestamp=1330045080217000) 

Cli時結果與 「假設鳴叫驗證作爲UTF8」;

RowKey: LTodorov1330045080096 
=> (column=email, [email protected], timestamp=1330045080214000) 
=> (column=id, value=LTodorov1330045080096, timestamp=1330045080098000) 
=> (column=msg, value=This is making me sadface, timestamp=1330045080210000) 
=> (column=username, value=LTodorov, timestamp=1330045080217000) 

柱族定義:

CREATE COLUMN FAMILY Tweets 
WITH comparator = UTF8Type 
AND key_validation_class=UTF8Type 
AND column_metadata = [ 
{column_name: id, validation_class: UTF8Type} 
{column_name: email, validation_class: UTF8Type} 
{column_name: msg, validation_class: UTF8Type} 
{column_name: user, validation_class: UTF8Type} 

任何卡桑德拉大師有任何指針?我正在獲取數據,但是應該在java中進行轉換,還是應該使用JDBC api來完成這項工作,還是應該成爲cassandra的東西? (我認爲這不能由卡桑德拉解決)

回答

2

我認爲你應該能夠在你的java類中做到這一點。結果集返回一些數據類型,查看方法getBytes()。您可以將字節值重建爲String。例如

String string_username = new String(result.getBytes("username"), "UTF-8"); 
System.out.println(string_username); 
+0

你的意思是錯誤處理?有很多方法可以考慮,例如,將它記錄爲日誌記錄中的錯誤,無論哪個類調用此類方法,並且如果出現此錯誤,他們必須進行其他必要的更改,或者可能有一個小的try-catch塊圍繞上面的兩條語句,嘗試使用其他字符集進行解碼或從結果中調用其他方法。但我認爲你有很好的做法,也就是說,不要留下錯誤處理,而是首先定義列元數據來預防。那很好! :-) – Jasonw 2012-02-24 09:56:39

相關問題