I’ve got curious about Cassandra and more and more I’ve been asking myself about the use cases it could be the most valuable solution for. With all the answers out there, I simply needed a place where I could dump the findings to ultimately build something useful as a single page (and perhaps ditch it to create something of a higher value).
So here they are, loose notes about Cassandra to understand the value propositions of the database and where it could fit well. Should I ever be faced the question of using Cassandra or not, I may some day find an answer here (or know where to look for it).
My story with Cassandra began with the Spark Cassandra Connector project that enables Apache Spark to use Cassandra to have lightning-fast cluster computing with Spark and Cassandra.
The latest version of Cassandra is 2.1.0.
Step 1. Installation
I installed Cassandra using brew
on Mac OS X. It was a mere brew install cassandra
and took a few secs.
➜ ~ brew info cassandra
cassandra: stable 2.1.0
http://cassandra.apache.org
/usr/local/Cellar/cassandra/2.1.0 (3899 files, 92M) *
Built from source
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/cassandra.rb
==> Caveats
If you plan to use the CQL shell (cqlsh), you will need the Python CQL library
installed. Since Homebrew prefers using pip for Python packages, you can
install that using:
pip install cql
To have launchd start cassandra at login:
ln -sfv /usr/local/opt/cassandra/*.plist ~/Library/LaunchAgents
Then to load cassandra now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.cassandra.plist
Execute sudo pip install cql
to install the Python packages.
➜ ~ sudo pip install cql
Downloading/unpacking cql
Downloading cql-1.4.0.tar.gz (76kB): 76kB downloaded
Running setup.py (path:/private/tmp/pip_build_root/cql/setup.py) egg_info for package cql
Requirement already satisfied (use --upgrade to upgrade): thrift in /Library/Python/2.7/site-packages (from cql)
Installing collected packages: cql
Running setup.py install for cql
Successfully installed cql
Cleaning up...
That should be all needed to get you going.
Step 2. Running the server
Start an instance using cassandra -f
.
➜ ~ cassandra -f
objc[43878]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/Current/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
INFO 22:49:39 Hostname: japila.local
INFO 22:49:39 Loading settings from file:/usr/local/etc/cassandra/cassandra.yaml
...
INFO 22:47:52 Using Netty Version: [netty-buffer=netty-buffer-4.0.20.Final.1709113, netty-codec=netty-codec-4.0.20.Final.1709113, netty-codec-http=netty-codec-http-4.0.20.Final.1709113, netty-codec-socks=netty-codec-socks-4.0.20.Final.1709113, netty-common=netty-common-4.0.20.Final.1709113, netty-handler=netty-handler-4.0.20.Final.1709113, netty-transport=netty-transport-4.0.20.Final.1709113, netty-transport-rxtx=netty-transport-rxtx-4.0.20.Final.1709113, netty-transport-sctp=netty-transport-sctp-4.0.20.Final.1709113, netty-transport-udt=netty-transport-udt-4.0.20.Final.1709113]
INFO 22:47:52 Starting listening for CQL clients on localhost/127.0.0.1:9042...
INFO 22:47:52 Binding thrift service to localhost/127.0.0.1:9160
INFO 22:47:52 Listening for thrift clients...
Step 3. Using cqlsh
In another terminal, start the Cassandra shell using cqlsh
:
➜ ~ cqlsh
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 2.1.0 | CQL spec 3.2.0 | Native protocol v3]
Use HELP for help.
cqlsh>
You may face the issue Why does cqlsh fail with LookupError: unknown encoding? that can be easily solved with setting up LC_ALL
appropriately. Mine’s pl_pl.utf-8
:
export LC_ALL=pl_pl.utf-8
Copying Step 4: Using cqlsh from the official Getting Started documentation:
CREATE KEYSPACE mykeyspace
WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
USE mykeyspace;
CREATE TABLE users (
user_id int PRIMARY KEY,
fname text,
lname text
);
INSERT INTO users (user_id, fname, lname)
VALUES (1745, 'john', 'smith');
INSERT INTO users (user_id, fname, lname)
VALUES (1744, 'john', 'doe');
INSERT INTO users (user_id, fname, lname)
VALUES (1746, 'john', 'smith');
SELECT * FROM users;