C++でApache-cassandraを読み書きする方法
スポンサーリンク
このドキュメントの内容は、以下の通りです。
はじめに
Apache Cassandra の 1.2.1 がリリースされてました。
C++ で Cassandra を触ってみようと思って、試してみました。
FreeBSD でやってたんですが、thrift で生成したC++のコードをコンパイルするのに、苦労しました。
Linux だと苦労しないのかもしれませんが、まだ、試してません。
Cのソースの生成
本件とは関係ないけど、メモ。
% /usr/local/bin/thrift --gen c_glib cassandra.thrift % ls -1 gen-c_glib/ cassandra.c cassandra.h cassandra_types.c cassandra_types.h
Cのソースのコンパイル
gcc -c `pkg-config --cflags thrift_c_glib` -I . *.c
C++のソースの生成
/usr/local/bin/thrift --gen cpp ./cassandra.thrift
% ls -1 gen-cpp/ Cassandra.cpp Cassandra.h Cassandra_server.skeleton.cpp cassandra_constants.cpp cassandra_constants.h cassandra_types.cpp cassandra_types.h
C++のソースのコンパイル
FreeBSD 9.0だと thrift で作ったソースがそのままコンパイルできないので、失敗します。
g++ -Wall *.cpp -lthrift -c -I /usr/local/include/thrift/ -I . -I /usr/local/include/ -L/usr/local/lib -lboost
ソースコードがよろしくないらしい。
いろいろ置換するとコンパイルできるようになるので、くじけないでください。
:argdo %s/[[:space:]]apache::/::apache::/g :argdo %s/<apache/< ::apache/g :argdo %s/(apache/( ::apache/g
テストコード
g++ -Wall -lthrift -I /usr/local/include/thrift/ -I . -I /usr/local/include/ -L/usr/local/lib Cassandra.o cassandra_constants.o cassandra_types.o main.cpp
cassandra に keyspace と column family を作っておきます。
% cassandra-cli
[default@unknown] create keyspace ks1;
ff3bf1ab-0b4f-3a01-ba7f-dda5b7336c50
[default@unknown] use ks1;
Authenticated to keyspace: ks1
[default@ks1] create column family cf1;
54290f95-6390-3a33-b837-63adc91ae73a
[default@ks1] set cf1[utf8('foo')][utf8('bar')] =utf8('hoge');
Value inserted.
Elapsed time: 85 msec(s).
main.c
#include "Cassandra.h"
#include <protocol/TBinaryProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/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;
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="foo";
ColumnPath cpath;
ColumnParent cp;
ColumnOrSuperColumn csc;
Column c;
c.name.assign("bar2");
c.value.assign("hogehoge");
c.timestamp = getTS();
c.ttl = 300;
string cf = "cf1";
cp.column_family.assign(cf);
cp.super_column.assign("");
cpath.column_family.assign(cf);
/* This is required - thrift 'feature' */
cpath.__isset.column = true;
cpath.column="column_name";
cpath.column_family = cf;
try {
transport->open();
cout << "Set keyspace to 'ks1'.." << endl;
client.set_keyspace("ks1");
ColumnOrSuperColumn ret_val;
cpath.column="bar";
client.get(ret_val, "foo", cpath, ConsistencyLevel::ONE);
cout << "name: " << ret_val.column.name << endl;
cout << "value: " << ret_val.column.value << endl;
cpath.column="bar2";
c.value = "metameta";
// InvalidRequest ERROR: Column value is required
c.__isset.timestamp = true;
c.__isset.value = true;
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;
}
スポンサーリンク
スポンサーリンク
いつもシェア、ありがとうございます!
もっと情報を探しませんか?
関連記事
最近の記事
- パナソニック ジェットウォッシャードルツ EW-DJ61-Wのホースの修理
- LinuxセキュリティモジュールIntegrity Policy Enforcement
- アマゾンのEcho Show 5を買ったのでレビューします
- アマゾンのサイバーマンデーはAlexa Echo Show 5が安い
- Android スマートフォン OnePlus 7T と OnePlus 7の違い
- Android スマートフォン OnePlus 7 をAndroid10にアップデートしてみた
- クレジットカードのバーチャルカードの比較のまとめ
- 活動量計 Xiaomi Mi Band 4を買ってみたのでレビュー
- Android スマートフォン OnePlus 7 のレビュー
- AliExpressでスマートフォンを買い物してみた
- パソコンのホコリ対策 レンジフードフィルターと養生テープ
- 80PLUS GOLDのPC電源ユニットAntec NeoEco 750 Goldのレビュー
- イギリスの付加価値税 VAT は払い戻しを受けられる
- イギリスのロンドンでスーツケースなど荷物を預けられる場所は
- イギリスのロンドンで地下鉄やバスに乗るならオイスターカードを使おう
- イギリスのヒースロー空港からロンドン市内への行き方
- 航空便でほかの航空会社に乗り継ぎがある場合のオンラインチェックイン
- SFC会員がANA便ではなくベトナム航空のコードシェアを試して解ったこと
- ベトナムの入国審査でeチケットの掲示が必要だった話
- シアトルの交通ICカードはオルカカード(Orca)です
人気のページ
- Windows7 IME 辞書ツールで単語の登録に失敗しました
- C言語 popen()でコマンドを実行して出力を読み込む
- Windows7で休止状態にする方法
- CentOS MySQLの起動、停止、再起動
- loggerコマンドでsyslogにエラーを出力する方法
- パソコンパーツの買取をしてくれる店のまとめ
- Java Mapの使い方 get(),put(),remove(),size(),clear()
- 楽天のRポイントカードを作ってみた
- iPhone 5 から iPhone 6 に乗り換えたのでレビュー
- netstatコマンドのステータスの意味
スポンサーリンク
過去ログ
2020 : 01 02 03 04 05 06 07 08 09 10 11 122019 : 01 02 03 04 05 06 07 08 09 10 11 12
2018 : 01 02 03 04 05 06 07 08 09 10 11 12
2017 : 01 02 03 04 05 06 07 08 09 10 11 12
2016 : 01 02 03 04 05 06 07 08 09 10 11 12
2015 : 01 02 03 04 05 06 07 08 09 10 11 12
2014 : 01 02 03 04 05 06 07 08 09 10 11 12
2013 : 01 02 03 04 05 06 07 08 09 10 11 12
2012 : 01 02 03 04 05 06 07 08 09 10 11 12
2011 : 01 02 03 04 05 06 07 08 09 10 11 12
2010 : 01 02 03 04 05 06 07 08 09 10 11 12
2009 : 01 02 03 04 05 06 07 08 09 10 11 12
2008 : 01 02 03 04 05 06 07 08 09 10 11 12
2007 : 01 02 03 04 05 06 07 08 09 10 11 12
2006 : 01 02 03 04 05 06 07 08 09 10 11 12
2005 : 01 02 03 04 05 06 07 08 09 10 11 12
2004 : 01 02 03 04 05 06 07 08 09 10 11 12
2003 : 01 02 03 04 05 06 07 08 09 10 11 12