使用Datastax API(使用新的二进制协议)升级/读入/读取Cassandra数据库

我已经开始使用Cassandra database 。 我打算使用Datastax API来upsert/read Cassandra database 。 我对这个Datastax API (使用新的二进制协议)完全不Datastax API ,我也找不到很多具有一些适当示例的文档。

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

第一个问题 –让我知道在使用使用新二进制协议的Datastax API连接到Cassandra数据库时,我是否遗漏了上述singleton class中的任何内容。

第二个问题 –现在我试图将upsert and read data插入/ upsert and read data Cassandra数据库 –

这些是我在DAO中使用的方法,它将使用上述Singleton类 –

 public Map getColumnNames(final String userId, final Collection 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 attributes = new ConcurrentHashMap(); 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 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. } 

谁能帮我这个? 我对这个使用新二进制协议的Datastax API完全不熟悉,因此在此问题上存在很多问题。

谢谢您的帮助。

在你的cassandra.yaml文件中查找标签start_native_transport ,默认情况下禁用它,启用它。

使用Datastax Java Driver非常类似于jdbc驱动程序。

插入代码

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

从Cassandra读书

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