Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding code for cpp lesson part 2 #137

Merged
merged 1 commit into from
Jul 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion cpp/part1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ CREATE TABLE IF NOT EXISTS ks.mutant_data (
PRIMARY KEY((first_name, last_name)));
INSERT INTO ks.mutant_data ("first_name","last_name","address","picture_location") VALUES ('Bob','Loblaw','1313 Mockingbird Lane', 'http://www.facebook.com/bobloblaw');
INSERT INTO ks.mutant_data ("first_name","last_name","address","picture_location") VALUES ('Bob','Zemuda','1202 Coffman Lane', 'http://www.facebook.com/bzemuda');
INSERT INTO ks.mutant_data ("first_name","last_name","address","picture_location") VALUES


### Running the CPP Example
Expand Down
2 changes: 1 addition & 1 deletion cpp/part1/connect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main(int argc, char* argv[]) {

// Add the contact points. These can be either IPs or domain names.
// You can specify more than one, comma-separated, but you don’t have to - driver will discover other nodes by itself. You should do it if you expect some of your contact points to be down.
cass_cluster_set_contact_points(cluster, "172.21.0.2"); // set the IP according to your setup
cass_cluster_set_contact_points(cluster, "172.18.0.2"); // set the IP according to your setup
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems random
If the IP change on every run, why change it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not very important. In the lesson (and in the code comments above) the instructions tell the user to change the IP according to their cluster.
I wanted it to be the same in Part 1 and Part 2 but it would be changed by the user anyhow so it doesn't matter much.


// Connect. `cass_session_connect` returns a pointer to "future"
// Also, this allocates the object pointed to by `connect_future`,
Expand Down
2 changes: 1 addition & 1 deletion cpp/part1/connect_unique.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int main(int argc, char* argv[]) {

// Add the contact points. These can be either IPs or domain names.
// You can specify more than one, comma-separated, but you don’t have to - driver will discover other nodes by itself. You should do it if you expect some of your contact points to be down.
cass_cluster_set_contact_points(cluster.get(), "172.21.0.2"); // set the IP according to your setup
cass_cluster_set_contact_points(cluster.get(), "172.18.0.2"); // set the IP according to your setup


// Connect. `cass_session_connect` returns a pointer to "future"
Expand Down
2 changes: 1 addition & 1 deletion cpp/part1/iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main(int argc, char* argv[]) {

// Add the contact points. These can be either IPs or domain names.
// You can specify more than one, comma-separated, but you don’t have to - driver will discover other nodes by itself. You should do it if you expect some of your contact points to be down.
cass_cluster_set_contact_points(cluster, "172.21.0.2"); // set the IP according to your setup
cass_cluster_set_contact_points(cluster, "172.18.0.2"); // set the IP according to your setup


// Connect. `cass_session_connect` returns a pointer to "future"
Expand Down
2 changes: 1 addition & 1 deletion cpp/part1/query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int main(int argc, char* argv[]) {

// Add the contact points. These can be either IPs or domain names.
// You can specify more than one, comma-separated, but you don’t have to - driver will discover other nodes by itself. You should do it if you expect some of your contact points to be down.
cass_cluster_set_contact_points(cluster, "172.21.0.2"); // set the IP according to your setup
cass_cluster_set_contact_points(cluster, "172.18.0.2"); // set the IP according to your setup


// Connect. `cass_session_connect` returns a pointer to "future"
Expand Down
62 changes: 62 additions & 0 deletions cpp/part2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Scylla CPP Driver
Instructions for using prepared statements and parameterized simple statements. Before running the code you should have a three-node cluster running with the ks.mutant_data table, and some data in it. This example uses the [CPP Driver](https://github.com/datastax/cpp-driver)
More info in the Scylla University Course [Using Scylla Drivers](https://university.scylladb.com/courses/using-scylla-drivers/).

### ### Instructions for setting up a Scylla Cluster from this repo.
```
cd mms
docker-compose up -d
```

Run bash in the node:
```
docker exec -it mms_scylla-node1_1 bash
```

Followed by scylla commands, like
```
> nodetool status
```
or
```
> cqlsh
```

### To manually add the tracking keyspace and data
docker exec -it mms_scylla-node1_1 cqlsh
CREATE KEYSPACE ks WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy','DC1' : 3};
use ks;
CREATE TABLE IF NOT EXISTS ks.mutant_data (
first_name text,
last_name text,
address text,
picture_location text,
PRIMARY KEY((first_name, last_name)));
INSERT INTO ks.mutant_data ("first_name","last_name","address","picture_location") VALUES ('Bob','Loblaw','1313 Mockingbird Lane', 'http://www.facebook.com/bobloblaw');
INSERT INTO ks.mutant_data ("first_name","last_name","address","picture_location") VALUES ('Bob','Zemuda','1202 Coffman Lane', 'http://www.facebook.com/bzemuda');


### Running the CPP Example
The application uses [CPP Driver](https://github.com/datastax/cpp-driver) which is an open-source Scylla driver for CPP. Start by installing the driver, you can read more about installation in the [Scylla University lesson CPP Driver – Part 1](https://university.scylladb.com/courses/using-scylla-drivers/lessons/cpp-driver-part-1/)
To run the prepared statement example change the IP according to the setup of your cluster. Now compile and run the code:
```bash
g++ prepared_statements.cpp -lcassandra -o prepared_statement
./prepared_statement
```

To run the parameterized simple statements example change the IP according to the setup of your cluster. Now compile and run the code:
```bash
g++ param_simple.cpp -lcassandra -o param_simple
./param_simple

```

### Destroying the Scylla Cluster
```
cd mms
docker-compose kill
docker-compose rm -f
```



66 changes: 66 additions & 0 deletions cpp/part2/param_simple.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Compile with: g++ param_simple.cpp [-L <path_to_libcassandra.so> -I <path_to_cassandra.h>] -lcassandra -o param_simple
// Example of parametrized simple query. DB is expected to have some data in `ks.mutant_data`!
#include <cassandra.h>
#include <iostream>

int main(int argc, char* argv[]) {
// Allocate the objects that represent cluster and session. Remember to free them once no longer needed!
CassCluster* cluster = cass_cluster_new();
CassSession* session = cass_session_new();

// Add the contact points. These can be either IPs or domain names.
// You can specify more than one, comma-separated, but you don’t have to - driver will discover other nodes by itself. You should do it if you expect some of your contact points to be down.
cass_cluster_set_contact_points(cluster, "172.18.0.2"); // set the IP according to your setup

// Connect. `cass_session_connect` returns a pointer to "future"
// Also, this allocates the object pointed to by `connect_future`,
// which must be freed manually (see below).
CassFuture* connect_future = cass_session_connect(session, cluster);

// `cass_future_error_code` will block until connected or refused.
if (cass_future_error_code(connect_future) == CASS_OK) {
std::cout << "Connected" << std::endl;

// Fetch data sample from ScyllaDB after the connection is established
const char* query = "SELECT first_name, last_name, address, picture_location FROM ks.mutant_data";
// Parameterized simple statement, not to be confused with prepared statements!
CassStatement* statement = cass_statement_new("SELECT * FROM ks.mutant_data WHERE first_name=? and last_name=?", 2); // `2` is the number of parameters

cass_statement_bind_string(statement, 0, "Bob");
cass_statement_bind_string(statement, 1, "Loblaw");

// Proceed with `statement` as usual
CassFuture* result_future = cass_session_execute(session, statement);

if (cass_future_error_code(result_future) == CASS_OK) {
const CassResult* result = cass_future_get_result(result_future);
const CassRow* row = cass_result_first_row(result);

if (row) {
const CassValue* value = cass_row_get_column_by_name(row, "address");

const char* address;
size_t address_length;
cass_value_get_string(value, &address, &address_length);
std::cout << "Address for Bob Loblaw is: ";
std::cout.write(address, address_length);
std::cout << std::endl;

}

cass_result_free(result);
} else {
// Handle error - omitted for brevity
}

cass_statement_free(statement);
cass_future_free(result_future);
} else {
std::cout << "Connection ERROR" << std::endl;
}

// Release the resources.
cass_future_free(connect_future);
cass_cluster_free(cluster);
cass_session_free(session);
}
57 changes: 57 additions & 0 deletions cpp/part2/prepared_statements.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Compile with: g++ prepared_statements.cpp [-L <path_to_libcassandra.so> -I <path_to_cassandra.h>] -lcassandra -o prepared_statements
// Demonstration of prepared statements. Table `ks.mutant_data` is expected to exist!
#include <cassandra.h>
#include <iostream>

int main(int argc, char* argv[]) {
// Allocate the objects that represent cluster and session. Remember to free them once no longer needed!
CassCluster* cluster = cass_cluster_new();
CassSession* session = cass_session_new();

// Add the contact points. These can be either IPs or domain names.
// You can specify more than one, comma-separated, but you don’t have to - driver will discover other nodes by itself. You should do it if you expect some of your contact points to be down.
cass_cluster_set_contact_points(cluster,"172.18.0.2"); // set the IP according to your setup

// Connect. `cass_session_connect` returns a pointer to "future"
// Also, this allocates the object pointed to by `connect_future`,
// which must be freed manually (see below).
CassFuture* connect_future = cass_session_connect(session, cluster);

// `cass_future_error_code` will block until connected or refused.
if (cass_future_error_code(connect_future) == CASS_OK) {
std::cout << "Connected" << std::endl;

// Imagine we have a lot of mutants to INSERT and we don’t know their addresses nor do we have their pictures
const char* query = "INSERT INTO ks.mutant_data (first_name, last_name, address, picture_location) VALUES (?, ?, 'unknown', 'no picture');"; // Note the question marks

CassFuture* prep_future = cass_session_prepare(session, query); // Send the “templated” query to Scylla to “compile” it.
cass_future_wait(prep_future); // Preparing (“compiling”) the query is an async operation.
const CassPrepared* prepared = cass_future_get_prepared(prep_future); // This object will be reused for every similar INSERT
cass_future_free(prep_future);

// Now this code block can be repeated, effectively performing similar INSERTs - with various values in places of question marks
{
CassStatement* bound_statement = cass_prepared_bind(prepared);
cass_statement_bind_string(bound_statement, 0, "Aaron"); // Fill in the first question mark
cass_statement_bind_string(bound_statement, 1, "Goldstein"); // Fill in the second question mark

// Proceed as usual:
CassFuture* exec_future = cass_session_execute(session, bound_statement);
cass_future_wait(exec_future);

std::cout << "Inserting data using Prepared Statement" << std::endl;

cass_future_free(exec_future);
cass_statement_free(bound_statement);
}

cass_prepared_free(prepared);
} else {
std::cout << "Connection ERROR" << std::endl;
}

// Release the resources.
cass_future_free(connect_future);
cass_cluster_free(cluster);
cass_session_free(session);
}