Last active
July 7, 2016 21:14
-
-
Save mpenick/e93e9ecd6ca4ea8820d954ad4cfa651e to your computer and use it in GitHub Desktop.
C/C++ Solr Example
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
| id: 40969, ethnicity: 'Not Hispanic' | |
| id: 9979, ethnicity: 'Not Hispanic' | |
| id: 9339, ethnicity: 'Not Hispanic' | |
| id: 18320, ethnicity: 'Not Hispanic' | |
| id: 7075, ethnicity: 'Not Hispanic' | |
| id: 39617, ethnicity: 'Not Hispanic' | |
| id: 47076, ethnicity: 'Not Hispanic' | |
| id: 7099, ethnicity: 'Not Hispanic' | |
| id: 38875, ethnicity: 'Not Hispanic' | |
| id: 37982, ethnicity: 'Not Hispanic' |
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
| Download data and schema from: http://docs.datastax.com/en/tutorials/solr_tutorial46.zip | |
| 1) Startup DSE w/ Solr support | |
| 2) Create schema | |
| CREATE KEYSPACE nhanes_ks WITH REPLICATION = {'class':'NetworkTopologyStrategy', 'dc1':1}; | |
| USE nhanes_ks; | |
| CREATE TABLE IF NOT EXISTS nhanes ( | |
| "id" INT, | |
| "num_smokers" INT, | |
| "age" INT, | |
| "age_unit" VARCHAR, | |
| "age_months" INT, | |
| "major_medical_coverage" VARCHAR, | |
| "dental_coverage" VARCHAR, | |
| "routine_medical_coverage" VARCHAR, | |
| "employer_paid_plan" VARCHAR, | |
| "secondary_smoke" VARCHAR, | |
| "county" VARCHAR, | |
| "screening_month" VARCHAR, | |
| "pets" VARCHAR, | |
| "asthma" VARCHAR, | |
| "bronchitis" VARCHAR, | |
| "goiter" VARCHAR, | |
| "hay_fever" VARCHAR, | |
| "thyroid_disease" VARCHAR, | |
| "chronic_bronchitis" VARCHAR, | |
| "diagnosed_asthma" VARCHAR, | |
| "diagnosed_cataracts" VARCHAR, | |
| "diagnosed_emphysema" VARCHAR, | |
| "diagnosed_goiter" VARCHAR, | |
| "diagnosed_gout" VARCHAR, | |
| "diagnosed_hay_fever" VARCHAR, | |
| "diagnosed_lupus" VARCHAR, | |
| "diagnosed_other_cancer" VARCHAR, | |
| "diagnosed_skin_cancer" VARCHAR, | |
| "diagnosed_stroke" VARCHAR, | |
| "diagnosed_thyroid_disease" VARCHAR, | |
| "diagnosed_congestive_heart_failure" VARCHAR, | |
| "ethnicity" VARCHAR, | |
| "exam_status" VARCHAR, | |
| "family_sequence" INT, | |
| "family_size" INT, | |
| "fips" VARCHAR, | |
| "grade_completed" VARCHAR, | |
| "household_size" INT, | |
| "health_status" VARCHAR, | |
| "marital_status" VARCHAR, | |
| "bird" VARCHAR, | |
| "cat" VARCHAR, | |
| "dog" VARCHAR, | |
| "fish" VARCHAR, | |
| "other_pet" VARCHAR, | |
| "race" VARCHAR, | |
| "race_ethnicity" VARCHAR, | |
| "gender" VARCHAR, | |
| "birthplace" VARCHAR, | |
| "annual_income_20000" VARCHAR, | |
| "income_group" INT, | |
| "monthly_income_total" INT, | |
| PRIMARY KEY ("id", "age")); | |
| 3) Import data | |
| USE nhanes_ks; | |
| COPY nhanes ("id","num_smokers","age","age_unit","age_months","major_medical_coverage","dental_coverage","routine_medical_coverage","employer_paid_plan","secondary_smoke","county","screening_month","pets","asthma","bronchitis","goiter","hay_fever","thyroid_disease","chronic_bronchitis","diagnosed_asthma","diagnosed_cataracts","diagnosed_emphysema","diagnosed_goiter","diagnosed_gout","diagnosed_hay_fever","diagnosed_lupus","diagnosed_other_cancer","diagnosed_skin_cancer","diagnosed_stroke","diagnosed_thyroid_disease","diagnosed_congestive_heart_failure","ethnicity","exam_status","family_sequence","family_size","fips","grade_completed","household_size","health_status","marital_status","bird","cat","dog","fish","other_pet","race","race_ethnicity","gender","birthplace","annual_income_20000","income_group","monthly_income_total") from './nhanes52.csv'; | |
| 4) Update index | |
| bin/dsetool create_core nhanes_ks.nhanes generateResources=true reindex=true | |
| 5) Run example |
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 <stdio.h> | |
| #include <cassandra.h> | |
| int main(int argc, char* argv[]) { | |
| /* Setup and connect to cluster */ | |
| CassFuture* connect_future = NULL; | |
| CassCluster* cluster = cass_cluster_new(); | |
| CassSession* session = cass_session_new(); | |
| char* hosts = "127.0.0.1"; | |
| if (argc > 1) { | |
| hosts = argv[1]; | |
| } | |
| /* Add contact points */ | |
| cass_cluster_set_contact_points(cluster, hosts); | |
| /* 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 */ | |
| const char* query = "select id, ethnicity FROM nhanes_ks.nhanes WHERE solr_query='ethnicity:\"~Hispanic\"' LIMIT 10;"; | |
| 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)) { | |
| cass_int32_t id; | |
| const char* ethnicity; | |
| size_t ethnicity_length; | |
| const CassRow* row = cass_iterator_get_row(rows); | |
| const CassValue* id_value = cass_row_get_column_by_name(row, "id"); | |
| const CassValue* ethnicity_value = cass_row_get_column_by_name(row, "ethnicity"); | |
| cass_value_get_int32(id_value, &id); | |
| cass_value_get_string(ethnicity_value, ðnicity, ðnicity_length); | |
| printf("id: %d, ethnicity: '%.*s'\n", id, (int)ethnicity_length, ethnicity); | |
| } | |
| cass_result_free(result); | |
| cass_iterator_free(rows); | |
| } 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; | |
| } |
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 <stdio.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); | |
| } | |
| CassError prepare_query(CassSession* session, | |
| const char* query, | |
| const CassPrepared** prepared) { | |
| CassError rc = CASS_OK; | |
| CassFuture* future = NULL; | |
| future = cass_session_prepare(session, query); | |
| cass_future_wait(future); | |
| rc = cass_future_error_code(future); | |
| if (rc == CASS_OK) { | |
| *prepared = cass_future_get_prepared(future); | |
| } else { | |
| print_error(future); | |
| } | |
| cass_future_free(future); | |
| return rc; | |
| } | |
| int main(int argc, char* argv[]) { | |
| /* Setup and connect to cluster */ | |
| CassFuture* connect_future = NULL; | |
| CassCluster* cluster = cass_cluster_new(); | |
| CassSession* session = cass_session_new(); | |
| const CassPrepared* prepared; | |
| char* hosts = "127.0.0.1"; | |
| if (argc > 1) { | |
| hosts = argv[1]; | |
| } | |
| /* Add contact points */ | |
| cass_cluster_set_contact_points(cluster, hosts); | |
| /* 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 */ | |
| const char* query = "select id, ethnicity FROM nhanes_ks.nhanes WHERE solr_query='ethnicity:\"~Hispanic\"' LIMIT 10;"; | |
| if (prepare_query(session, query, &prepared) == CASS_OK) { | |
| CassStatement* statement = cass_prepared_bind(prepared); | |
| 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)) { | |
| cass_int32_t id; | |
| const char* ethnicity; | |
| size_t ethnicity_length; | |
| const CassRow* row = cass_iterator_get_row(rows); | |
| const CassValue* id_value = cass_row_get_column_by_name(row, "id"); | |
| const CassValue* ethnicity_value = cass_row_get_column_by_name(row, "ethnicity"); | |
| cass_value_get_int32(id_value, &id); | |
| cass_value_get_string(ethnicity_value, ðnicity, ðnicity_length); | |
| printf("id: %d, ethnicity: '%.*s'\n", id, (int)ethnicity_length, ethnicity); | |
| } | |
| cass_result_free(result); | |
| cass_iterator_free(rows); | |
| } else { | |
| print_error(result_future); | |
| } | |
| 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