【问题标题】:Compiling a c++ program using thrift and cassandra使用 thrift 和 cassandra 编译 C++ 程序
【发布时间】:2012-01-06 22:45:21
【问题描述】:

我试图编译一个简单的例子来使用 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

它主要抱怨生成文件中的内容,所以我不确定我是否可以更改它们或者我是否做错了什么。作为参考,我的节俭安装是$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

再次感谢!

【问题讨论】:

  • 您能否发布完整的错误信息以及来自Cassandra.h 的第4289 行?我怀疑生成的文件会尝试使用#include &lt;thrift/something.h&gt;,而您的路径设置会强制它使用#include &lt;something.h&gt;
  • 包含完整文件的链接
  • 您能告诉我们变量 CPLUS_INCLUDE_PATH、LD_LIBRARY_PATH 和 LIBRARY_PATH 包含什么吗?我的怀疑是他们没有引用适当的“顶级”,其中包括开始用于节俭类型。
  • 您使用的是什么版本的 g++? g++ Cassandra.cpp 是否构建 Cassandra.o?或者那也不编译。

标签: c++ compilation compiler-errors cassandra thrift


【解决方案1】:

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

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

【讨论】:

  • 顺便说一句,这似乎与 thrift 版本有所不同,事实证明 cassandra 1.0 使用 thrift 0.6,而我使用的是最新版本,降级 thrift 也解决了这个问题。跨度>
【解决方案2】:

我认为 Pierre 的补丁缺少一个修复程序。我做了一个小改动,它是available on pastebin

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-03-12
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-17
      • 1970-01-01
      • 2015-07-28
      • 2011-03-11
      相关资源
      最近更新 更多