Last active
September 16, 2015 17:32
-
-
Save mpenick/a43b3d7ddde454fe9c0a 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
| /* | |
| 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 <string.h> | |
| int main() { | |
| /* Setup and connect to cluster */ | |
| CassFuture* connect_future = NULL; | |
| 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 */ | |
| connect_future = cass_session_connect(session, cluster); | |
| if (cass_future_error_code(connect_future) == CASS_OK) { | |
| CassFuture* close_future = NULL; | |
| /* This query will cause the warning: "Aggregation query used without partition key" */ | |
| const char* query = "select sum(gossip_generation) FROM system.local"; | |
| CassStatement* statement = cass_statement_new(query, 0); | |
| const char* value1 = "value1"; | |
| const char* value2 = "value2"; | |
| CassCustomPayload* payload = cass_custom_payload_new(); | |
| CassFuture* result_future; | |
| cass_custom_payload_set(payload, "key1", (cass_byte_t *)value1, strlen(value1)); | |
| cass_custom_payload_set(payload, "key2", (cass_byte_t *)value2, strlen(value2)); | |
| cass_statement_set_custom_payload(statement, payload); | |
| cass_custom_payload_free(payload); | |
| result_future = cass_session_execute(session, statement); | |
| if (cass_future_error_code(result_future) == CASS_OK) { | |
| { | |
| size_t i; | |
| size_t item_count; | |
| const char* item_name; | |
| size_t item_name_size; | |
| const cass_byte_t* item_value; | |
| size_t item_value_size; | |
| item_count = cass_future_custom_payload_item_count(result_future); | |
| for (i = 0; i < item_count; ++i) { | |
| cass_future_custom_payload_item(result_future, i, | |
| &item_name, &item_name_size, | |
| &item_value, & item_value_size); | |
| printf("index: %.*s:%.*s\n", (int)item_name_size, item_name, (int)item_value_size, (char*)item_value); | |
| } | |
| } | |
| } else { | |
| /* Handle error */ | |
| const char* message; | |
| size_t message_length; | |
| cass_future_error_message(result_future, &message, &message_length); | |
| fprintf(stderr, "Unable to run query: '%.*s'\n", (int)message_length, | |
| message); | |
| } | |
| 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 */ | |
| const char* message; | |
| size_t message_length; | |
| cass_future_error_message(connect_future, &message, &message_length); | |
| fprintf(stderr, "Unable to connect: '%.*s'\n", (int)message_length, | |
| message); | |
| } | |
| cass_future_free(connect_future); | |
| cass_cluster_free(cluster); | |
| cass_session_free(session); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment