简体   繁体   English

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

[英]Upsert/Read into/from Cassandra database using Datastax API (using new Binary protocol)

I have started working with Cassandra database . 我已经开始使用Cassandra database I am planning to use Datastax API to upsert/read into/from Cassandra database . 我打算使用Datastax APIupsert/read Cassandra database I am totally new to this Datastax API (which uses new Binary protocol) and I am not able to find lot of documentations as well which have some proper examples. 我对这个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'}
      ];

Now below is the Singleton class that I have created for connecting to Cassandra database using Datastax API which uses new Binary protocol- 现在下面是我使用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;
    }
}

First question- let me know if I am missing anything in the above singleton class while making connection to Cassandra database using Datastax API which uses new Binary protocol. 第一个问题 -让我知道在使用使用新二进制协议的Datastax API连接到Cassandra数据库时,我是否遗漏了上述singleton class中的任何内容。

Second question- Now I am trying to upsert and read data into/from Cassandra database- 第二个问题 -现在我正在尝试向/从Cassandra数据库中upsert and read data

These are the methods I have in my DAO's which will use the above Singleton class- 这些是我在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.

}

Can anyone help me with this? 谁能帮我这个? I am totally new to this Datastax API which is using new Binary protocol so having lot of problem on this. 我对这个使用新二进制协议的Datastax API完全不熟悉,因此在此问题上存在很多问题。

Thanks for the help. 谢谢您的帮助。

In your cassandra.yaml file look for the tag start_native_transport , by default its disabled, enable it. 在你的cassandra.yaml文件中查找标签start_native_transport ,默认情况下禁用它,启用它。

Playing with Datastax Java Driver is quite similar like jdbc driver. 使用Datastax Java Driver与jdbc驱动程序非常相似。

Insertion code 插入代码

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

Reading from Cassandra 读卡萨德拉

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Astyanax客户端将数据插入Cassandra数据库 - Upsert data into Cassandra database using Astyanax client 如何使用com.datastax.driver.mapping.annotations.Accessor从cassandra中读取字符串? - how to just read a string from cassandra using com.datastax.driver.mapping.annotations.Accessor? 使用datastax cassandra映射器的NoSuchMethodException - NoSuchMethodException using the datastax cassandra mapper 如何使用datastax cassandra-core-api将数据插入4节点cassandra集群中? - How to insert data into a 4 node cassandra cluster using datastax cassandra-core-api? 使用 datastax 收听 cassandra 数据库 - Listen to a cassandra database with datastax 无法使用Cassandra Datastax Java驱动程序连接到Cassandra节点之一 - Unable to connect to one of the Cassandra nodes using Cassandra Datastax Java Driver 使用Datastax Spark Cassandra Connector将PairDStram写入cassandra - Write PairDStram to cassandra using Datastax Spark Cassandra Connector 使用Datastax Cassandra驱动程序时重用PreparedStatement? - Re-using PreparedStatement when using Datastax Cassandra Driver? 如何使用 Spring Data Cassandra 从 cassandra 数据库读取超过百万条记录并使用 Spring Batch 将其写入文件? - How to read more than million records from cassandra Database using Spring Data Cassandra and write it into a file using Spring Batch? 将Datastax Cassandra ResultSet与Java 8并行流一起使用-快速 - Using a Datastax Cassandra ResultSet with Java 8 Parallel Streams - Quickly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM