2013-04-19 17 views
0

我已經開始使用Cassandra database。我計劃使用Datastax APIupsert/read進入/從Cassandra database。我對這個Datastax API(它使用新的Binary協議)完全陌生,而且我也無法找到很多文檔以及具有一些正確示例的文檔。使用Datastax API(使用新的Binary協議)插入/從Cassandra數據庫讀入/插入

create column family profile 
    with key_validation_class = 'UTF8Type' 
    and comparator = 'UTF8Type' 
    and default_validation_class = 'UTF8Type' 
    and column_metadata = [ 
     {column_name : crd, validation_class : 'DateType'} 
     {column_name : lmd, validation_class : 'DateType'} 
     {column_name : account, validation_class : 'UTF8Type'} 
     {column_name : advertising, validation_class : 'UTF8Type'} 
     {column_name : behavior, validation_class : 'UTF8Type'} 
     {column_name : info, validation_class : 'UTF8Type'} 
     ]; 

現在下面是我使用Datastax API它使用新的二進制協議 - 連接到Cassandra的數據庫中創建的Singleton class

public class CassandraDatastaxConnection { 

    private static CassandraDatastaxConnection _instance; 
    protected static Cluster cluster; 
    protected static Session session; 


    public static synchronized CassandraDatastaxConnection getInstance() { 
     if (_instance == null) { 
      _instance = new CassandraDatastaxConnection(); 
     } 
     return _instance; 
    } 

    /** 
    * Creating Cassandra connection using Datastax API 
    * 
    */ 
    private CassandraDatastaxConnection() { 

     try{ 
      cluster = Cluster.builder().addContactPoint("localhost").build(); 
      session = cluster.connect("my_keyspace");   
     } catch (NoHostAvailableException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    public static Cluster getCluster() { 
     return cluster; 
    } 

    public static Session getSession() { 
     return session; 
    } 
} 

首先question-讓我知道如果我在缺少什麼以上singleton class使用使用新Binary協議的Datastax API連接到Cassandra數據庫。

二question-現在我想upsert and read data進/出卡桑德拉數據庫 -

這是我的方法,我的DAO的將使用上述辛格爾頓接收機類

public Map<String, String> getColumnNames(final String userId, final Collection<String> columnNames) { 

    //I am not sure what I am supposed to do here? 
    //Given a userId, I need to retrieve those columnNames from the Cassandra database 
    //And then put it in the map with column name and its value and then finally return the map 

    Map<String, String> attributes = new ConcurrentHashMap<String, String>(); 

    for(String col : columnNames) { 
     attributes.put(col, colValue); 
    } 

    return attributes; 
} 


/** 
* Performs an upsert of the specified attributes for the specified id. 
*/ 
public void upsertAttributes(final String userId, final Map<String, String> columnNameAndValue) { 

    //I am not sure what I am supposed to do here to upsert the data in Cassandra database. 
    //Given a userId, I need to upsert the columns values into Cassandra database. 
    //columnNameAndValue is the map which will have column name as the key and corresponding column value as the value. 

} 

燦任何人都可以幫助我?我對這個使用新Binary協議的Datastax API完全陌生,所以在這方面有很多問題。

感謝您的幫助。

+0

你能夠連接或它顯示任何錯誤? – abhi

+0

是的,我也無法使用Datastax Java驅動程序連接到Cassandra數據庫。我也遇到了'NoHostAvailableException'異常。你能解決這個問題嗎? – ferhan

+0

ohh ....我也有同樣的概率,然後發佈了一個問題cassandra論壇,是的最終能夠解決它。你正在使用哪個版本的cassandra? 1.2。? – abhi

回答

2

在您的cassandra.yaml文件中,查找標記start_native_transport,默認情況下禁用它,啓用它。

使用Datastax Java Driver與jdbc驅動程序非常相似。

插入代碼

String query = "insert into test(key,col1,col2) values('1','value1','value2')"; 
session.execute(query); 

從卡桑德拉

String query="select * from test;"; 
ResultSet result = session.execute(query); 
for (Row rows: result){ 
    System.out.println(rows.getString("key")); 
} 
+0

我已經啓用了從false到true。無論如何,我會盡力讓這件事情起作用。你知道我如何使用Datastax Java驅動程序插入Cassandra,然後檢索它?意思是我最初的問題。 – ferhan

+0

更改yaml文件後,你有沒有重新啓動服務器?嘗試清理commitlog文件並啓動服務器。回答原始問題,肯定會嘗試提供 – abhi

+0

你還可以告訴我如何清理提交日誌文件嗎?一般來說,我們應該怎樣做? – ferhan