%% Explore and Import Data from Cassandra Database Table
% This example shows how to explore the structure of an Apache Cassandra® database and import data from a Cassandra database table into MATLAB® using a Cassandra database connection with the Apache Cassandra database C++ interface. The Cassandra database stores database tables according to the partition key. The partition key affects how the data is filtered in the database.
% In this example, the Cassandra database contains the employees_by_job database table with employee data and the job_id partition key.
%
%% Create Cassandra Database Connection
% Create a Cassandra database connection using the configured data source CassandraDataSource and a blank user name and password. The apacheCassandra function returns conn as a connection object.

datasource = "CassandraDataSource";
username = "";
password = "";
conn = apacheCassandra(datasource,username,password);

%
%% Explore Cassandra Database Structure
% View the keyspaces in the Cassandra database using the Cassandra database connection. The returned string array contains the keyspaces. A keyspace contains one or more database tables and defines how the database replicates the data in the tables.

conn.Keyspaces

%
% Return the names of the Cassandra database tables in the employeedata keyspace. t is a string array that contains the names of the database tables in the employeedata keyspace.

keyspace = "employeedata";
t = tablenames(conn,keyspace)

%
% Return the names of the Cassandra database columns in the employees_by_job database table.

tablename = "employees_by_job";
cols = columninfo(conn,keyspace,tablename);

%
% Display the first few names of the Cassandra database columns in the employees_by_job database table.

head(cols)

% cols is a table with these variables:
% Name — Cassandra database column name
% DataType — Cassandra Query Language (CQL) data type of the Cassandra database column
% PartitionKey — Partition key indicator
% ClusteringColumn — Clustering column indicator
% The value in the PartitionKey variable indicates whether the database column is a partition key. The column job_id (job identifier) is a partition key in this database table.
%
%% Import Data from Cassandra Database
% Import data from the employees_by_job database table into MATLAB. This database has data about shop clerks, so use the partition key value SH_CLERK.

keyValue = "SH_CLERK";
results = partitionRead(conn,keyspace,tablename, ...
    keyValue);

%
% Display the first few rows of the returned employee data.

head(results)

% results is a table that contains these variables:
% job_id — Job identifier
% hire_date — Hire date
% employee_id — Employee identifier
% commission_pct — Commission percentage
% department_id — Department identifier
% email — Email address
% first_name — First name
% last_name — Last name
% manager_id — Manager identifier
% office — Office location (table that contains two variables for the building and room)
% performance_ratings — Performance ratings
% phone_number — Phone number
% salary — Salary
%
%% Close Cassandra Database Connection

close(conn)

% Copyright 2020 The MathWorks, Inc.