Created
October 28, 2014 16:29
-
-
Save mpenick/ba44e24300609994ecb4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <uv.h> | |
#include "cassandra.h" | |
#define USE_COMPOUND_PARTITION_KEY 0 | |
void print_error(CassFuture* future) { | |
CassString message = cass_future_error_message(future); | |
fprintf(stderr, "Error: %.*s\n", (int)message.length, message.data); | |
} | |
CassCluster* create_cluster() { | |
CassCluster* cluster = cass_cluster_new(); | |
cass_cluster_set_contact_points(cluster, "127.0.0.1,127.0.0.2,127.0.0.3"); | |
return cluster; | |
} | |
CassError connect_session(CassCluster* cluster, CassSession** output) { | |
CassError rc = CASS_OK; | |
CassFuture* future = cass_cluster_connect(cluster); | |
*output = NULL; | |
cass_future_wait(future); | |
rc = cass_future_error_code(future); | |
if(rc != CASS_OK) { | |
print_error(future); | |
} else { | |
*output = cass_future_get_session(future); | |
} | |
cass_future_free(future); | |
return rc; | |
} | |
CassError execute_query(CassSession* session, const char* query) { | |
CassError rc = CASS_OK; | |
CassFuture* future = NULL; | |
CassStatement* statement = cass_statement_new(cass_string_init(query), 0); | |
future = cass_session_execute(session, statement); | |
cass_future_wait(future); | |
rc = cass_future_error_code(future); | |
if(rc != CASS_OK) { | |
print_error(future); | |
} | |
cass_future_free(future); | |
cass_statement_free(statement); | |
return rc; | |
} | |
CassError insert_into_compound(CassSession* session) { | |
CassError rc = CASS_OK; | |
CassStatement* statement = NULL; | |
CassFuture* future = NULL; | |
CassUuid key; | |
CassString query = cass_string_init("INSERT INTO examples.compound (key, ts, value) VALUES (?, ?, ?);"); | |
statement = cass_statement_new(query, 3); | |
cass_uuid_generate_random(key); | |
cass_statement_bind_uuid(statement, 0, key); | |
cass_statement_bind_int64(statement, 1, uv_hrtime()); | |
cass_statement_bind_string(statement, 2, cass_string_init("value")); | |
future = cass_session_execute(session, statement); | |
cass_future_wait(future); | |
rc = cass_future_error_code(future); | |
if(rc != CASS_OK) { | |
print_error(future); | |
} | |
cass_future_free(future); | |
cass_statement_free(statement); | |
return rc; | |
} | |
CassError select_from_compound(CassSession* session) { | |
CassError rc = CASS_OK; | |
CassStatement* statement = NULL; | |
CassFuture* future = NULL; | |
CassString query = cass_string_init("SELECT * FROM examples.compound"); | |
statement = cass_statement_new(query, 0); | |
future = cass_session_execute(session, statement); | |
cass_future_wait(future); | |
rc = cass_future_error_code(future); | |
if(rc != CASS_OK) { | |
print_error(future); | |
} else { | |
const CassResult* result = cass_future_get_result(future); | |
CassIterator* iterator = cass_iterator_from_result(result); | |
while (cass_iterator_next(iterator)) { | |
char key[CASS_UUID_STRING_LENGTH]; | |
CassUuid uuid; | |
cass_int64_t ts; | |
CassString value; | |
const CassRow* row = cass_iterator_get_row(iterator); | |
cass_value_get_uuid(cass_row_get_column(row, 0), uuid); | |
cass_value_get_int64(cass_row_get_column(row, 1), &ts); | |
cass_value_get_string(cass_row_get_column(row, 2), &value); | |
cass_uuid_string(uuid, key); | |
printf("%s, %lld, %.*s\n", key, ts, (int)value.length, value.data); | |
} | |
cass_result_free(result); | |
cass_iterator_free(iterator); | |
} | |
cass_future_free(future); | |
cass_statement_free(statement); | |
return rc; | |
} | |
int main() { | |
CassError rc = CASS_OK; | |
CassCluster* cluster = create_cluster(); | |
CassSession* session = NULL; | |
CassFuture* close_future = NULL; | |
rc = connect_session(cluster, &session); | |
if(rc != CASS_OK) { | |
return -1; | |
} | |
execute_query(session, | |
"CREATE KEYSPACE examples WITH replication = { \ | |
'class': 'SimpleStrategy', 'replication_factor': '3' };"); | |
#if USE_COMPOUND_PARTITION_KEY | |
execute_query(session, | |
"CREATE TABLE examples.compound (key uuid, ts timestamp, value text, \ | |
PRIMARY KEY ((key, ts)));"); | |
#else | |
execute_query(session, | |
"CREATE TABLE examples.compound (key uuid, ts timestamp, value text, \ | |
PRIMARY KEY (key, ts));"); | |
#endif | |
insert_into_compound(session); | |
select_from_compound(session); | |
close_future = cass_session_close(session); | |
cass_future_wait(close_future); | |
cass_future_free(close_future); | |
cass_cluster_free(cluster); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment