Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d3646a8

Browse files
initial commit
0 parents commit d3646a8

File tree

16 files changed

+602
-0
lines changed

16 files changed

+602
-0
lines changed

‎README.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# java-cli-gradle-cassandra-single-node-without-ssl-batch
2+
3+
## Description
4+
Creates a small database table
5+
called `dog`. Uses batch operation to `insert | delete | update` dog table.
6+
7+
A java gradle build, that connects to single node
8+
cassandra database without ssl.
9+
10+
## Tech stack
11+
- docker-compose-wait
12+
- java
13+
- gradle
14+
- cassandra drivers
15+
16+
## Docker stack
17+
- cassandra:4.0
18+
- gradle:jdk11
19+
20+
## To run
21+
`sudo ./install.sh -u`
22+
23+
## To stop
24+
`sudo ./install.sh -d`
25+
26+
## For help
27+
`sudo ./install.sh -h`
28+
29+
## Credit
30+
- [Docker setup](https://2much2learn.com/setting-up-cassandra-with-docker/)
31+
- [Cassandra java client](https://github.com/eugenp/tutorials/tree/master/persistence-modules/java-cassandra)
32+
- [Simple cassandra java client](https://raw.githubusercontent.com/oscerd/cassandra-java-example/master/src/main/java/com/github/oscerd/cassandra/SimpleClient.java)
33+
- [Batch operation](https://www.tutorialspoint.com/cassandra/cassandra_batch.htm)

‎docker-compose.yml‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: '3'
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+
db:
18+
image: cassandra:4.0
19+
ports:
20+
- "9042:9042"
21+
healthcheck:
22+
test: [ "CMD", "ls", "/opt/cassandra/bin" ]
23+
interval: 15s
24+
timeout: 10s
25+
retries: 10
26+
environment:
27+
- CASSANDRA_HOST=db
28+
- CASSANDRA_SEEDS=db
29+
- CASSANDRA_PASSWORD_SEEDER=yes
30+
- CASSANDRA_CLUSTER_NAME=citizix

‎general.log‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[2022年09月09日 13:56:36 INFO]: install::setup-logging ended
2+
================
3+
[2022年09月09日 13:56:36 INFO]: install::start-up started
4+
[2022年09月09日 13:56:36 INFO]: install::start-up starting services

‎install.sh‎

Lines changed: 140 additions & 0 deletions
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‎

Lines changed: 20 additions & 0 deletions
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‎

Lines changed: 25 additions & 0 deletions
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"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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("03", CQLOPT.BATCH);
26+
client.operation("04", CQLOPT.SELECT);
27+
28+
client.close();
29+
}
30+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
package example.dto;
3+
4+
public enum CQLOPT {
5+
KEYSPACE("-create-keyspace.cql", "Keyspace"),
6+
INDEX("-index-table.cql", "Index"),
7+
SELECT("-select-table.cql", "Select"),
8+
BATCH("-run-batch.cql", "Batch"),
9+
VIEW("-create-view.cql", "View"),
10+
INSERT("-insert-table.cql", "Insert"),
11+
CREATE("-create-table.cql", "Create");
12+
13+
public final String cqlFile;
14+
public final String operation;
15+
16+
private CQLOPT(String file, String op)
17+
{
18+
this.cqlFile = file;
19+
this.operation = op;
20+
}
21+
22+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /