【发布时间】:2013-04-19 13:39:28
【问题描述】:
问题陈述:-
我正在尝试将数据插入 Cassandra 数据库。我为此使用Netflix/Astyanax client。
以下是将upsert 数据转换为Cassandra database 使用Astyanax client 的代码。
在我下面的方法中,它接受两个参数。一个是userId,我将使用它作为RowKey 和attributes 映射,其中将包含column name 作为键,column value 作为映射中的值。
现在我如何使用以下接受这两个参数的方法将 Astyanax client 到 upsert 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);
}
}
【问题讨论】: