小编典典

使用Datastax API(使用新的二进制协议)向上/从Cassandra数据库中读取数据

java

我已经开始与Cassandra database。我打算用Datastax
API
upsert/read进/出Cassandra database。我对此完全陌生Datastax API(它使用新的Binary协议),而且我也找不到很多具有适当示例的文档。

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

现在,下面是Singleton class我创建的用于连接到Cassandra数据库的数据库,Datastax API该数据库使用了新的Binary协议-

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

第一个问题- 让我知道在singleton class使用使用新的二进制协议的Datastax
API建立与Cassandra数据库的连接时,是否缺少上述内容。

第二个问题- 现在我正在尝试upsert and read data进入/离开Cassandra数据库-

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

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.

}

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

谢谢您的帮助。


阅读 195

收藏
2020-11-30

共1个答案

小编典典

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

使用Datastax Java驱动程序与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"));
 }
2020-11-30