使用Astyanax客户端将数据上传到Cassandra数据库 [英] Upsert data into Cassandra database using Astyanax client

查看:262
本文介绍了使用Astyanax客户端将数据上传到Cassandra数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题陈述: -

我正在将数据插入Cassandra数据库。我使用 Netflix / Astyanax客户端

I am trying to insert data into Cassandra database. I am using Netflix/Astyanax client for this.

下面是数据插入 Cassandra数据库使用 Astyanax客户端

在我的下面的方法,它接受两个参数。一个是 userId ,我将使用 RowKey 属性 map,它将包含列名作为键,它的列值作为地图中的值。

In my below method, it accepts two parameter. One is the userId which I will be using as the RowKey and attributes map which will contain, column name as the key and it's column value as the value in the map.

现在如何使用 Astyanax客户端 upsert数据进入Cassandra数据库,使用接受这两个参数的以下方法?

Now how can I use Astyanax client to upsert data into Cassandra database using the below method which accepts those two parameters?

我主要关注 attributes Map。我应该如何使用该地图将 upsert 数据导入Cassandra数据库?

I am mainly concern about attributes Map. How should I use that map to upsert data into Cassandra database?

/**
 * 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);
    }
}


推荐答案

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

You have almost given the solution, but i think its not clear to you

 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支持的所有数据类型。

Basically putColumn has different overloaded variations, so it will support all the datatypes supported by cassandra.

这篇关于使用Astyanax客户端将数据上传到Cassandra数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆