Skip to content

Instantly share code, notes, and snippets.

@mpenick
Last active August 29, 2015 14:15
Show Gist options
  • Save mpenick/01a6e38ee05db1420338 to your computer and use it in GitHub Desktop.
Save mpenick/01a6e38ee05db1420338 to your computer and use it in GitHub Desktop.
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#include <cassandra.h>
#include <stdio.h>
#include <unistd.h>
void on_log(const CassLogMessage* message, void* data) {
printf("log: '%s'\n", message->message);
}
int main() {
/* Setup and connect to cluster */
CassFuture* connect_future = NULL;
CassCluster* cluster = cass_cluster_new();
CassSession* session = cass_session_new();
cass_log_set_callback(on_log, NULL);
/* Add contact points */
cass_cluster_set_contact_points(cluster, "127.0.0.1,127.0.0.2,127.0.0.3");
/* 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 */
CassString query = cass_string_init("SELECT * FROM examples.var");
CassStatement* statement = cass_statement_new(query, 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)) {
size_t i;
CassString key;
CassBytes num;
const CassRow* row = cass_iterator_get_row(rows);
const CassValue* key_value = cass_row_get_column_by_name(row, "key");
const CassValue* num_value = cass_row_get_column_by_name(row, "num");
if(cass_value_get_string(key_value, &key) != CASS_OK) {
printf("Error getting value from key\n");
}
printf("%.*s: ", (int)key.length, key.data);
if(cass_value_get_bytes(num_value, &num) == CASS_OK) {
for (i = 0; i < num.size; ++i) {
printf("0x%02x ", num.data[i]);
}
} else if(cass_value_is_null(num_value)) {
printf("null");
} else {
printf("Error getting value from value");
}
printf("\n");
}
cass_result_free(result);
cass_iterator_free(rows);
} else {
/* Handle error */
CassString message = cass_future_error_message(result_future);
fprintf(stderr, "Unable to run query: '%.*s'\n", (int)message.length,
message.data);
}
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 */
CassString message = cass_future_error_message(connect_future);
fprintf(stderr, "Unable to connect: '%.*s'\n", (int)message.length,
message.data);
}
cass_future_free(connect_future);
cass_cluster_free(cluster);
cass_session_free(session);
cass_log_cleanup();
return 0;
}
CREATE TABLE var (key text primary key, num varint);
INSERT INTO examples.var (key, num) VALUES ('1', 1);
INSERT INTO examples.var (key, num) VALUES ('1234', 1234);
INSERT INTO examples.var (key, num) VALUES ('null', null);
INSERT INTO examples.var (key, num) VALUES ('0', 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment