Import Data from Cassandra Database Table Using CQL

This example shows how to import data from an Apache Cassandra® database table into MATLAB® using the Cassandra Query Language (CQL) and a Cassandra database connection.

Use the executecql function to execute CQL queries that filter by clustering columns or limit rows in the query results, for example. Also, you can use the executecql function to write non-SELECT CQL statements. For easy data import using the partition key values of a Cassandra database table, use the partitionRead function instead.

In this example, the Cassandra database contains the employees_by_job database table with employee data and the job_id partition key. The hire_date database column is a clustering column.

To run this example, you must first install the Database Toolbox™ Interface for Apache Cassandra® Database. For details, see Database Toolbox Interface for Apache Cassandra Database Installation.

Create a Cassandra database connection using the local host address. conn is a cassandra object.

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

Write a CQL query that selects all employees who are programmers or shop clerks, and retrieves their job identifiers, hire dates, and email addresses. Filter the query by those employees hired before April 30, 2006 using the hire_date clustering column. Limit the returned data to four rows.

query = strcat("SELECT job_id,hire_date,email ", ... 
    "FROM employeedata.employees_by_job ", ...
    "WHERE job_id IN ('IT_PROG','SH_CLERK') ", ...
    "AND hire_date < '2006-04-30'", ...
    "LIMIT 4;");

Execute the CQL query using the Cassandra database connection and display the results.

results = executecql(conn,query)
results=4×3 table
      job_id       hire_date       email   
    __________    ___________    __________

    "IT_PROG"     05-Feb-2006    "VPATABAL"
    "IT_PROG"     03-Jan-2006    "AHUNOLD" 
    "IT_PROG"     25-Jun-2005    "DAUSTIN" 
    "SH_CLERK"    24-Apr-2006    "AWALSH"  

results is a table with the job_id, hire_date, and email variables. The hire_date variable is a datetime array and the job_id and email variables are string arrays.

Close the Cassandra database connection.

close(conn)

See Also

| |

Related Topics

External Websites