使用datastax api(使用新的二进制协议)向上插入/读取cassandra数据库

whhtz7ly  于 2021-06-14  发布在  Cassandra
关注(0)|答案(1)|浏览(297)

我已经开始和 Cassandra database . 我计划使用datastax api upsert/read 进/出 Cassandra database . 我对这个完全陌生 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'}
      ];

下面是 Singleton class 我为连接cassandra数据库而创建的 Datastax API 使用新的二进制协议-

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 同时使用新的二进制协议datastaxapi连接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.

}

有人能帮我吗?我是一个全新的DataStaxAPI,它使用新的二进制协议,所以在这方面有很多问题。
谢谢你的帮助。

ig9co6j1

ig9co6j11#

在cassandra.yaml文件中查找标记start\u native\u transport,默认情况下禁用它,启用它。
使用datastax java驱动程序与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"));
 }

相关问题