Install Cassandra on Ubuntu 18.04 LTS (bionic)
Follow the steps below to install the latest Apache Cassandra 3.11 release: 3.11.4 released on 2019-02-11.
TL;DR
{
sudo apt update
sudo apt install openjdk-8-jdk -y
sudo apt install python-minimal -y
echo "deb http://www.apache.org/dist/cassandra/debian 311x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
curl https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add -
sudo apt update
sudo apt-key adv --keyserver pool.sks-keyservers.net --recv-key A278B781FE4B2BDA
sudo apt update
sudo apt install cassandra -y
}
- Verify that Cassandra is running by invoking
nodetool statusfrom the command line.
nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 127.0.0.1 132.62 KiB 256 100.0% 029e3a8b-2ffc-4683-8353-1ed7a2141706 rack1
Prerequisites
Ubuntu 18.04 LTS (bionic)
Check your Ubuntu version with the following command
lsb_release -dc
Description: Ubuntu 18.04.2 LTS
Codename: bionic
Java 8
The latest version of Java 8, either the Oracle Java Standard Edition 8 or OpenJDK 8.
{
sudo apt update
sudo apt install openjdk-8-jdk -y
}
To verify that you have the correct version of java installed, type java -version.
java -version
openjdk version "1.8.0_222"
OpenJDK Runtime Environment (build 1.8.0_222-8u222-b10-1ubuntu1~18.04.1-b10)
OpenJDK 64-Bit Server VM (build 25.222-b10, mixed mode)
Python 2.7
For using cqlsh, the latest version of Python 2.7.
sudo apt install python-minimal -y
To verify that you have the correct version of Python installed, type python --version.
python --version
Python 2.7.15+
Installation from Debian packages
Add the Apache repository of Cassandra to /etc/apt/sources.list.d/cassandra.sources.list for the latest 3.11 version:
echo "deb http://www.apache.org/dist/cassandra/debian 311x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
deb http://www.apache.org/dist/cassandra/debian 311x main
Add the Apache Cassandra repository keys:
curl https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add -
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 244k 100 244k 0 0 220k 0 0:00:01 0:00:01 --:--:-- 220k
OK
Update the repositories:
sudo apt update
Then add the public key A278B781FE4B2BDA as follows:
sudo apt-key adv --keyserver pool.sks-keyservers.net --recv-key A278B781FE4B2BDA
Executing: /tmp/apt-key-gpghome.YcImG8znk3/gpg.1.sh --keyserver pool.sks-keyservers.net --recv-key A278B781FE4B2BDA
gpg: key A278B781FE4B2BDA: 28 signatures not checked due to missing keys
gpg: key A278B781FE4B2BDA: "Michael Shuler <michael@pbandjelly.org>" not changed
gpg: Total number processed: 1
gpg: unchanged: 1
Update the repositories again:
sudo apt update
Install Cassandra:
sudo apt install cassandra -y
sudo service cassandra status
● cassandra.service - LSB: distributed storage system for structured data
Loaded: loaded (/etc/init.d/cassandra; generated)
Active: active (running) since Wed 2019-08-07 13:54:43 UTC; 2min 9s ago
Docs: man:systemd-sysv-generator(8)
Tasks: 43 (limit: 4915)
CGroup: /system.slice/cassandra.service
└─10716 java -Xloggc:/var/log/cassandra/gc.log -ea -XX:+UseThreadPriorities -XX:ThreadPriorityPolicy=42 -XX:+HeapDumpOnOutOfMemoryError -Xss256k -XX:StringTableSize=1000003 -XX:+Al
Aug 07 13:54:43 instance-template-1 systemd[1]: Starting LSB: distributed storage system for structured data...
Aug 07 13:54:43 instance-template-1 systemd[1]: Started LSB: distributed storage system for structured data.
- You can start Cassandra with
sudo service cassandra startand stop it withsudo service cassandra stop. However, normally the service will start automatically. For this reason be sure to stop it if you need to make any configuration changes. - Verify that Cassandra is running by invoking
nodetool statusfrom the command line.
nodetool status
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 127.0.0.1 103.73 KiB 256 100.0% f09cb964-1d27-40e7-a3cf-f6f623bbcb63 rack1
- The default location of configuration files is
/etc/cassandra. - The default location of log and data directories is
/var/log/cassandra/and/var/lib/cassandra. - Start-up options (heap size, etc) can be configured in
/etc/default/cassandra.
Getting Started with cqlsh
cqlsh is a command line shell for interacting with Cassandra through CQL. It is shipped with every Cassandra package.
cqlsh
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh>
Create a keyspace
Similar to Database in SQL
CREATE KEYSPACE admatic WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};
Using the keyspace that was created
USE admatic;
Creating a table
CREATE TABLE emp(
emp_id int PRIMARY KEY,
emp_name text,
emp_phone varint
);
Inserting data into table
INSERT INTO emp (emp_id, emp_name, emp_phone) VALUES(1, 'Rahman', 9848022330);
Querying the table
SELECT * FROM emp;
emp_id | emp_name | emp_phone
--------+----------+------------
1 | Rahman | 9848022330
(1 rows)
Updating the Data
UPDATE emp SET emp_phone=9842341567 WHERE emp_id=1;
SELECT * FROM emp;
emp_id | emp_name | emp_phone
--------+----------+------------
1 | Rahman | 9842341567
(1 rows)
Deleting a row
DELETE FROM emp WHERE emp_id=1;
SELECT * FROM emp;
emp_id | emp_name | emp_phone
--------+----------+-----------
(0 rows)