Using Liquibase with Cassandra

Apache Cassandra is an open source, distributed, NoSQL database. It presents a partitioned wide column storage model with consistent semantics.

Note: For more information, see the Apache Cassandra page.

Supported Versions

  • 3.11

Prerequisites

Before using Liquibase and Cassandra, ensure you have completed the following:

Driver Information

To use Liquibase and Apache Cassandra, you need to have two jar files – JDBC and the Liquibase Cassandra extension:

  1. Ensure you downloaded the Simba JDBC jar driver file. To download it, go to the DataStax ODBC/JDBC Drivers page and select Simba JDBC Driver for Apache Cassandra from the dropdown menu. Also, select the default package option unless you need a specific package. The driver downloads as a .zip file named SambacassandraJDBC43-x.x.x.zip.
  2. Extract the CassandraJDBCxx.jar file and place it in the liquibase/lib directory.
  3. Go to the liquibase-cassandra repository and download the latest released Liquibase extension .jar file.
  4. Place the liquibase-cassandra-version.jar file in the liquibase/lib install directory.
  5. Open your liquibase.properties file and specify the driver as follows:
  6. driver: com.simba.cassandra.jdbc42.Driver

    Note: If you put the CassandraJDBCxx.jar and liquibase-cassandra-version.jar files in any other directory, specify the path to them in the liquibase.properties file: classpath:../path_to_drivers/CassandraJDBCxx.jar; liquibase-cassandra-version.jar.

If you use Maven, you also need to download the Simba JDBC driver for Cassandra and put the driver in a location that your Maven build can access. Configure the Maven pom.xml file to use the local copy of the driver jar file. For example:

<dependency>
<groupId>com.datastax.jdbc</groupId>
<artifactId>CassandraJDBC42</artifactId>
<version>4.2</version>
<scope>system</scope>
<systemPath>${basedir}/lib/CassandraJDBC42.jar</systemPath>
</dependency>

You need to specify that the scope is system and provide the systemPath in pom.xml. In the example, the ${basedir}/lib is the location of the driver jar file.

Testing Your Connection

For Liquibase and Apache Cassandra to work, you need to:

  1. Ensure your Cassandra database is configured. If you have Cassandra tools locally and want to check the status of Cassandra, run $ bin/nodetool status. The status column in the output should report UN which stands for “Up/Normal”:
  2. # nodetool status
    Datacenter: datacenter1
    =======================
    Status=Up/Down
    |/ State=Normal/Leaving/Joining/Moving
    -- Address Load Tokens Owns (effective) Host ID Rack
    UN 172.18.0.6 198.61 KiB 276 100.0% 5rtc74d1-237f-87c0-88eb-72643bd0a8t7 rack1

    Note: For more information, see the Installing Cassandra documentation.

  1. Specify the database URL in your liquibase.properties file as follows:
  2. url: jdbc:cassandra://localhost:9042/myKeyspace;DefaultKeyspace=myKeyspace

    Tip: If you already have a Liquibase Pro key and want to apply it to your project, add the following property to your liquibase.properties file: liquibaseProLicenseKey: <paste license key>.

  3. Create a text file called changelog (.xml, .sql, .json, or .yaml) in your project directory and add a changeset.
  1. Run the status command to see whether the connection is successful. Specify the name of the changelog you created in place of changelog.xml. Also, to run Liquibase commands, you need to specify your username and password in the liquibase.properties file or on the command line. However, the username and password attributes are not required for connections and systems which use alternate means of authentication.
  2. liquibase --username=test --password=test --changeLogFile=changelog.xml status

    Note: Alternatively, you can add the changeLogFile attribute to your liquibase.properties file.

  1. Run your first update with the Liquibase update command, which will make changes to your database:
  2. liquibase --changeLogFile=changelog.xml update

After your first update, you will see a new table along with the DATABASECHANGELOG and DATABASECHANGELOGLOCK tables added to a keyspace:

  • DATABASECHANGELOG table. This table keeps a record of all the changesets that were deployed. When you deploy, the changesets in the changelog are compared with the DATABASECHANGELOG tracking table, and only the new changesets that were not found in the DATABASECHANGELOG will be deployed.
  • DATABASECHANGELOGLOCK table. This table is used internally by Liquibase to manage access to the DATABASECHANGELOG table during deployment and ensure only one instance of Liquibase is updating the database at a time, whether that is creating, updating or deleting changes.

Supported Commands and Change Types

The supported Change Types for Cassandra are:

  • createTable
  • addColumn
  • dropColumn
  • dropTable
  • renameColumn

You can use all the commands with Liquibase and Cassandra, except for:

  • diff
  • diff JSON
  • diffChangeLog
  • generateChangeLog
  • snapshot

Related Links