Ready to get started?

Integrate with Cassandra Data using Apache Camel

Create a simple Java app that uses Apache Camel routing and the CData JDBC Driver to copy Cassandra data to a JSON file on disk.

Apache Camel is an open source integration framework that allows you to integrate various systems consuming or producing data. When paired with the CData JDBC Driver for Cassandra, you can write Java apps that use Camel routes that integrate with live Cassandra data. This article walks through creating an app in NetBeans that connects, queries, and routes Cassandra data to a JSON file.

With built-in optimized data processing, the CData JDBC Driver offers unmatched performance for interacting with live Cassandra data. When you issue complex SQL queries to Cassandra, the driver pushes supported SQL operations, like filters and aggregations, directly to Cassandra and utilizes the embedded SQL engine to process unsupported operations client-side (often SQL functions and JOIN operations). Its built-in dynamic metadata querying allows you to work with and analyze Cassandra data using native data types.

Creating A New Maven/Java Project

Follow the steps below to create a new Java project and add the appropriate dependencies:

Open NetBeans and create a new project.

Select Maven from the categories list and Java Application from the projects list, then click Next.

Name the project (and adjust any other properties) and click Finish.

In the source package, create a new Java class (we used App.java for this article) and add the main method to the class.

Adding Project Dependencies

With the project created, we can start adding the dependencies needed to work with live Cassandra data from our App. If you have not already done so, intall Maven in your environment, as it is required to add the JAR file for the CData JDBC Driver to your project.

Installing the CData JDBC Driver for Cassandra with Maven

Download the CData JDBC Driver for Cassandra installer, unzip the package, and run the JAR file to install the driver.

Once the JDBC Driver is installed, we can add dependencies to our project. To add a dependency, you can either edit the pom.xml file or right-click the dependencies folder and click Add Dependency. The properties for each dependency follow, but you can search through the available libraries by typing the name of the dependency in the Query box in the Add Dependency wizard.

Required Dependencies

Dependency

Group ID

Artifact ID

Version

camel-core

org.apache.camel

camel-core

3.0.0

camel-jackson

org.apache.camel

camel-jackson

3.0.0

camel-jdbc

org.apache.camel

camel-jdbc

3.0.0

camel-jsonpath

org.apache.camel

camel-jsonpath

3.0.0

cdata-cassandra-connector

org.cdata.connectors

cdata-salesforce-connector

19

commons-dbcp2

org.apache.commons

commons-dbcp2

2.7.0

slf4j-log4j12

org.slf4j

slf4j-log4j12

1.7.30

log4j

org.apache.logging.log4j

log4j

2.12.1

Accessing Cassandra Data in Java Apps with Camel

After adding the required dependencies, we can use the Java DSL (Domain Specific Language) to create routes with access to live Cassandra data. Code snippets follow. Download the sample project (zip file) to follow along (make note of the TODO comments).

Then in the main method, we configure logging, create a new BasicDataSource and add it to the registry, create a new CamelContext, and finally add a route to the context. In this sample, we route Cassandra data to a JSON file.

Configure Logging

BasicConfigurator.configure();

Create a BasicDataSource

Create a BasicDataSource and set the driver class name (cdata.jdbc.salesforce.SalesforceDriver) and URL (using the required connection properties).

Set the Server, Port, and Database connection properties to connect to Cassandra. Additionally, to use internal authentication set the User and Password connection properties.

Add the BasicDataSource to the Registry and Create a CamelContext

Add Routing to the CamelContext

The routing below uses a timer component to run one time and passes a SQL query to the JDBC Driver. The results are marshaled as JSON (and formatted for pretty print) and passed to a file component to write to disk as a JSON file.

Managing the CamelContext Lifecycle

With the route defined, start the CamelContext to begin the lifecycle. In this example, we wait 10 seconds and then shut down the context.

context.start();
Thread.sleep(10000);
context.stop();

Free Trial, Sample Project & Technical Support

Now, you have a working Java application that uses Camel to route data from Cassandra to a JSON file. Download a free, 30-day trial of the CData JDBC Driver for Cassandra and the sample project (make note of the TODO comments) and start working with your live Cassandra data in Apache Camel. Reach out to our Support Team if you have an questions.

This website stores cookies on your computer. These cookies are used to collect information about how you interact with our website and allow us to remember you. We use this information in order to improve and customize your browsing experience and for analytics and metrics about our visitors both on this website and other media. To find out more about the cookies we use, see our Privacy Policy.