微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用Datastax API使用新的二进制协议向上/从Cassandra数据库中读取数据

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

使用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"));
 }

解决方法

我已经开始与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完全陌生,因此存在很多问题。

谢谢您的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。