incubator-cassandra-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From 刘志明 <liuzhiming...@163.com>
Subject developing with cassandra use c++ encounter some problem
Date Mon, 15 Aug 2011 07:13:25 GMT
hi:
   I am now use cassandra 0.8.4, when i use java, i can insert and get my value,but c++ insert
and get all not success. i compare the cassandra logs,find it success,but the client raise
a exception.  do you have any c++ cassandra  client demo? great thanks! 
 
DEBUG 15:08:21,757 Command/ConsistencyLevel is SliceByNamesReadCommand(table='nm_example',
key=30, columnParent='QueryPath(columnFamilyName='nm_cfamily', superColumnName='null', columnName='null')',
columns=[636f6c756d6e5f6e616d65,])/ONE
DEBUG 15:08:21,758 Blockfor/repair is 1/true; setting up requests to localhost/127.0.0.1
DEBUG 15:08:21,758 reading data locally
DEBUG 15:08:21,758 LocalReadRunnable reading SliceByNamesReadCommand(table='nm_example', key=30,
columnParent='QueryPath(columnFamilyName='nm_cfamily', superColumnName='null', columnName='null')',
columns=[636f6c756d6e5f6e616d65,])
DEBUG 15:08:21,759 Read: 1 ms.
DEBUG 15:08:21,760 logged out: #<User allow_all groups=[]>
 
my program:
 
#include "Cassandra.h"
#include <protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TTransportUtils.h>
#include <iostream>
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;
static string host("127.0.0.1");
static int port= 9160;
int64_t getTS(){
/* If you're doing things quickly, you may want to make use of tv_usec
* or something here instead
*/
time_t ltime;
ltime=time(NULL);
return (int64_t)ltime;
}
int main(){
shared_ptr<TSocket> socket(new TSocket(host, port));
shared_ptr<TFramedTransport> transport(new TFramedTransport(socket));
shared_ptr<TBinaryProtocol> 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 'nm_example'.." << endl;
client.set_keyspace("nm_example");
//cout << "Insert key '" << key << "' in column '" << c.name <<
"' in column family '" << cp.column_family<<"column value "<<c.value <<
"' 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, "0", 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;
}
 
result:
[root@chunkservers3 cpp-test]# ./cpptest
Set keyspace to 'nm_example'..
Retrieve key 'your_key' from column 'column_name' in column family 'nm_cfamily' again...
NotFoundException ERROR: Default TException.
[root@chunkservers3 cpp-test]#

 

 
 
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message