Getting Started with Cassandra DB on Windows

dazevedo

Daniel Azevedo

Posted on October 1, 2024

Getting Started with Cassandra DB on Windows

Hi devs,

Cassandra is a powerful NoSQL database, built for scalability and high availability, which makes it great for applications that handle large amounts of data across multiple locations. In this guide, we’ll go through the setup process on Windows and cover some common issues you might face, along with solutions.

Why Cassandra?

Before we dive into the setup, let’s quickly recap why Cassandra is a popular choice for modern applications:

  • Horizontal scalability: Easily scale your database by adding more nodes.
  • Fault tolerance: There’s no single point of failure, making it perfect for highly available applications.
  • Decentralized architecture: Each node in a Cassandra cluster is identical, distributing the load and avoiding bottlenecks.

Now, let’s get it up and running on Windows.


Step 1: Installing Cassandra on Windows

Download and Install Java (JDK)

Before installing Cassandra, you need to install the Java Development Kit (JDK), since Cassandra runs on Java. Follow these steps:

  1. Download the JDK from Oracle's official site (JDK 11 is recommended).
  2. Install the JDK and ensure it's set up correctly by running the following command in your terminal (PowerShell or Command Prompt):
   java -version
Enter fullscreen mode Exit fullscreen mode

This should return the installed Java version, indicating that it’s properly installed.


Download and Install Apache Cassandra

  1. Download the latest version of Apache Cassandra from the official download page.

  2. Extract the contents of the ZIP file to a folder (e.g., C:\Cassandra).

  3. Navigate to the extracted folder in PowerShell or Command Prompt.

  4. Start Cassandra by running:

   bin\cassandra.bat
Enter fullscreen mode Exit fullscreen mode

This will launch the Cassandra server. You should see logs appearing in the terminal as Cassandra starts up.


Step 2: Connecting to Cassandra with CQLSH

Cassandra uses CQLSH (Cassandra Query Language Shell) to interact with the database. After starting Cassandra, open another terminal and navigate to the Cassandra installation folder.

Run the following command to open the CQLSH:

bin\cqlsh
Enter fullscreen mode Exit fullscreen mode

You should see something like this:

Connected to Test Cluster at 127.0.0.1:9042.
cqlsh>
Enter fullscreen mode Exit fullscreen mode

If you see this prompt, congratulations! You’re successfully connected to Cassandra and ready to start using it.


Step 3: Creating a Keyspace and Table

Let’s create a keyspace, which is like a database in relational systems, and a table for storing employee salary data.

Create a Keyspace

In the CQL shell, create a keyspace named salary_processing:

CREATE KEYSPACE salary_processing
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};
Enter fullscreen mode Exit fullscreen mode

This command sets up a simple keyspace with one replica, useful for local development.

Create a Table

Next, create a table to store employee salary data:

USE salary_processing;

CREATE TABLE employee_salaries (
    employee_id UUID PRIMARY KEY,
    name text,
    department text,
    salary decimal,
    payment_date timestamp
);
Enter fullscreen mode Exit fullscreen mode

This table will store employee information such as their ID, name, department, salary, and payment date.


Step 4: Inserting and Querying Data

Inserting Data

Let’s insert some employee records:

INSERT INTO employee_salaries (employee_id, name, department, salary, payment_date)
VALUES (uuid(), 'Alice', 'Engineering', 8500.00, toTimestamp(now()));

INSERT INTO employee_salaries (employee_id, name, department, salary, payment_date)
VALUES (uuid(), 'Bob', 'HR', 6200.00, toTimestamp(now()));
Enter fullscreen mode Exit fullscreen mode

Each time you use uuid(), a new unique ID is generated for the employee.

Querying Data

You can retrieve all records from the table like this:

SELECT * FROM employee_salaries;
Enter fullscreen mode Exit fullscreen mode

The output will display all the employee salary data.


Step 5: Common Issues and How to Solve Them

1. Cassandra Won’t Start (Java Issues)

If Cassandra fails to start, it’s often because the Java version is either not installed or not correctly configured.

Solution:

  • Ensure that you’ve installed JDK 11 or higher.
  • Set the JAVA_HOME environment variable correctly:
    1. Right-click on This PC > Properties > Advanced system settings > Environment Variables.
    2. Under System variables, click New, then enter JAVA_HOME as the name and the path to your JDK installation (e.g., C:\Program Files\Java\jdk-11) as the value.

Restart your computer and try running Cassandra again.

2. Firewall or Port Issues

Cassandra communicates over port 9042, which can sometimes be blocked by firewalls.

Solution:

  • Make sure port 9042 is open. You can add an exception in Windows Firewall for Cassandra.
  • To test, run the following command:
  telnet 127.0.0.1 9042
Enter fullscreen mode Exit fullscreen mode

If the connection fails, it’s likely a firewall issue.

3. CQLSH Connection Refused

If you get a "connection refused" error when running cqlsh, it could be due to a misconfigured IP address.

Solution:

  • Make sure Cassandra is bound to the correct IP. Open the cassandra.yaml file located in the conf directory of your installation and check the listen_address setting.
    • Set listen_address: 127.0.0.1 for local installations.
    • Restart Cassandra after making any changes.

Final Thoughts

Setting up Cassandra on Windows can seem tricky at first, but once you get the hang of it, you'll have a powerful tool for managing large datasets across distributed systems. Whether you're building scalable microservices or working with big data, Cassandra's decentralized architecture makes it a robust choice.

Keep coding ;)

💖 💪 🙅 🚩
dazevedo
Daniel Azevedo

Posted on October 1, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related