Cassandra で CRUD (cqlsh)

Arch Linux でインストール

```bash
yay -S cassandra
```

Ubuntu 18.10 でインストール

```bash
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-get update
sudo apt install cassandra
```

サーバーの起動

>java-8 がディフォールトになっていることを確認

>```text
$ archlinux-java status
Available Java environments:
  java-10-openjdk
  java-8-openjdk (default)
```

>もしもなっていなければ、次のコマンドで切り替える。

>```bash
sudo archlinux-java set java-8-openjdk
```


```bash
sudo systemctl start cassandra
```

Create

```bash:cassandra_create.sh
#! /bin/bash
#
#	cassandra_create.sh
#
#						Oct/21/2018
#
WORK_FILE="/tmp/tmp0051.txt"
#
# ----------------------------------------------------------------
function record_gen_proc
{
key_in=$1
name_in=$2
population_in=$3
date_mod=$4
echo "insert into cities (key,name,population,date_mod)
                values ('$key_in','$name_in',$population_in,'$date_mod');" >> $WORK_FILE
#
}

# ----------------------------------------------------------------
echo "*** 開始 ***"
#
rm -f $WORK_FILE
#
echo "drop keyspace city;" > $WORK_FILE
#
echo "create keyspace city with replication = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };" >> $WORK_FILE
echo "use city;" >> $WORK_FILE
echo "create table cities (
        key varchar primary key,
        name text,
        population int,
        date_mod timestamp
);" >> $WORK_FILE
#
record_gen_proc t0531 秋田 97126 2001-8-12
record_gen_proc t0532 男鹿 28657 2001-6-27
record_gen_proc t0533 由利本荘 72416 2001-4-9
record_gen_proc t0534 横手 95128 2001-2-12
record_gen_proc t0535 鹿角 42761 2001-10-23
record_gen_proc t0536 大館 25613 2001-11-8
record_gen_proc t0537 能代 81257 2001-8-17
record_gen_proc t0538 にかほ 53412 2001-7-21
record_gen_proc t0539 湯沢 41576 2001-4-16
#
echo "quit;" >> $WORK_FILE
#
echo "*** pppp ***"
#
cqlsh < $WORK_FILE
#
rm -f $WORK_FILE
#
echo "*** 終了 ***"
# ----------------------------------------------------------------
```

Read

```bash:cassandra_read.sh
#! /bin/bash
#
#	cassandra_read.sh
#
#						Oct/21/2018
#
# ------------------------------------------------------------------------
WORK_FILE_A=/tmp/tmp03202.txt
#
echo 'use city;' > $WORK_FILE_A
echo 'select key,name,population,date_mod from cities;' >> $WORK_FILE_A
echo 'quit' >> $WORK_FILE_A
#
cqlsh < $WORK_FILE_A
#
rm -f $WORK_FILE_A
#
echo "*** 終了 ***"
# ------------------------------------------------------------------------
```

実行結果
![cassandra_read.png](https://qiita-image-store.s3.amazonaws.com/0/179446/edd738b6-ee99-cda2-2593-a05245351667.png)


Update

```bash:cassandra_update.sh
#! /bin/bash
#
#	cassandra_update.sh
#
#						Oct/22/2018
#
# -----------------------------------------------------------------------
WORK_FILE="/tmp/tmp030251.txt"
#
echo "*** 開始 ***"
#
key_in=$1
population_in=$2
today=`date '+%F'`
echo $today
#
echo $key_in
echo $population_in
#
echo "use city;" > $WORK_FILE
echo "update cities set population = $population_in, date_mod = '$today' where key = '$key_in';" >> $WORK_FILE
echo "quit;" >> $WORK_FILE
#
cqlsh < $WORK_FILE
#
rm -f $WORK_FILE
#
echo "*** 終了 ***"
# -----------------------------------------------------------------------
```

Delete

```bash:cassandra_delete.sh
#! /bin/bash
#
#	cassandra_delete.sh
#
#						Oct/21/2013
#
# ------------------------------------------------------------------------
WORK_FILE="/tmp/tmp030451.txt"
#
key_in=$1
echo "*** 開始 ***"
echo $key_in
echo "use city;" > $WORK_FILE
echo "delete from cities where key = '$key_in';" >> $WORK_FILE
echo "quit;" >> $WORK_FILE
#
cqlsh < $WORK_FILE
#
rm -f $WORK_FILE
echo "*** 終了 ***"
# ------------------------------------------------------------------------
```