gpt4 book ai didi

c++ - 使用 Thrift 和 Cassandra 编译 C++ 程序

转载 作者:搜寻专家 更新时间:2023-10-31 01:55:08 24 4
gpt4 key购买 nike

我正在尝试编译一个简单的示例,以使用 thrift 接口(interface)连接到 cassandra 实例。请注意,我在没有访问 linux 机器上的 super 用户权限的情况下执行所有这些操作。

我安装了 thrift 和 c++ 生成器,将包含 header 放在我的 CPLUS_INCLUDE_PATH 变量上,将 lib 目录放在我的 LIBRARY_PATH 和 LD_LIBRARY_PATH 上。 Cassandra 也已安装,我运行 thrift --gen cpp cassandra.thrift 来生成 cassandra 头文件。

使用这些我像这样编译了我的示例
g++ -Wall run_measure.cpp cassandra_constants.cpp Cassandra.cpp cassandra_types.cpp -lthrift -o cassandra_example

程序大致是这样的

#include "Cassandra.h"

#include <protocol/TBinaryProtocol.h>
#include <transport/TSocket.h>
#include <transport/TTransportUtils.h>

using namespace std;
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace org::apache::cassandra;
using namespace boost;

int main(int argc, const char* argv[]) {
shared_ptr socket(new TSocket(host, port));
shared_ptr transport(new TFramedTransport(socket));
shared_ptr protocol(new TBinaryProtocol(transport));
CassandraClient client(protocol);

const string& key="your_key";

ColumnPath cpath;
ColumnParent cp;

ColumnOrSuperColumn csc;
Column c;

c.name.assign("column_name");
c.value.assign("Data for our key to go into column_name");
c.timestamp = getTS();
c.ttl = 300;

cp.column_family.assign("nm_cfamily");
cp.super_column.assign("");

cpath.column_family.assign("nm_cfamily");
/* This is required - thrift 'feature' */
cpath.__isset.column = true;
cpath.column="column_name";
try {
transport->open();
cout << "Set keyspace to 'dpdns'.." << endl;
client.set_keyspace("nm_example");

cout << "Insert key '" << key << "' in column '" << c.name << "' in column family '" << cp.column_family << "' with timestamp " << c.timestamp << "..." << endl;
client.insert(key, cp, c, org::apache::cassandra::ConsistencyLevel::ONE);

cout << "Retrieve key '" << key << "' from column '" << cpath.column << "' in column family '" << cpath.column_family << "' again..." << endl;
client.get(csc, key, cpath, org::apache::cassandra::ConsistencyLevel::ONE);
cout << "Value read is '" << csc.column.value << "'..." << endl;

c.timestamp++;
c.value.assign("Updated data going into column_name");
cout << "Update key '" << key << "' in column with timestamp " << c.timestamp << "..." << endl;
client.insert(key, cp, c, org::apache::cassandra::ConsistencyLevel::ONE);

cout << "Retrieve updated key '" << key << "' from column '" << cpath.column << "' in column family '" << cpath.column_family << "' again..." << endl;
client.get(csc, key, cpath, org::apache::cassandra::ConsistencyLevel::ONE);
cout << "Updated value is: '" << csc.column.value << "'" << endl;

cout << "Remove the key '" << key << "' we just retrieved. Value '" << csc.column.value << "' timestamp " << csc.column.timestamp << " ..." << endl;
client.remove(key, cpath, csc.column.timestamp, org::apache::cassandra::ConsistencyLevel::ONE);

transport->close();
}
catch (NotFoundException &nf){
cerr << "NotFoundException ERROR: "<< nf.what() << endl;
}
catch (InvalidRequestException &re) {
cerr << "InvalidRequest ERROR: " << re.why << endl;
}
catch (TException &tx) {
cerr << "TException ERROR: " << tx.what() << endl;
}

return 0;
}

我得到的错误是

In file included from run_measure.cpp:13:
Cassandra.h:4289: error: ‘org::apache::thrift’ has not been declared
Cassandra.h:4289: error: expected ‘,’ or ‘...’ before ‘*’ token
Cassandra.h:4291: error: cannot declare pointer to ‘void’ member
Cassandra.h:4291: error: template argument 2 is invalid
Cassandra.h:4291: error: template argument 4 is invalid
Cassandra.h:4292: error: ‘org::apache::thrift’ has not been declared
Cassandra.h:4292: error: expected ‘,’ or ‘...’ before ‘*’ token
Cassandra.h:4293: error: ‘org::apache::thrift’ has not been declared
Cassandra.h:4293: error: expected ‘,’ or ‘...’ before ‘*’ token
Cassandra.h:4294: error: ‘org::apache::thrift’ has not been declared
Cassandra.h:4294: error: expected ‘,’ or ‘...’ before ‘*’ token
Cassandra.h:4295: error: ‘org::apache::thrift’ has not been declared
Cassandra.h:4295: error: expected ‘,’ or ‘...’ before ‘*’ token

它主要提示生成文件中的内容,所以我不确定我是否可以更改它们或者我是否做错了其他事情。作为引用,我的 Thrift 安装是 $HOME/thrift 所以包含的路径有点奇怪,它看起来像 $HOME/thrift/include/thrift 但我不' 认为这会导致此错误。如果有人有任何在 C++ 中使用 cassandra 的经验,我将非常感谢您的帮助。

这是错误中引用的行

  while (true)
{
xfer += iprot->readFieldBegin(fname, ftype, fid);
if (ftype == ::apache::thrift::protocol::T_STOP) {
break;
}
switch (fid)
{
default:
xfer += iprot->skip(ftype);
break;
}
xfer += iprot->readFieldEnd();
}

xfer += iprot->readStructEnd();

return xfer;
}

Full Cassandra.cpp
Full Error

再次感谢!

最佳答案

我遇到了同样的问题并修复了它:
对于 gcc,您应该在 Cassandra.h、Cassandra.cpp 中添加更改。

替换 apache::thrift -> ::apache::thrift

关于c++ - 使用 Thrift 和 Cassandra 编译 C++ 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8765473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com