%% Export MATLAB Data into Cassandra Database
% This example shows how to export data from a MATLAB® table into an Apache Cassandra® database using a Cassandra database connection with the Apache Cassandra database C++ interface.
% In this example, the Cassandra database includes the employees_by_job database table, which contains 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 Data from Cassandra Database
% Return the names of the Cassandra database tables in the employeedata keyspace. t is a string array that contains the names of these tables.

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

%
% Import employee data into MATLAB from the employees_by_job table in the employeedata keyspace by using the Cassandra database connection.

keyspace = "employeedata";
tablename = "employees_by_job";
results = partitionRead(conn,keyspace,tablename);

% Display the last few rows of the imported employee data.

tail(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
%
% Display the CQL data types of the columns in the employees_by_job database table.

cols = columninfo(conn,keyspace,tablename);
cols(:,1:2)

%
%% Insert Data from MATLAB into Cassandra Database
% Create a table of data representing one employee to insert into the Cassandra database. Specify the names of the variables. Create a table for the office information. Then, create a table with the employee information that contains the nested table of office information. Set the names of the variables.

varnames = ["job_id" "hire_date" "employee_id" ...
    "commission_pct" "department_id" "email" "first_name" ...
    "last_name" "manager_id" "office" "performance_ratings" ... 
    "phone_number" "salary"];
office = table("South",160, ...
    'VariableNames',["building" "room"]);
data = table("IT_ADMIN",datetime('today'),301,0.25,30,"SMITH123", ...
    "Alex","Smith",114,office,{[4 5]},"515.123.2345",3000);
data.Properties.VariableNames = varnames;

% Insert the employee information into the Cassandra database.

upsert(conn,keyspace,tablename,data)

% Display the inserted data by importing it into MATLAB using the partition key IT_ADMIN. The employees_by_job table contains a new row.

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

%
%% Update Data in Cassandra Database
% Update the email variable in the new row of employee information.

results.email = "SMITH456";
upsert(conn,keyspace,tablename,results)

% Display the updated data by importing it into MATLAB. The row contains the updated data in the email variable of the employees_by_job table.

results = partitionRead(conn,keyspace,tablename,keyValue)

%
%% Close Cassandra Database Connection

close(conn)

% Copyright 2020 The MathWorks, Inc.