【问题标题】:Upsert data into Cassandra database using Astyanax client使用 Astyanax 客户端将数据插入 Cassandra 数据库
【发布时间】:2013-04-19 13:39:28
【问题描述】:

问题陈述:-

我正在尝试将数据插入 Cassandra 数据库。我为此使用Netflix/Astyanax client

以下是将upsert 数据转换为Cassandra database 使用Astyanax client 的代码。

在我下面的方法中,它接受两个参数。一个是userId,我将使用它作为RowKeyattributes 映射,其中将包含column name 作为键,column value 作为映射中的值。

现在我如何使用以下接受这两个参数的方法将 Astyanax clientupsert data 用于 Cassandra 数据库?

我主要关心attributes Map。我应该如何使用该映射将 upsert 数据导入 Cassandra 数据库?

/**
 * Performs an upsert of the specified attributes for the specified id.
 */
public void upsertAttributes(final String userId, final Map<String, String> attributes) {

    MutationBatch m = CassandraAstyanaxConnection.getInstance().getKeyspace().prepareMutationBatch();

    m.withRow(CassandraAstyanaxConnection.getInstance().getEmp_cf(), userId)
    .putColumn(attributeName from attributesMap, attributeValue from attributesMap, null)
    ;

    try {
        m.execute();
    } catch (ConnectionException e) {
        StringBuilder message = new StringBuilder();
        message.append("Failed to read from C* = '").append(e).append("'. ");

        LOG.error("Failed to write data to C*", e);

        throw new RuntimeException("failed to write data to C*", e);
    }
}

【问题讨论】:

    标签: java cassandra astyanax


    【解决方案1】:

    你几乎给出了解决方案,但我认为你不清楚

     MutationBatch m = CassandraAstyanaxConnection.getInstance().getKeyspace().prepareMutationBatch();
     ColumnListMutation<String> clm = m.withRow(CassandraAstyanaxConnection.getInstance().getEmp_cf(), userId);
     for(String key: attributeMap.keySet()){
         clm.putColumn(key, attributeMap.get(key), null);
     }
    
    try {
        m.execute();
    } catch (ConnectionException e) {
        e.printStackTrace();
    }
    

    基本上 putColumn 有不同的重载变体,因此它将支持 cassandra 支持的所有数据类型。

    【讨论】:

    • 谢谢阿比。那件事,我已经知道您在上面发布的任何代码。我的主要挑战是从attributes Mapcol1 value 从同一个attributesMap 中获取col1。如果你再看一遍我的问题,你就会明白我想说什么。
    • ,基本上在我的属性Map中,我会有列名和对应的列值。所以我需要从这些映射中获取列名和列值。
    • 谢谢。有效。我猜,你是这里唯一的 Cassandra 人。 :)。我还有两个与 Cassandra 相关的问题。还没有人回答这个问题。这两个与Datastax API(新二进制协议)postpost有关。如果你有时间,也可以看看。非常感谢您的帮助。
    猜你喜欢
    • 2013-03-29
    • 2013-05-06
    • 2017-11-01
    • 2013-12-10
    • 2013-08-27
    • 1970-01-01
    • 2013-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多