%% Update Data in Cassandra Database Using Consistency Level
% Update data in a Cassandra database with MATLAB® data, and display the updated data by using a Cassandra database connection. Specify a consistency level for the write operation.
% The Cassandra database includes the employees_by_job database table, which contains employee data and the job_id partition key.
%
% Create a Cassandra database connection using the local host address. conn is a cassandra object.

contactPoints = "localhost";
conn = cassandra(contactPoints);

%
% 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 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)

%
% Import the data to update by using the partitionRead function with the partition key value MK_REP. The data is for an employee who is a marketing representative.

keyValue = "MK_REP";
data = partitionRead(conn,keyspace,tablename,keyValue)

%
% Update the commission percentage to 0.25 for the marketing representative. Also, specify the consistency level "QUORUM" to ensure that most replica nodes commit the write operation.

data.commission_pct = 0.25;
level = "QUORUM";
upsert(conn,keyspace,tablename,data,'ConsistencyLevel',level)

%
% Display the updated data by importing it into MATLAB using the partition key value MK_REP. The updated commission percentage for the marketing representative is 0.25.

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

%
% Close the Cassandra database connection.

close(conn)

% Copyright 2019 The MathWorks, Inc.