Created
August 6, 2023 06:37
-
-
Save yskooo/82e7854de699a4cc22fdbc6698383cf6 to your computer and use it in GitHub Desktop.
Basic CRUD app with C + Apache Cassandra with explanations for beginners
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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <cassandra.h> | |
// Define a structure to represent user information | |
struct Users_ { | |
const char* lastname; | |
coss_int32_t age; | |
const char* city; | |
const char* email; | |
const char* firstname; | |
} | |
typedef struct Users_ Users; | |
// Function to create a Cassandra cluster with the specified hosts | |
CassCluster* create_cluster(const char* hosts) { | |
CassCluster* cluster = cass_cluster_new(); | |
cass_cluster_set_contact_points(cluster, hosts); | |
return cluster; | |
} | |
// Function to connect to a Cassandra session with a specific keyspace | |
CassError connect_session(CassSession* session, const CassCluster* cluster, const char* keyspace) { | |
CassError rc = CASS_OK; | |
CassFuture* future = cass_session_connect_keyspace(session, cluster, keyspace); | |
cass_future_wait(future); | |
rc = cass_future_error_code(future); | |
if (rc != CASS_OK) { | |
print_error(future); | |
} | |
cass_future_free(future); | |
return rc; | |
} | |
// Funciton to insert a new user into the "users" table | |
CassError insert_user(CassSession* session, const Users* users) { | |
CassError rc = CASS_OK; | |
CassStatement* statement = NULL; | |
CassFuture* future = NULL; | |
const char* query = "INSERT INTO users (lastname, age, city, email, firstname) VALUES (?, ?, ?, ?, ?)"; | |
statement = cass_statement_new(query, 5); | |
cass_statement_bind_string(statement, 0, users->lastname); | |
cass_statement_bind_int32(statement, 1, users->age); | |
cass_statement_bind_string(statement, 2, users->city); | |
cass_statement_bind_string(statement, 3, users->email); | |
cass_statement_bind_string(statement, 4, users->firstname); | |
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; | |
} | |
// Function to select and print user information based on their lastname | |
CassError select_user(CassSession* session, const char* lastname) { | |
CassError rc = CASS_OK; | |
CassStatement* statement = NULL; | |
CassFuture* future = NULL; | |
const char* query = "SELECT * FROM users WHERE lastname=?"; | |
statement = cass_statement_new(query, 1); | |
cass_statement_bind_string(statement, 0, lastname); | |
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); | |
const CassRow* row = cass_result_first_row(result); | |
if (row) { | |
const char *firstname = NULL; | |
size_t firstname_length = 0; | |
cass_int32_t age = 0; | |
cass_value_get_string(cass_row_get_column_by_name(row, "firstname"), &firstname, | |
&firstname_length); | |
cass_value_get_int32(cass_row_get_column_by_name(row, "age"), &age); | |
printf("firstname: '%.*s' age: %d\n", (int)firstname_length, | |
firstname, age); | |
} | |
cass_result_free(result); | |
} | |
cass_statement_free(statement); | |
cass_future_free(future); | |
return rc; | |
} | |
// Function to update the age of a user based on their lastname | |
CassError update_user_age(CassSession* session, const char* lastname, cass_int32_t age) { | |
CassError rc = CASS_OK; | |
CassStatement* statement = NULL; | |
CassFuture* future = NULL; | |
const char* query = "UPDATE users SET age =? WHERE lastname =?"; | |
statement = cass_statement_new(query, 2); | |
cass_statement_bind_int32(statement, 0, age); | |
cass_statement_bind_string(statement, 1, lastname); | |
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; | |
} | |
// Function to delete a user based on their lastname | |
CassError delete_user(CassSession* session, const char* lastname) { | |
CassError rc = CASS_OK; | |
CassStatement* statement = NULL; | |
CassFuture* future = NULL; | |
const char* query = "DELETE FROM users WHERE lastname=?"; | |
statement = cass_statement_new(query, 1); | |
cass_statement_bind_string(statement, 0, lastname); | |
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 = "127.0.0.1"; | |
char* keyspace = "demo"; | |
Users input = { "Jones", 35, "Austin", "[email protected]", "Bob" }; | |
if (argc > 1) { | |
hosts = argv[1]; | |
} | |
cluster = create_cluster(hosts); | |
// Connect to the Cassandra cluster and keyspace | |
if (connect_session(session, cluster, keyspace) != CASS_OK) { | |
cass_cluster_free(cluster); | |
cass_session_free(session); | |
return -1; | |
} | |
// Perform CRUD operations on the "users" table | |
insert_user(session, &input); | |
select_user(session, "Jones"); | |
update_user_age(session, "Jones", 36); | |
select_user(session, "Jones"); | |
delete_user(session, "Jones"); | |
// Clean up resources | |
cass_cluster_free(cluster); | |
cass_session_free(session); | |
return 0; | |
} | |
// Feel free to pull request if you think there are some errors! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment