elseif(EXISTS "/etc/redhat-release" OR EXISTS "/etc/fedora-release" OR EXISTS "/etc/slackware-version" OR EXISTS "/etc/gentoo-release" OR EXISTS "/etc/os-release") if(CMAKE_SIZEOF_VOID_P EQUAL 8) set (CMAKE_INSTALL_LIBDIR "lib64") else() set (CMAKE_INSTALL_LIBDIR "lib") endif()
(* 我有送出一個 pull request 被接受了,所以接下來的版本可能可以不用修改這裡)
Cassandra C++ driver 需要 libuv-devel 和 Open SSL 有安裝才能正確編譯。
建立一個 build 的目錄,然後使用下面的命令建立 Makefile (for openSUSE 64bit):
cmake -DCMAKE_INSTALL_PREFIX=/usr ..
再來就是 make 與 make install:
make sudo make install
下面就是測試的 C++ 程式:
#include <cassandra.h> #include <stdio.h> int main() { /* Setup and connect to cluster */ CassFuture* connect_future = NULL; CassCluster* cluster = cass_cluster_new(); CassSession* session = cass_session_new(); /* Add contact points */ cass_cluster_set_contact_points(cluster, "127.0.0.1"); /* Provide the cluster object as configuration to connect the session */ connect_future = cass_session_connect(session, cluster); if (cass_future_error_code(connect_future) == CASS_OK) { CassFuture* close_future = NULL; /* Build statement and execute query */ CassStatement* statement = cass_statement_new("SELECT keyspace_name " "FROM system_schema.keyspaces", 0); CassFuture* result_future = cass_session_execute(session, statement); if(cass_future_error_code(result_future) == CASS_OK) { /* Retrieve result set and iterate over the rows */ const CassResult* result = cass_future_get_result(result_future); CassIterator* rows = cass_iterator_from_result(result); while(cass_iterator_next(rows)) { const CassRow* row = cass_iterator_get_row(rows); const CassValue* value = cass_row_get_column_by_name(row, "keyspace_name"); const char* keyspace; size_t keyspace_length; cass_value_get_string(value, &keyspace, &keyspace_length); printf("keyspace_name: '%.*s'\n", (int)keyspace_length, keyspace); } cass_result_free(result); cass_iterator_free(rows); } else { /* Handle error */ const char* message; size_t message_length; cass_future_error_message(result_future, &message, &message_length); fprintf(stderr, "Unable to run query: '%.*s'\n", (int)message_length, message); } cass_statement_free(statement); cass_future_free(result_future); /* Close the session */ close_future = cass_session_close(session); cass_future_wait(close_future); cass_future_free(close_future); } else { /* Handle error */ const char* message; size_t message_length; cass_future_error_message(connect_future, &message, &message_length); fprintf(stderr, "Unable to connect: '%.*s'\n", (int)message_length, message); } cass_future_free(connect_future); cass_cluster_free(cluster); cass_session_free(session); return 0; }
注意,system_schema.keyspaces 在 3.x 之前,需要使用 system.schema_keyspaces 才行 (CASSANDRA-6717)。
因為 Tcl driver 建立在 CPP driver 之上,所以接下來是在 openSUSE 64bit 環境安裝 Tcl driver。
下面是 build 的命令:
autoconf ./configure --enable-64bit --libdir=/usr/lib64/tcl make sudo make install
如果成功的話,就可以使用 casstcl 這個套件來存取 Apache Cassandra。
下面是一個範例:
package require casstcl set cass [::casstcl::connect -host 127.0.0.1 -port 9042] $cass select "SELECT keyspace_name FROM system_schema.keyspaces;" row { parray row } $cass delete
目前 casstcl 在 prepared statement 使用 ? 做 parameters binding,需要在第一次使用 exec 前呼叫 reimport_column_type_map,執行的結果才會是正確的(更正:或者是在 create table 之後,因為新增的 table 還沒有對應資料)。
沒有留言:
張貼留言