Skip to content

Commit 8d584d3

Browse files
committed
initial commit
0 parents  commit 8d584d3

18 files changed

+605
-0
lines changed

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# java-cli-gradle-cassandra-cql-multi-node-without-ssl-index
2+
3+
## Description
4+
Creates a small database table
5+
called `dog`. This table, `dog`, has been normalized to 3NF. Uses batch operation to `insert | delete | update` dog table.
6+
7+
A java gradle build, that connects to multi node
8+
cassandra database without ssl.
9+
10+
## Tech stack
11+
- docker-compose-wait
12+
- java
13+
- gradle
14+
- cql
15+
- cassandra drivers
16+
17+
## Docker stack
18+
- cassandra:4.0
19+
- gradle:jdk11
20+
21+
## To run
22+
`sudo ./install.sh -u`
23+
24+
## To stop
25+
`sudo ./install.sh -d`
26+
27+
## For help
28+
`sudo ./install.sh -h`
29+
30+
## Credit
31+
- [Docker setup](https://2much2learn.com/setting-up-cassandra-with-docker/)
32+
- [Cql java client](https://github.com/eugenp/tutorials/tree/master/persistence-modules/java-cassandra)

docker-compose.yml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
version: '3.5'
2+
services:
3+
java-srv:
4+
build:
5+
context: java-srv
6+
depends_on:
7+
- db
8+
links:
9+
- "db:db"
10+
command: sh -c "/wait && gradle run"
11+
environment:
12+
- WAIT_HOSTS=db:9042
13+
- WAIT_HOSTS_TIMEOUT=300
14+
- WAIT_SLEEP_INTERVAL=30
15+
- WAIT_HOST_CONNECT_TIMEOUT=30
16+
17+
18+
db:
19+
image: cassandra:latest
20+
ports:
21+
- 9042
22+
healthcheck:
23+
test: [ "CMD", "ls", "/opt/cassandra/bin" ]
24+
interval: 15s
25+
timeout: 10s
26+
retries: 10
27+
environment:
28+
- CASSANDRA_SEEDS=node2,node3
29+
- CASSANDRA_CLUSTER_NAME=citizix
30+
- CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch
31+
- CASSANDRA_DC=datacenter1
32+
33+
node2:
34+
image: cassandra:latest
35+
ports:
36+
- 9042
37+
healthcheck:
38+
test: [ "CMD", "ls", "/opt/cassandra/bin" ]
39+
interval: 15s
40+
timeout: 10s
41+
retries: 10
42+
environment:
43+
- CASSANDRA_CLUSTER_NAME=citizix
44+
- CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch
45+
- CASSANDRA_DC=datacenter1
46+
47+
node3:
48+
image: cassandra:latest
49+
ports:
50+
- 9042
51+
healthcheck:
52+
test: [ "CMD", "ls", "/opt/cassandra/bin" ]
53+
interval: 15s
54+
timeout: 10s
55+
retries: 10
56+
environment:
57+
- CASSANDRA_CLUSTER_NAME=citizix
58+
- CASSANDRA_ENDPOINT_SNITCH=GossipingPropertyFileSnitch
59+
- CASSANDRA_DC=datacenter1

general.log

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[2022-09-09 13:28:06 INFO]: install::setup-logging ended
2+
================
3+
[2022-09-09 13:28:06 INFO]: install::start-up started
4+
[2022-09-09 13:28:06 INFO]: install::start-up starting services
5+
[2022-09-09 13:28:06 INFO]: install::start-up ended
6+
================
7+
[2022-09-09 13:31:19 INFO]: install::root-check started
8+
[2022-09-09 13:31:19 INFO]: install::root-check ended
9+
================
10+
[2022-09-09 13:31:19 INFO]: install::docker-check started
11+
[2022-09-09 13:31:19 INFO]: install::docker-check ended
12+
================
13+
[2022-09-09 13:31:19 INFO]: install::docker-compose-check started
14+
[2022-09-09 13:31:19 INFO]: install::docker-compose-check ended
15+
================
16+
[2022-09-09 13:31:19 INFO]: install::tear-down started
17+
[2022-09-09 13:31:19 INFO]: install::tear-down starting services
18+
[2022-09-09 13:31:19 INFO]: install::tear-down ended
19+
================

install.sh

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
#!/usr/bin/env bash
2+
basefile="install"
3+
logfile="general.log"
4+
timestamp=`date '+%Y-%m-%d %H:%M:%S'`
5+
6+
if [ "$#" -ne 1 ]; then
7+
msg="[ERROR]: $basefile failed to receive enough args"
8+
echo "$msg"
9+
echo "$msg" >> $logfile
10+
exit 1
11+
fi
12+
13+
function setup-logging(){
14+
scope="setup-logging"
15+
info_base="[$timestamp INFO]: $basefile::$scope"
16+
17+
echo "$info_base started" >> $logfile
18+
19+
echo "$info_base removing old logs" >> $logfile
20+
21+
rm -f $logfile
22+
23+
echo "$info_base ended" >> $logfile
24+
25+
echo "================" >> $logfile
26+
}
27+
28+
function root-check(){
29+
scope="root-check"
30+
info_base="[$timestamp INFO]: $basefile::$scope"
31+
32+
echo "$info_base started" >> $logfile
33+
34+
#Make sure the script is running as root.
35+
if [ "$UID" -ne "0" ]; then
36+
echo "[$timestamp ERROR]: $basefile::$scope you must be root to run $0" >> $logfile
37+
echo "==================" >> $logfile
38+
echo "You must be root to run $0. Try the following"
39+
echo "sudo $0"
40+
exit 1
41+
fi
42+
43+
echo "$info_base ended" >> $logfile
44+
echo "================" >> $logfile
45+
}
46+
47+
function docker-check() {
48+
scope="docker-check"
49+
info_base="[$timestamp INFO]: $basefile::$scope"
50+
cmd=`docker -v`
51+
52+
echo "$info_base started" >> $logfile
53+
54+
if [ -z "$cmd" ]; then
55+
echo "$info_base docker not installed"
56+
echo "$info_base docker not installed" >> $logfile
57+
fi
58+
59+
echo "$info_base ended" >> $logfile
60+
echo "================" >> $logfile
61+
62+
}
63+
64+
function docker-compose-check() {
65+
scope="docker-compose-check"
66+
info_base="[$timestamp INFO]: $basefile::$scope"
67+
cmd=`docker-compose -v`
68+
69+
echo "$info_base started" >> $logfile
70+
71+
if [ -z "$cmd" ]; then
72+
echo "$info_base docker-compose not installed"
73+
echo "$info_base docker-compose not installed" >> $logfile
74+
fi
75+
76+
echo "$info_base ended" >> $logfile
77+
echo "================" >> $logfile
78+
79+
}
80+
function usage() {
81+
echo ""
82+
echo "Usage: "
83+
echo ""
84+
echo "-u: start."
85+
echo "-d: tear down."
86+
echo "-h: Display this help and exit."
87+
echo ""
88+
}
89+
90+
function start-up(){
91+
92+
local scope="start-up"
93+
local docker_img_name=`head -n 1 README.md | sed 's/# //'`
94+
local info_base="[$timestamp INFO]: $basefile::$scope"
95+
96+
echo "$info_base started" >> $logfile
97+
98+
echo "$info_base starting services" >> $logfile
99+
100+
sudo docker-compose up --build
101+
102+
echo "$info_base ended" >> $logfile
103+
104+
echo "================" >> $logfile
105+
}
106+
function tear-down(){
107+
108+
scope="tear-down"
109+
info_base="[$timestamp INFO]: $basefile::$scope"
110+
111+
echo "$info_base started" >> $logfile
112+
113+
echo "$info_base starting services" >> $logfile
114+
115+
sudo docker-compose down
116+
117+
echo "$info_base ended" >> $logfile
118+
119+
echo "================" >> $logfile
120+
}
121+
122+
root-check
123+
docker-check
124+
docker-compose-check
125+
126+
while getopts ":udh" opts; do
127+
case $opts in
128+
u)
129+
setup-logging
130+
start-up ;;
131+
d)
132+
tear-down ;;
133+
h)
134+
usage
135+
exit 0 ;;
136+
/?)
137+
usage
138+
exit 1 ;;
139+
esac
140+
done

java-srv/Dockerfile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM gradle:jdk11
2+
3+
USER root
4+
5+
VOLUME "/root/log"
6+
7+
WORKDIR /app
8+
9+
ADD --chown=gradle:gradle /bin/ .
10+
11+
RUN chmod -R +x *
12+
13+
# Add docker-compose-wait tool -------------------
14+
ENV WAIT_VERSION 2.7.2
15+
16+
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/$WAIT_VERSION/wait /wait
17+
18+
RUN chmod +x /wait
19+
20+
CMD ["gradle", "run"]

java-srv/bin/build.gradle

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apply plugin: 'java'
2+
apply plugin: 'application'
3+
4+
repositories {
5+
mavenCentral()
6+
}
7+
8+
ext {
9+
datastaxVersion = '4.5.0'
10+
slf4jVersion = '1.7.5'
11+
}
12+
13+
dependencies {
14+
// https://mvnrepository.com/artifact/com.codahale.metrics/metrics-core
15+
implementation 'com.codahale.metrics:metrics-core:3.0.2'
16+
implementation 'io.netty:netty-transport:4.1.71.Final'
17+
implementation "com.datastax.oss:java-driver-core:${datastaxVersion}"
18+
implementation "com.datastax.oss:java-driver-query-builder:${datastaxVersion}"
19+
implementation 'com.datastax.cassandra:cassandra-driver-core:3.1.2'
20+
implementation 'log4j:log4j:1.2.17'
21+
implementation "org.slf4j:slf4j-api:${slf4jVersion}"
22+
implementation "org.slf4j:slf4j-log4j12:${slf4jVersion}"
23+
}
24+
25+
mainClassName = "example.Main"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package example;
2+
3+
import org.apache.log4j.PropertyConfigurator;
4+
5+
import org.apache.log4j.Logger;
6+
7+
import example.dto.*;
8+
9+
public class Main {
10+
11+
private static final Logger logger = Logger.getLogger(Main.class);
12+
13+
14+
public static void main(String[] args) {
15+
String PWD = System.getenv("PWD");
16+
17+
// PropertyConfigurator.configure(PWD + "/src/main/resources/log4j.xml");
18+
19+
String serverIP = "db";
20+
Generic client = new Generic(serverIP, PWD);
21+
client.operation("00", CQLOPT.KEYSPACE);
22+
23+
client.operation("01", CQLOPT.CREATE);
24+
client.operation("02", CQLOPT.INSERT);
25+
client.operation("02", CQLOPT.BATCH);
26+
client.operation("03", CQLOPT.INDEX);
27+
client.operation("04", CQLOPT.INDEX);
28+
client.operation("05", CQLOPT.SELECT);
29+
30+
client.close();
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package example.dto;
2+
3+
public enum CQLOPT {
4+
KEYSPACE("-create-keyspace.cql", "Keyspace"),
5+
CREATE("-create-table.cql", "Create"),
6+
INDEX("-create-index.cql", "Index"),
7+
BATCH("-run-batch.cql", "Batch"),
8+
INSERT("-insert-table.cql", "Insert"),
9+
SELECT("-select-table.cql", "Select"),
10+
VIEW("-create-view.cql", "View");
11+
12+
public final String cqlFile;
13+
public final String operation;
14+
15+
private CQLOPT(String file, String op)
16+
{
17+
this.cqlFile = file;
18+
this.operation = op;
19+
}
20+
21+
}

0 commit comments

Comments
 (0)