java—使用astyanax客户端将数据上传到cassandra数据库

t5zmwmid  于 2021-06-14  发布在  Cassandra
关注(0)|答案(1)|浏览(494)

问题statement:-
我正试图把数据插入Cassandra数据库。我正在使用 Netflix/Astyanax client 为了这个。
下面的代码将 upsert 数据输入 Cassandra database 使用 Astyanax client .
在我下面的方法中,它接受两个参数。一个是 userId 我将用它作为 RowKey 以及 attributes 包含以下内容的Map:, column name 作为钥匙 column value 作为Map上的值。
现在我怎么用 Astyanax clientupsert data 使用下面接受这两个参数的方法输入cassandra数据库?
我主要关心的是 attributes Map。我该怎么用那张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);
    }
}
csga3l58

csga3l581#

你几乎已经给出了解决办法,但我想你还不清楚

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

相关问题