title: Compiling a c++ program using thrift and cassandra
body: <p>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. </p>
  
  <p>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      <code>thrift --gen cpp cassandra.thrift</code> to generate the cassandra header files. </p>
  
  <p>Using those I compiled my example like so<br>
  <code>g++ -Wall run_measure.cpp cassandra_constants.cpp Cassandra.cpp cassandra_types.cpp -lthrift -o cassandra_example</code>  </p>
  
  <p>The program looks mostly like this</p>
  
  <pre><code>#include "Cassandra.h"
  
  #include &lt;protocol/TBinaryProtocol.h&gt;
  #include &lt;transport/TSocket.h&gt;
  #include &lt;transport/TTransportUtils.h&gt;
  
  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&amp; 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-&gt;open();
              cout &lt;&lt; "Set keyspace to 'dpdns'.." &lt;&lt; endl;
              client.set_keyspace("nm_example");
  
              cout &lt;&lt; "Insert key '" &lt;&lt; key &lt;&lt; "' in column '" &lt;&lt; c.name &lt;&lt; "' in column family '" &lt;&lt; cp.column_family &lt;&lt; "' with timestamp " &lt;&lt; c.timestamp &lt;&lt; "..." &lt;&lt; endl;
              client.insert(key, cp, c, org::apache::cassandra::ConsistencyLevel::ONE);
  
              cout &lt;&lt; "Retrieve key '" &lt;&lt; key &lt;&lt; "' from column '" &lt;&lt; cpath.column &lt;&lt; "' in column family '" &lt;&lt; cpath.column_family &lt;&lt; "' again..." &lt;&lt; endl;
              client.get(csc, key, cpath, org::apache::cassandra::ConsistencyLevel::ONE);
              cout &lt;&lt; "Value read is '" &lt;&lt; csc.column.value &lt;&lt; "'..." &lt;&lt; endl;
  
              c.timestamp++;
              c.value.assign("Updated data going into column_name");
              cout &lt;&lt; "Update key '" &lt;&lt; key &lt;&lt; "' in column with timestamp " &lt;&lt; c.timestamp &lt;&lt; "..." &lt;&lt; endl;
              client.insert(key, cp, c, org::apache::cassandra::ConsistencyLevel::ONE);
  
              cout &lt;&lt; "Retrieve updated key '" &lt;&lt; key &lt;&lt; "' from column '" &lt;&lt; cpath.column &lt;&lt; "' in column family '" &lt;&lt; cpath.column_family &lt;&lt; "' again..." &lt;&lt; endl;
              client.get(csc, key, cpath, org::apache::cassandra::ConsistencyLevel::ONE);
              cout &lt;&lt; "Updated value is: '" &lt;&lt; csc.column.value &lt;&lt; "'" &lt;&lt; endl;
  
              cout &lt;&lt; "Remove the key '" &lt;&lt; key &lt;&lt; "' we just retrieved. Value '" &lt;&lt; csc.column.value &lt;&lt; "' timestamp " &lt;&lt; csc.column.timestamp &lt;&lt; " ..." &lt;&lt; endl;
      client.remove(key, cpath, csc.column.timestamp, org::apache::cassandra::ConsistencyLevel::ONE);
  
      transport-&gt;close();
  }
  catch (NotFoundException &amp;nf){
  cerr &lt;&lt; "NotFoundException ERROR: "&lt;&lt; nf.what() &lt;&lt; endl;
  }
  catch (InvalidRequestException &amp;re) {
  cerr &lt;&lt; "InvalidRequest ERROR: " &lt;&lt; re.why &lt;&lt; endl;
  }
  catch (TException &amp;tx) {
  cerr &lt;&lt; "TException ERROR: " &lt;&lt; tx.what() &lt;&lt; endl;
  }
  
  return 0;
  }
  </code></pre>
  
  <p>The errors that I get are </p>
  
  <pre><code>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
  </code></pre>
  
  <p>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 <code>$HOME/thrift</code> so the path of the include is a little wierd, it looks like <code>$HOME/thrift/include/thrift</code> 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. </p>
  
  <p>Here are the lines referenced in the error</p>
  
  <pre><code>  while (true)
    {
      xfer += iprot-&gt;readFieldBegin(fname, ftype, fid);
      if (ftype == ::apache::thrift::protocol::T_STOP) {
        break;
      }
      switch (fid)
      {
        default:
          xfer += iprot-&gt;skip(ftype);
          break;
      }
      xfer += iprot-&gt;readFieldEnd();
    }
  
    xfer += iprot-&gt;readStructEnd();
  
    return xfer;
  }
  </code></pre>
  
  <p><a href="https://gist.github.com/1573845" rel="nofollow">Full Cassandra.cpp</a><br>
  <a href="https://gist.github.com/1573848" rel="nofollow">Full Error</a></p>
  
  <p>Thanks again!</p>