我已經開始使用Cassandra database
。我計劃使用Datastax API到upsert/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完全陌生,所以在這方面有很多問題。
感謝您的幫助。
你能夠連接或它顯示任何錯誤? – abhi
是的,我也無法使用Datastax Java驅動程序連接到Cassandra數據庫。我也遇到了'NoHostAvailableException'異常。你能解決這個問題嗎? – ferhan
ohh ....我也有同樣的概率,然後發佈了一個問題cassandra論壇,是的最終能夠解決它。你正在使用哪個版本的cassandra? 1.2。? – abhi