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;
}
Thanks again!
I have the same problem and fixed it:
for gcc you should add changes in Cassandra.h, Cassandra.cpp.
replace apache::thrift
-> ::apache::thrift