c++ - 使用 CQL 准备的查询在 C++ 中管理计数器

标签 c++ cassandra cql

重新发布实际代码

    // cassandra is a connected org::apache::cassandra::CassandraClient
// statement is a CqlPreparedResult
// result is a CqlResult
// CF declaration is:
/*
CREATE COLUMNFAMILY xp (
  cid ascii PRIMARY KEY,
  exp4 counter,
  exp2 counter,
  exp3 counter,
  exp1 counter
) WITH
  comment='' AND
  comparator=text AND
  read_repair_chance=0.100000 AND
  gc_grace_seconds=864000 AND
  default_validation=counter AND
  min_compaction_threshold=4 AND
  max_compaction_threshold=32 AND
  replicate_on_write=True AND
  compaction_strategy_class='SizeTieredCompactionStrategy' AND
  compression_parameters:sstable_compression='org.apache.cassandra.io.compress.SnappyCompressor';
 */
std::vector<std::string> values;
values.resize(2);
values[0]="1";
values[1]="103";
cassandra->prepare_cql_query(statement,"update xp set exp2 = exp2 + ? where cid=?",Compression::NONE);
int32_t handle=statement.itemId;
try {
    cassandra->execute_prepared_cql_query(result,handle,values);
}
catch(InvalidRequestException &e) {
    cout << __LINE__ << " " << e.why << " "<< e.what() << endl;
}
// throws '?' is an invalid value, should be a long.
values.resize(1);
values[0]="103";
cassandra->prepare_cql_query(statement,"update xp set exp2 = exp2 + 1 where cid=?",Compression::NONE);
handle=statement.itemId;
cassandra->execute_prepared_cql_query(result,handle,values);
//works

看起来问题在于我们无法传递二进制数据。 我试图传入一个包含 long 的实际表示的字符串,但没有成功。 当我从中读取数据时出现同样的问题,但我认为我可以管理它(扫描返回的字符串并将其一次转换一个字节作为最后的手段)

有没有人成功使用过计数器、c++ 和 cql?

最佳答案

long 值应编码为 big-endian 64 位整数。所以尝试:

values[0] = "\0\0\0\0\0\0\0\x64";

如果您打算在没有驱动程序的情况下继续使用 CQL,您将需要大量的编码/解码例程来处理这类事情。

关于c++ - 使用 CQL 准备的查询在 C++ 中管理计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10609272/

相关文章:

c++ - 加入 Portaudio 和 Opus

cassandra - 如何在 cqlsh 中获得一毫秒差异的结果

"select * from CF where column in (..,..,..)"的 python CQL 查询替换

cassandra - 如何在 CQL 3 中仅返回一些映射键(又名,切片一系列映射/集合元素)?

database - Cassandra:如何在没有EQ或IN限制的PRIMARY KEY的情况下使用 'ORDER BY'?

c++ - 迭代时从 vector 和内存中删除一个对象

c++ - linux 上的 c++ 交叉编译到 windows

c++ - ComboBox 子类列表框

cassandra - 如何在使用命令行时使用 CQL 获取当前时间戳?

cassandra - Cassandra 中没有回滚,那么 Cassandra 是如何删除失败的写入的呢?