Learn how to set up a single-node Cassandra environment for development, including installation, configuration, and verification using cqlsh.
Apache Cassandra is a highly scalable, distributed NoSQL database designed to handle large amounts of data across many commodity servers, providing high availability with no single point of failure. For developers, setting up a single-node Cassandra instance on a local machine is an excellent way to get familiar with its capabilities and begin integrating it with Clojure applications. This section will guide you through the installation, configuration, and verification of a single-node Cassandra setup for development purposes.
Before we begin, ensure that your system meets the following prerequisites:
Visit the Apache Cassandra website: Go to the Apache Cassandra download page to get the latest stable release.
Choose the appropriate version: Download the binary tarball for your operating system. As of this writing, the latest stable version is Cassandra 4.0.
Extract the tarball: Once downloaded, extract the tarball to a directory of your choice. You can use the following command in your terminal:
tar -xvzf apache-cassandra-4.0-bin.tar.gz
To make Cassandra commands accessible from anywhere in your terminal, add the Cassandra bin
directory to your system’s PATH.
Open your shell configuration file: This could be .bashrc
, .bash_profile
, or .zshrc
, depending on your shell.
Add the following line:
export PATH=$PATH:/path/to/apache-cassandra-4.0/bin
Apply the changes:
source ~/.bashrc # or source ~/.zshrc
Cassandra’s configuration is primarily managed through the cassandra.yaml
file located in the conf
directory of your Cassandra installation. This file contains numerous parameters that control the behavior of your Cassandra instance.
Cluster Name: Although you’re setting up a single-node instance, it’s good practice to set a unique cluster name.
cluster_name: 'MyCassandraCluster'
Data Directories: Specify directories for data, commit logs, and caches. Ensure these directories have the necessary permissions.
data_file_directories:
- /var/lib/cassandra/data
commitlog_directory: /var/lib/cassandra/commitlog
saved_caches_directory: /var/lib/cassandra/saved_caches
Network Interfaces: Configure the listen address and RPC address. For a local setup, these can be set to localhost
.
listen_address: localhost
rpc_address: localhost
Seeds: In a multi-node setup, seeds are used to bootstrap the gossip protocol. For a single-node setup, set the seed to localhost
.
seed_provider:
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
- seeds: "127.0.0.1"
JVM Options: Cassandra’s performance can be tuned by adjusting JVM options in the jvm.options
file. For development, the default settings are usually sufficient.
cassandra.yaml
file before making changes.With Cassandra installed and configured, you can now start the Cassandra service.
Navigate to the Cassandra directory:
cd /path/to/apache-cassandra-4.0
Start Cassandra:
bin/cassandra -f
The -f
flag runs Cassandra in the foreground, which is useful for development and debugging.
Verify the Startup: Check the logs in logs/system.log
for any errors or warnings. A successful startup will end with a message indicating that the server is ready to accept client connections.
Cassandra Query Language Shell (cqlsh) is an interactive command-line interface for executing CQL commands.
Launch cqlsh:
bin/cqlsh
If Cassandra is running correctly, you should see a prompt like this:
Connected to MyCassandraCluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 4.0.0 | CQL spec 3.4.5 | Native protocol v4]
Use HELP for help.
cqlsh>
Create a Keyspace: Keyspaces are analogous to databases in relational systems.
CREATE KEYSPACE test_keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
Create a Table:
CREATE TABLE test_keyspace.users (
user_id UUID PRIMARY KEY,
name TEXT,
email TEXT
);
Insert Data:
INSERT INTO test_keyspace.users (user_id, name, email) VALUES (uuid(), 'John Doe', 'john.doe@example.com');
Query Data:
SELECT * FROM test_keyspace.users;
This should return the data you just inserted, verifying that your Cassandra setup is functioning correctly.
cassandra.yaml
have the correct permissions.jvm.options
.With Cassandra up and running, you can now begin integrating it with your Clojure applications. Libraries such as clojurewerkz/cassaforte provide idiomatic Clojure interfaces for interacting with Cassandra.
Add Cassaforte to your project dependencies:
[clojurewerkz/cassaforte "3.0.0"]
Connect to Cassandra:
(require '[qbits.alia :as alia])
(def session (alia/connect {:contact-points ["127.0.0.1"]}))
Execute Queries:
(alia/execute session "SELECT * FROM test_keyspace.users")
Setting up a single-node Cassandra instance on your local machine is a straightforward process that provides a solid foundation for developing and testing Clojure applications with Cassandra. By following this guide, you should now have a functioning Cassandra environment ready for further exploration and integration.
For more advanced configurations and optimizations, consider exploring multi-node clusters and Cassandra’s extensive documentation on performance tuning and data modeling.