Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Astyanax客户端将数据上传到Cassandra数据库_Java_Cassandra_Astyanax - Fatal编程技术网

Java 使用Astyanax客户端将数据上传到Cassandra数据库

Java 使用Astyanax客户端将数据上传到Cassandra数据库,java,cassandra,astyanax,Java,Cassandra,Astyanax,问题陈述:- 我正在尝试将数据插入Cassandra数据库。为此,我正在使用Netflix/Astyanax客户端 下面是使用Astyanax客户端将数据插入Cassandra数据库的代码 在我下面的方法中,它接受两个参数。一个是userId,我将使用它作为RowKey和attributes映射,它将包含,column name作为键,它的column value作为映射中的值 现在,我如何使用Astyanax客户端使用下面接受这两个参数的方法将数据上传到Cassandra数据库中 我主要关注属

问题陈述:-

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

下面是使用
Astyanax客户端
将数据插入
Cassandra数据库
的代码

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

现在,我如何使用
Astyanax客户端
使用下面接受这两个参数的方法将数据
上传到Cassandra数据库中

我主要关注
属性
映射。我应该如何使用该映射将数据插入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);
    }
}
/**
*为指定id执行指定属性的upsert。
*/
public void upserttributes(最终字符串userId、最终映射属性){
MutationBatch m=CassandraAstyanaxConnection.getInstance().getKeyspace().prepareMutationBatch();
m、 withRow(CassandraAstyanaxConnection.getInstance().getEmp_cf(),userId)
.putColumn(AttributeMap中的attributeName,AttributeMap中的attributeValue,null)
;
试一试{
m、 执行();
}捕获(连接异常e){
StringBuilder消息=新建StringBuilder();
message.append(“未能从C*=”)读取;
日志错误(“未能将数据写入C*”,e);
抛出新的运行时异常(“未能将数据写入C*”,e);
}
}

您几乎给出了解决方案,但我想您还不清楚

 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();
}
MutationBatch m=CassandraAstyanaxConnection.getInstance().getKeyspace().prepareMutationBatch();
columnlistclm=m.withRow(cassandraastyanxconnection.getInstance().getEmp_cf(),userId);
for(字符串键:attributeMap.keySet()){
clm.putColumn(key,attributeMap.get(key),null);
}
试一试{
m、 执行();
}捕获(连接异常e){
e、 printStackTrace();
}

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

谢谢你,阿比。那件事,我已经知道你上面发布的代码了。我的主要挑战是从
属性映射中获取
col1
,并从相同的
属性映射中获取
col1值。如果你再看一遍我的问题,你会看到我想说的。基本上在我的属性映射中,我会有列名和相应的列值。所以我需要从这些映射中获取列名和列值。谢谢。成功了。我想,你是这里唯一的卡桑德拉人。我还有两个关于卡桑德拉公开赛的问题。现在还没有人回答这个问题。这两个与Datastax API(新二进制协议)和。如果你有时间,也可以考虑一下。非常感谢你的帮助。