编译 c++使用 thrift 和 cassandra 的程序

发布于 2024-12-25 06:08:54 字数 5552 浏览 2 评论 0原文

我试图编译一个简单的示例,用于使用 thrift 接口连接到 cassandra 实例。需要注意的是,我在没有访问 Linux 机器上的超级用户权限的情况下完成所有这些操作。

我安装了 thrift 和 c++ 生成器,将包含头文件放在我的 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;
}

完整 Cassandra.cpp
中引用的行 完全错误

再次感谢!

I was trying to compile a simple example for connecting to a cassandra instance using the thrift interface. As a note, I am doing all of this without access to super user privileges on a linux machine.

I installed thrift and the c++ generator, put the include headers on my CPLUS_INCLUDE_PATH variable, and the lib directory on my LIBRARY_PATH, and LD_LIBRARY_PATH. Cassandra is also installed and I ran thrift --gen cpp cassandra.thrift to generate the cassandra header files.

Using those I compiled my example like so
g++ -Wall run_measure.cpp cassandra_constants.cpp Cassandra.cpp cassandra_types.cpp -lthrift -o cassandra_example

The program looks mostly like this

#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;
}

The errors that I get are

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

Its complaining mostly about things in the generated files so I am not sure if I can change them or if I did something else wrong. As reference my thrift install is to $HOME/thrift so the path of the include is a little wierd, it looks like $HOME/thrift/include/thrift but I don't think that would cause this error. If anyone has any experience using cassandra in c++ I would really appreciate the help.

Here are the lines referenced in the error

  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

Thanks again!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追风人 2025-01-01 06:08:54

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

replace apache::thrift -> ::apache::thrift

I have the same problem and fixed it:

for gcc you should add changes in Cassandra.h, Cassandra.cpp.

replace apache::thrift -> ::apache::thrift

旧街凉风 2025-01-01 06:08:54

我认为皮埃尔的补丁缺少一个修复。我做了一个小小的更改,它可以在pastebin上使用

我可能是错的,如果我错了,请纠正我 - 这是我第一次玩补丁。

I think Pierre's patch was missing one fix. I've made a minor change and it's available on pastebin.

I may be wrong, and if I am please correct me - this is the first time I've played with patches.

反差帅 2025-01-01 06:08:54

如果您不想替换所有apache -> ::apache 将此补丁应用于 thrift-0.8 生成器。
http://pastebin.com/Sjhhiffm

重新编译 thrift 并使用新的二进制文件。

if you do not want to replace all apache -> ::apache apply this patch to thrift-0.8 generator.
http://pastebin.com/Sjhhiffm

recompile thrift and use the new binary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文