Skip to content

hborn/cpp-driver

Repository files navigation

DataStax C/C++ Driver for Apache Cassandra (Beta)

A C/C++ driver for Apache Cassandra. This driver works exclusively with the Cassandra Query Language version 3 (CQL3) and Cassandra's native protocol (version 2).

Current Functionality and Design

  • Completely asynchronous
  • An emphasis on avoiding copying of memory when possible
  • Exception safe
  • Ad-hoc queries
  • Prepared statements
  • Batch statements
  • Connection pool with auto-reconnect
  • Cassandra collections

TODO

  • Compression
  • SSL support
  • Authentication
  • More docs
  • Packaging for Redhat/Centos and OSX
  • Integration tests
  • Query tracing
  • Event registration and notification
  • Compatibility with native protcol version 1
  • Binary releases for Windows and Linux

Building

The driver is known to work on OS X 10.9, Windows 7, and Ubuntu 14.04. The driver itself currently only has two dependencies libuv 0.10 and OpenSSL. To test the driver you will also need boost 1.41+, libssh2 and ccm.

To build the driver you will also need to install CMake.

OS X

The driver has been built and tested using the Clang compiler provided by XCode 5.1. The dependencies were obtained using Homebrew.

To obtain the dependencies:

brew install libuv cmake

Note: We currently use the OpenSSL library included with XCode.

To build:

git clone https://github.com/datastax/cpp-driver.git
cd cpp-driver
cmake . && make

Windows

The driver has been built and tested using Microsoft Visual Studio Express 2013 for Windows Desktop. The dependencies need to be manually built or obtained.

To obtain the dependencies:

  • Download or clone the driver
  • Download and install CMake for Windows. Make sure to select the option "Add CMake to the system PATH for all users" or "Add CMake to the system PATH for current user".
  • Download and build the latest release of libuv 0.10 from https://github.com/joyent/libuv/releases. -- Follow the instructions here. -- Open up the generated Visual Studio solution "uv.sln". -- If you want a 64-bit build you will need to create a "x64" solution platform in the "Configuration Manager". -- Open "Properties" on the "libuv" project. Set "Multi-threaded DLL (/MD)" for the "Configuration Properties -> C/C++ -> Code Generation -> Runtime Library" option. -- Build the "libuv" project -- Copy the files in "libuv/include" to "cpp-driver/lib/libuv/include" and "libuv/Release/lib" to "cpp-driver/lib/libuv/lib".
  • Download and install either the 32-bit or 64-bit version of OpenSSL from http://slproweb.com/products/Win32OpenSSL.html. You may also need to install the "Visual C++ 2008 Redistributables".

To build 32-bit (using "VS2013 x86 Native Tools Command Prompt"):

cd <path to driver>/cpp-driver
cmake -G "Visual Studio 12"
msbuild cassandra.vcxproj /p:Configuration=Release /t:Clean,Build

To build 64-bit (using "VS2013 x64 Cross Tools Command Prompt"):

cd <path to driver>/cpp-driver
cmake -G "Visual Studio 12 Win64"
msbuild cassandra.vcxproj /p:Configuration=Release /t:Clean,Build

Ubuntu

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

Examples

There are several examples provided here: examples.

A Simple Example - a simple query against system.schema_keyspaces.

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

int main() {
  CassError rc = 0;
  CassCluster* cluster = cass_cluster_new();
  CassSession* session = NULL;
  CassFuture* session_future = NULL;
  const char* contact_points[] = { "127.0.0.1",  NULL };
  const char** contact_point = NULL;

  for(contact_point = contact_points; *contact_point; contact_point++) {
    cass_cluster_setopt(cluster, CASS_OPTION_CONTACT_POINT_ADD, *contact_point, strlen(*contact_point));
  }

  session = cass_cluster_connect(cluster, &session_future);
  cass_future_wait(session_future);
  rc = cass_future_error_code(session_future);

  if(rc == CASS_OK) {
    CassFuture* result_future = NULL;
    CassFuture* shutdown_future = NULL;
    CassString query = cass_string_init("SELECT * FROM system.schema_keyspaces;");
    CassStatement* statement = cass_statement_new(query, 0, CASS_CONSISTENCY_ONE);

    result_future = cass_session_execute(session, statement);
    cass_future_wait(result_future);

    rc = cass_future_error_code(result_future);
    if(rc == CASS_OK) {
      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);
        CassString keyspace_name;
        cass_bool_t durable_writes;
        CassString strategy_class;
        CassString strategy_options;

        cass_value_get_string(cass_row_get_column(row, 0), &keyspace_name);
        cass_value_get_bool(cass_row_get_column(row, 1), &durable_writes);
        cass_value_get_string(cass_row_get_column(row, 2), &strategy_class);
        cass_value_get_string(cass_row_get_column(row, 3), &strategy_options);

        printf("keyspace_name: '%.*s', durable_writes: %s, strategy_class: '%.*s', strategy_options: %.*s\n",
               (int)keyspace_name.length, keyspace_name.data,
               durable_writes ? "true" : "false",
               (int)strategy_class.length, strategy_class.data,
               (int)strategy_options.length, strategy_options.data);
      }

      cass_result_free(result);
      cass_iterator_free(rows);
    } else {
      CassString message = cass_future_error_message(result_future);
      fprintf(stderr, "Error: %.*s\n", (int)message.length, message.data);
    }

    cass_future_free(result_future);
    shutdown_future = cass_session_shutdown(session);
    cass_future_wait(shutdown_future);
    cass_future_free(shutdown_future);
  } else {
    CassString message = cass_future_error_message(session_future);
    fprintf(stderr, "Error: %.*s\n", (int)message.length, message.data);
  }

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

  return 0;
}

License

Copyright (c) 2014 DataStax

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

DataStax C/C++ Driver for Apache Cassandra

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 83.4%
  • C 16.6%