【问题标题】:Upsert/Read into/from Cassandra database using Datastax API (using new Binary protocol)使用 Datastax API(使用新的二进制协议)Upsert/Read into/Read into/from Cassandra database
【发布时间】:2013-04-12 08:24:24
【问题描述】:

我已经开始使用Cassandra database。我计划使用Datastax APIupsert/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,它使用Datastax API 连接到Cassandra 数据库,它使用新的二进制协议-

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 进入/退出 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 完全陌生,所以对此有很多问题。

感谢您的帮助。

【问题讨论】:

  • 您可以连接还是显示任何错误?
  • 是的,我也无法使用 Datastax Java 驱动程序连接到 Cassandra 数据库。我也遇到了与NoHostAvailableException 相同的异常。你能解决这个问题吗?
  • 哦....我也有同样的问题,然后在 cassandra-forum 上发布了一个问题,是的,最终能够解决它。您使用的是哪个版本的 cassandra? 1.2.?
  • 我正在运行 1.2.3。我还向您发送了一封电子邮件,我正在尝试通过该电子邮件建立连接并连接到集群。
  • 顺便问一下,您在 cassandra.yaml 文件中做了哪些更改?我也会做出改变并运行它,看看这是否会产生任何影响。

标签: java cassandra cql cql3 datastax-java-driver


【解决方案1】:

在您的 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"));
 } 

【讨论】:

  • 我已经将它从 false 变为 true。无论如何,我会努力让这件事发挥作用。您知道如何使用 Datastax Java 驱动程序插入 Cassandra,然后检索它吗?意思是我最初的问题。
  • 修改yaml文件后重启服务器了吗?尝试清理提交日志文件并启动服务器。回答原始问题,肯定会尝试提供
  • 你能告诉我如何清理提交日志文件吗?意思是我们一般应该怎么做?
  • 默认存储在 '\var\lib\cassandra\commitlog\' 中。清理 commitlog 文件夹中的所有文件(使用 rm -fr)
  • 好的,就是把它删掉。我在 Windows 中工作。我也删除了它,然后重新启动服务器,仍然是同样的事情。我相信我尝试连接的方式不正确。您能否提供一个您也尝试连接到 Cassandra 数据库的示例?含义连接代码。
猜你喜欢
  • 2013-04-12
  • 2020-07-13
  • 2010-10-19
  • 1970-01-01
  • 1970-01-01
  • 2013-06-19
  • 1970-01-01
  • 2014-03-16
  • 1970-01-01
相关资源
最近更新 更多