Develop C/C++ Apps

AttentionThis page documents an earlier version. Go to the latest (v2.1)version.

Pre-requisites

The tutorial assumes that you have:

  • installed YugabyteDB, created a universe and are able to interact with it using the CQL shell. Ifnot, please follow these steps in the quick start guide.
  • have a 32-bit (x86) or 64-bit (x64) architecture machine.
  • have gcc 4.1.2+, Clang 3.4+ installed.

Installing the C/C++ Driver

To get the C/C++ driver run:

  1. $ git clone https://github.com/datastax/cpp-driver.git

Dependencies

The C/C++ driver depends on the following:

  • CMake v2.6.4+
  • libuv 1.x
  • OpenSSL v1.0.x or v1.1.x

More detailed instructions for installing the dependencies aregiven here.

Build and Install

To build and install the driver:

  1. $ mkdir build
  2. $ cd build
  3. $ cmake ..
  4. $ make
  5. $ make install

Working Example

Writing the C/C++ Code.

Create a file ybcql_hello_world.c and copy the contents below:

  1. #include <assert.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "cassandra.h"
  6. void print_error(CassFuture* future) {
  7. const char* message;
  8. size_t message_length;
  9. cass_future_error_message(future, &message, &message_length);
  10. fprintf(stderr, "Error: %.*s\n", (int)message_length, message);
  11. }
  12. // Create a new cluster.
  13. CassCluster* create_cluster(const char* hosts) {
  14. CassCluster* cluster = cass_cluster_new();
  15. cass_cluster_set_contact_points(cluster, hosts);
  16. return cluster;
  17. }
  18. // Connect to the cluster given a session.
  19. CassError connect_session(CassSession* session, const CassCluster* cluster) {
  20. CassError rc = CASS_OK;
  21. CassFuture* future = cass_session_connect(session, cluster);
  22. cass_future_wait(future);
  23. rc = cass_future_error_code(future);
  24. if (rc != CASS_OK) {
  25. print_error(future);
  26. }
  27. cass_future_free(future);
  28. return rc;
  29. }
  30. CassError execute_query(CassSession* session, const char* query) {
  31. CassError rc = CASS_OK;
  32. CassFuture* future = NULL;
  33. CassStatement* statement = cass_statement_new(query, 0);
  34. future = cass_session_execute(session, statement);
  35. cass_future_wait(future);
  36. rc = cass_future_error_code(future);
  37. if (rc != CASS_OK) {
  38. print_error(future);
  39. }
  40. cass_future_free(future);
  41. cass_statement_free(statement);
  42. return rc;
  43. }
  44. CassError execute_and_log_select(CassSession* session, const char* stmt) {
  45. CassError rc = CASS_OK;
  46. CassFuture* future = NULL;
  47. CassStatement* statement = cass_statement_new(stmt, 0);
  48. future = cass_session_execute(session, statement);
  49. rc = cass_future_error_code(future);
  50. if (rc != CASS_OK) {
  51. print_error(future);
  52. } else {
  53. const CassResult* result = cass_future_get_result(future);
  54. CassIterator* iterator = cass_iterator_from_result(result);
  55. if (cass_iterator_next(iterator)) {
  56. const CassRow* row = cass_iterator_get_row(iterator);
  57. int age;
  58. const char* name; size_t name_length;
  59. const char* language; size_t language_length;
  60. cass_value_get_string(cass_row_get_column(row, 0), &name, &name_length);
  61. cass_value_get_int32(cass_row_get_column(row, 1), &age);
  62. cass_value_get_string(cass_row_get_column(row, 2), &language, &language_length);
  63. printf ("Select statement returned: Row[%.*s, %d, %.*s]\n", (int)name_length, name,
  64. age, (int)language_length, language);
  65. } else {
  66. printf("Unable to fetch row!\n");
  67. }
  68. cass_result_free(result);
  69. cass_iterator_free(iterator);
  70. }
  71. cass_future_free(future);
  72. cass_statement_free(statement);
  73. return rc;
  74. }
  75. int main() {
  76. // Ensure we log errors.
  77. cass_log_set_level(CASS_LOG_ERROR);
  78. CassCluster* cluster = NULL;
  79. CassSession* session = cass_session_new();
  80. CassFuture* close_future = NULL;
  81. char* hosts = "127.0.0.1";
  82. cluster = create_cluster(hosts);
  83. if (connect_session(session, cluster) != CASS_OK) {
  84. cass_cluster_free(cluster);
  85. cass_session_free(session);
  86. return -1;
  87. }
  88. CassError rc = CASS_OK;
  89. rc = execute_query(session, "CREATE KEYSPACE IF NOT EXISTS ybdemo");
  90. if (rc != CASS_OK) return -1;
  91. printf("Created keyspace ybdemo\n");
  92. rc = execute_query(session, "DROP TABLE IF EXISTS ybdemo.employee");
  93. if (rc != CASS_OK) return -1;
  94. rc = execute_query(session,
  95. "CREATE TABLE ybdemo.employee (id int PRIMARY KEY, \
  96. name varchar, \
  97. age int, \
  98. language varchar)");
  99. if (rc != CASS_OK) return -1;
  100. printf("Created table ybdemo.employee\n");
  101. const char* insert_stmt = "INSERT INTO ybdemo.employee (id, name, age, language) VALUES (1, 'John', 35, 'C/C++')";
  102. rc = execute_query(session, insert_stmt);
  103. if (rc != CASS_OK) return -1;
  104. printf("Inserted data: %s\n", insert_stmt);
  105. const char* select_stmt = "SELECT name, age, language from ybdemo.employee WHERE id = 1";
  106. rc = execute_and_log_select(session, select_stmt);
  107. if (rc != CASS_OK) return -1;
  108. close_future = cass_session_close(session);
  109. cass_future_wait(close_future);
  110. cass_future_free(close_future);
  111. cass_cluster_free(cluster);
  112. cass_session_free(session);
  113. return 0;
  114. }

Running the application

You can compile the file using gcc or clang.For clang, you can use:

  1. $ clang ybcql_hello_world.c -lcassandra -Iinclude -o yb_cql_hello_world

Run with:

  1. $ ./yb_cql_hello_world

You should see the following output:

  1. Created keyspace ybdemo
  2. Created table ybdemo.employee
  3. Inserted data: INSERT INTO ybdemo.employee (id, name, age, language) VALUES (1, 'John', 35, 'C/C++')
  4. Select statement returned: Row[John, 35, C/C++]

Pre-requisites

The tutorial assumes that you have:

  • installed YugabyteDB, created a universe and are able to interact with it using the Redis shell. Ifnot please follow these steps in the quick start guide.
  • have C++11.

Installing the Redis C++ Driver

We use the cpp_redis driver. To install the library do the following:

  • Clone the cpp_redis repository
  1. $ git clone https://github.com/Cylix/cpp_redis.git
  • Get the networking module (tacopie)
  1. $ cd cpp_redis
  2. $ git submodule init && git submodule update
  • Create a build directory and move into it
  1. $ mkdir build && cd build
  • Generate the Makefile using CMake
  1. $ cmake .. -DCMAKE_BUILD_TYPE=Release
  • Build and install the library
  1. $ make
  2. $ make install

Writing a hello world redis app

Create a file ybredis_hello_world.cpp and copy the contents below:

  1. #include <cpp_redis/cpp_redis>
  2. #include<iostream>
  3. #include<vector>
  4. #include<string>
  5. #include<utility>
  6. using namespace std;
  7. int main() {
  8. cpp_redis::client client;
  9. client.connect("127.0.0.1", 6379, [](const std::string& host, std::size_t port, cpp_redis::client::connect_state status) {
  10. if (status == cpp_redis::client::connect_state::dropped) {
  11. std::cout << "client disconnected from " << host << ":" << port << std::endl;
  12. }
  13. });
  14. string userid = "1";
  15. vector<pair<string, string>> userProfile;
  16. userProfile.push_back(make_pair("name", "John"));
  17. userProfile.push_back(make_pair("age", "35"));
  18. userProfile.push_back(make_pair("language", "Redis"));
  19. // Insert the data
  20. client.hmset(userid, userProfile, [](cpp_redis::reply& reply) {
  21. cout<< "HMSET returned " << reply << ": id=1, name=John, age=35, language=Redis" << endl;
  22. });
  23. // Query the data
  24. client.hgetall(userid, [](cpp_redis::reply& reply) {
  25. std::vector<cpp_redis::reply> retVal;
  26. if (reply.is_array()) {
  27. retVal = reply.as_array();
  28. }
  29. cout << "Query result:" <<endl;
  30. for (int i = 0; i < retVal.size(); i=i+2) {
  31. cout << retVal[i] << "=" <<retVal[i+1] << endl;
  32. }
  33. });
  34. // synchronous commit, no timeout
  35. client.sync_commit();
  36. return 0;
  37. }

Running the app

To compile the file, run the following command

  1. $ g++ -ltacopie -lcpp_redis -std=c++11 -o ybredis_hello_world ybredis_hello_world.cpp

To run the app do

  1. $ ./ybredis_hello_world

You should see the following output

  1. HMSET returned OK: id=1, name=John, age=35, language=Redis
  2. Query result:
  3. age=35
  4. language=Redis
  5. name=John