Created
July 21, 2017 13:07
-
-
Save mpenick/cb11e47f7e7757ffc2f59f9dfbd00bba 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 <assert.h> | |
| #include <string.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include "cassandra.h" | |
| void print_error(CassFuture* future) { | |
| const char* message; | |
| size_t message_length; | |
| cass_future_error_message(future, &message, &message_length); | |
| fprintf(stderr, "Error: %.*s\n", (int)message_length, message); | |
| } | |
| CassCluster* create_cluster(const char* hosts) { | |
| CassCluster* cluster = cass_cluster_new(); | |
| cass_cluster_set_contact_points(cluster, hosts); | |
| return cluster; | |
| } | |
| CassError connect_session(CassSession* session, const CassCluster* cluster) { | |
| CassError rc = CASS_OK; | |
| CassFuture* future = cass_session_connect(session, cluster); | |
| cass_future_wait(future); | |
| rc = cass_future_error_code(future); | |
| if (rc != CASS_OK) { | |
| print_error(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(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; | |
| } | |
| int main(int argc, char* argv[]) { | |
| CassCluster* cluster = NULL; | |
| CassSession* session = cass_session_new(); | |
| char* hosts = "192.168.56.1"; | |
| cass_log_set_level(CASS_LOG_INFO); | |
| if (argc > 1) { | |
| hosts = argv[1]; | |
| } | |
| cluster = create_cluster(hosts); | |
| if (connect_session(session, cluster) != CASS_OK) { | |
| cass_cluster_free(cluster); | |
| cass_session_free(session); | |
| return -1; | |
| } | |
| execute_query(session, "DROP KEYSPACE test"); | |
| execute_query(session, | |
| "CREATE KEYSPACE test WITH replication = { \ | |
| 'class': 'SimpleStrategy', 'replication_factor': '1' };"); | |
| execute_query(session, "USE test;"); | |
| execute_query(session, | |
| "CREATE TABLE schema_meta (key text, " | |
| "value bigint, " | |
| "PRIMARY KEY (key));"); | |
| execute_query(session, | |
| "CREATE FUNCTION avg_state(state tuple<int, bigint>, val int) " | |
| "CALLED ON NULL INPUT RETURNS tuple<int, bigint> " | |
| "LANGUAGE java AS " | |
| " 'if (val != null) { " | |
| " state.setInt(0, state.getInt(0) + 1); " | |
| " state.setLong(1, state.getLong(1) + val.intValue()); " | |
| " } ;" | |
| " return state;'" | |
| ";"); | |
| execute_query(session, | |
| "CREATE FUNCTION avg_final (state tuple<int, bigint>) " | |
| "CALLED ON NULL INPUT RETURNS double " | |
| "LANGUAGE java AS " | |
| " 'double r = 0; " | |
| " if (state.getInt(0) == 0) return null; " | |
| " r = state.getLong(1); " | |
| " r /= state.getInt(0); " | |
| " return Double.valueOf(r);' " | |
| ";"); | |
| execute_query(session, | |
| "CREATE AGGREGATE average(int) " | |
| "SFUNC avg_state STYPE tuple<int, bigint> FINALFUNC avg_final " | |
| "INITCOND(0, 0);"); | |
| execute_query(session, | |
| "CREATE MATERIALIZED VIEW schema_meta_by_value " | |
| "AS SELECT value " | |
| " FROM schema_meta " | |
| " WHERE value IS NOT NULL and key IS NOT NULL " | |
| "PRIMARY KEY(value, key)" | |
| ); | |
| execute_query(session, | |
| "CREATE TYPE address (street text, city text)" | |
| ); | |
| execute_query(session, | |
| "CREATE INDEX schema_meta_index " | |
| "ON schema_meta (value)" | |
| ); | |
| { | |
| const char* keyspace = "test"; | |
| const char* table = "schema_meta"; | |
| const char* index = "schema_meta_index"; | |
| const CassSchemaMeta* schema_meta = cass_session_get_schema_meta(session); | |
| const CassKeyspaceMeta* keyspace_meta = cass_schema_meta_keyspace_by_name(schema_meta, keyspace); | |
| if (keyspace_meta != NULL) { | |
| printf("Found keyspace \"%s\"\n", keyspace); | |
| } else { | |
| fprintf(stderr, "Unable to find \"%s\" keyspace in the schema metadata\n", keyspace); | |
| } | |
| const CassTableMeta* table_meta = cass_keyspace_meta_table_by_name(keyspace_meta, table); | |
| if (table_meta != NULL) { | |
| printf("Found table \"%s\"\n", table); | |
| } else { | |
| fprintf(stderr, "Unable to find \"%s\" table in the schema metadata\n", table); | |
| } | |
| const CassIndexMeta* index_meta = cass_table_meta_index_by_name(table_meta, index); | |
| if (index_meta != NULL) { | |
| printf("Found index \"%s\"\n", index); | |
| } else { | |
| fprintf(stderr, "Unable to find \"%s\" index in the schema metadata\n", index); | |
| } | |
| cass_schema_meta_free(schema_meta); | |
| } | |
| execute_query(session, "DROP KEYSPACE test"); | |
| 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