Skip to content

Instantly share code, notes, and snippets.

@kyunghoj
Created January 14, 2018 02:04
Show Gist options
  • Save kyunghoj/4780a0c788b6e278d0bd582045208120 to your computer and use it in GitHub Desktop.
Save kyunghoj/4780a0c788b6e278d0bd582045208120 to your computer and use it in GitHub Desktop.
Cassandra C/C++ Driver 설치

Cassandra C/C++ Driver 설치 및 설정

참고자료: http://datastax.github.io/cpp-driver/topics/building/

설치

공식 사이트에서 deb 패키지가 제공되지만, 제가 해 보니 잘 안 되므로 (Ubuntu 16.04 기준) 소스코드를 받아서 빌드해서 설치해 봅시다. Ubuntu Linux에 설치한다고 가정합니다.

필요한 라이브러리 (Dependencies) 설치

  • CMake
  • libuv (1.x or 0.10.x)
  • OpenSSL (Optional)
  • boost 1.59+ (For Test)
  • libssh2 (For test, Optional)

libuv라는 라이브러리가 필요한데, Ubuntu 12.04 까지는 기본 repository에 포함되지 않았다고 합니다. 다음 명령으로 repository를 추가합니다.

$ sudo apt-add-repository ppa:linuxjedi/ppa
$ sudo apt-get update

GCC를 쓸 경우:

$ sudo apt-get install g++ make cmake libuv-dev libssl-dev

Clang을 쓸 경우:

$ sudo apt-get install clang make cmake libuv-dev libssl-dev

Boost 라이브러리 버전 v1.59 이상을 설치하기 위해 다음과 같이 소스코드를 받아서 설치합니다.

$ sudo apt-get install libssh2-1-dev
$ pushd /tmp
$ wget http://sourceforge.net/projects/boost/files/boost/1.63.0/boost_1_63_0.tar.gz/download -O boost_1_63_0.tar.gz
$ tar xzf boost_1_63_0.tar.gz
$ pushd boost_1_63_0
./bootstrap.sh --with-libraries=atomic,chrono,system,thread,test
$ sudo ./b2 install
$ popd
$ popd

드라이버 설치

이제 Cassandra Driver 소스코드를 받아서 컴파일하고 설치합니다.

$ git clone https://github.com/datastax/cpp-driver.git
$ mkdir cpp-driver/build
$ cd cpp-driver/build
$ cmake -DCASS_BUILD_TESTS=ON ..
$ make
$ sudo make install

설치하고 나면 libcassandra* 류의 파일들이 /usr/local/lib/x86_64-linux-gnu 에 복사됩니다.

$ sudo vi /etc/ld.so.conf.d/x86_64-linux-gnu.conf

에서 /usr/local/lib/x86_64-linux-gnu를 마지막 줄에 추가하고,

$ sudo ldconfig

를 실행해서 새로 추가된 Cassadra driver 의 shared library의 캐시를 만듭니다. (참고자료-Shared Library)

잘 되나?

다음과 같은 간단한 예제 소스코드를 main.c 파일로 저장합니다.

#include <cassandra.h>
#include <stdio.h>

int main() {
  /* Setup and connect to cluster */
  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 */
  CassFuture* connect_future = cass_session_connect(session, cluster);

  /* This operation will block until the result is ready */
  CassError rc = cass_future_error_code(connect_future);

  printf("Connect result: %s\n", cass_error_desc(rc));

  /* Run queries... */

  cass_future_free(connect_future);
  cass_session_free(session);
  cass_cluster_free(cluster);

  return 0;
}

컴파일하고,

$ gcc -o main main.c

실행!

$ ./main
1515895366.037 [ERROR] (src/control_connection.cpp:263:virtual void cass::ControlConnection::on_close(cass::Connection*)): Unable to establish a control connection to host 127.0.0.1 because of the following error: Connect error 'connection refused'
Connect result: No hosts available

에러는 Cassandra 서버를 실행하지 않았기 때문에 발생합니다. 서버 실행해서 테스트 하는 것은 생략...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment