---
# System prepended metadata

title: '28.11'

---

# 28.11
- https://hub.docker.com/_/cassandra

```  docker ps ```

```
docker run --name some-cassandra --network some-network -d cassandra           

Unable to find image 'cassandra:latest' locally
latest: Pulling from library/cassandra
eaead16dc43b: Pull complete 
46e1869246ce: Pull complete 
bbd45db92608: Pull complete 
6fcfd0f47989: Pull complete 
996685dfbe33: Pull complete 
4927828dcc1b: Pull complete 
7f67cde8352d: Pull complete 
0da185f5d218: Pull complete 
48dc66f305e8: Pull complete 
Digest: sha256:8d3187f77bfa34340e72735d642df18bc6db6ac7e6545ab471f3bce3c10b5dad
Status: Downloaded newer image for cassandra:latest
7c9d17c6e432626368ececc510becf8a4e15ce62f829ebf7511ed224988ab6ee
docker: Error response from daemon: network some-network not found.
```

- identificira točno jednu mrežu vidi se kod pokretanja ```docker network ls```
```
docker network create some-network 
a993a27733e5fce2de241e3d4cbb898843a621617cda76eeed65252fd8488102
```

```
docker container rm some-cassandra 

some-cassandra
```
```
docker run --name some-cassandra --network some-network -d cassandra         
9e20da761e326ec97e2cf203bb0d21a469f6be48b82dd605373b4452205bf626

```

- klijent CQLSH - to je shell kojim se spajamo na cassandru
- pokreni interaktivni terminal na mreži some netowok i izbrisi kada je gotovo  kontejner na kojme pokretcemo je cassanda nemoj pokreniti trenutni trenutni kontjener nego some-cassandra container ako nije naveden klijent pokreće se automatki poslužitelj
- pokrenut je jedan klijent i jedan poslužitelj

```
docker run -it --network some-network --rm cassandra cqlsh some-cassandra

Connected to Test Cluster at some-cassandra:9042
[cqlsh 6.0.0 | Cassandra 4.0.7 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
cqlsh> 
```
- https://cassandra.apache.org/_/quickstart.html

```
cqlsh> CREATE TABLE IF NOT EXISTS store.shopping_cart ( userid text PRIMARY KEY, item_count int, last_update_timestamp timestamp );
cqlsh> INSERT INTO store.shopping_cart
   ... (userid, item_count, last_update_timestamp)
   ... VALUES ('9876', 2, toTimeStamp(now()));
cqlsh> INSERT INTO store.shopping_cart
   ... (userid, item_count, last_update_timestamp)
   ... VALUES ('1234', 5, toTimeStamp(now()));
cqlsh> select * from store.shopping_cart;

 userid | item_count | last_update_timestamp
--------+------------+---------------------------------
   1234 |          5 | 2022-11-28 16:28:23.753000+0000
   9876 |          2 | 2022-11-28 16:28:15.892000+0000

(2 rows)

```

## zad isprobajte preostale SQL naredbe, specifično DESCRIBE, UPDATE i DELETE
```
cqlsh> UPDATE store.shopping_cart SET item_count = 5555 WHERE userid = '1234';
cqlsh> select * from store.shopping_cart;

 userid | item_count | last_update_timestamp
--------+------------+---------------------------------
   1234 |       5555 | 2022-11-28 16:28:23.753000+0000
   9876 |          2 | 2022-11-28 16:28:15.892000+0000

(2 rows)
```
```
cqlsh> DESCRIBE store.shopping_cart

CREATE TABLE store.shopping_cart (
    userid text PRIMARY KEY,
    item_count int,
    last_update_timestamp timestamp
) WITH additional_write_policy = '99p'
    AND bloom_filter_fp_chance = 0.01
    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
    AND cdc = false
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '16', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND crc_check_chance = 1.0
    AND default_time_to_live = 0
    AND extensions = {}
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair = 'BLOCKING'
    AND speculative_retry = '99p';
cqlsh> delete from store.shopping_cart where userid = '1234';
cqlsh> select * from store.shopping_cart;

 userid | item_count | last_update_timestamp
--------+------------+---------------------------------
   9876 |          2 | 2022-11-28 16:28:15.892000+0000

(1 rows)
```
<hr/>

- kod describe se navode dodatne postavke jer kada se radi replkiacije bitno je navesti točne dodatne podatke kako bi replikacija bila identična 
- not only SQL baza NE no SQL !!! to je vazno


- u novi terminal pokreni
``` 
docker run --name some-cassandra2 -d --network some-network -e CASSANDRA_SEEDS=some-cassandra cassandra
745b756cad789472b86dc83880d9f94146c1c481aba452add2058a0b5d9a0483
```
```
docker ps

CONTAINER ID   IMAGE       COMMAND                  CREATED              STATUS          PORTS                                         NAMES
745b756cad78   cassandra   "docker-entrypoint.s…"   About a minute ago   Up 58 seconds   7000-7001/tcp, 7199/tcp, 9042/tcp, 9160/tcp   some-cassandra2
7ef51d4e29a5   cassandra   "docker-entrypoint.s…"   25 minutes ago       Up 25 minutes   7000-7001/tcp, 7199/tcp, 9042/tcp, 9160/tcp   amazing_albattani
9e20da761e32   cassandra   "docker-entrypoint.s…"   27 minutes ago       Up 27 minutes   7000-7001/tcp, 7199/tcp, 9042/tcp, 9160/tcp   some-cassandra
```
- - ime -d je daemonizacija, mreža, -e ... je novo
- cassandra seeds - varijabla kao lista ip adresa koja se koristi za povezivanje čvorova tj. ovo je follower koji prikuplja podatke od leadera odnosno prve cassandre

```
docker run -it --network some-network --rm cassandra cqlsh some-cassandra2                           ✔ 
Connected to Test Cluster at some-cassandra2:9042
[cqlsh 6.0.0 | Cassandra 4.0.7 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
cqlsh> 
```
```
cqlsh> select * FROM store.shopping_cart ;

 userid | item_count | last_update_timestamp
--------+------------+---------------------------------
   1234 |       5555 | 2022-11-28 16:44:40.431000+0000
   9876 |          2 | 2022-11-28 16:28:15.892000+0000

(2 rows)
``` 


## zad.  isprobajte naredbu update na svakom od servera pa uočite replikaciju, repliciraju li se promjene

- meni se sve replicira 
- pokrene se update pa select na jednom i onda select na drugom -> i obrnuto i uvijek su isti podaci u bazi







- ```simplestrategy``` je za testiranje, a ```networktopologystrategy``` je za produkciju
- tu je objašnjeno https://cassandra.apache.org/doc/latest/cassandra/architecture/dynamo.html

```
cqlsh> create keyspace if not exists store2 with replication = {'class' : 'SimpleStrategy', 'replication_factor' : '5'};

Warnings :
Your replication factor 5 for keyspace store2 is higher than the number of nodes 2

```

## zad3 pokrenite jos jedan poslužitelj i stvorite keyspace s replikcaijskim faktorom
```
docker run --name some-cassandra3 -d --network some-network -e CASSANDRA_SEEDS=some-cassandra cassandra
4e86866336140f952702e221dcbae97b672f926ae1a58fa0f69d005becd1e3fe


docker run -it --network some-network --rm cassandra cqlsh some-cassandra3

Connected to Test Cluster at some-cassandra3:9042
[cqlsh 6.0.0 | Cassandra 4.0.7 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
cqlsh> 
```
- ``` docker logs some-cassandra2``` ako se desi neki problem bez

pokreni na prvoj cassandri
```
cqlsh> create keyspace if not exists store7 with replication = {'class' : 'NetworkTopologyStrategy', 'replication_factor' : '2'};
cqlsh> desc store7;

CREATE KEYSPACE store7 WITH replication = {'class': 'NetworkTopologyStrategy', 'datacenter1': '2'}  AND durable_writes = true;
```
- unutar clastera postoji samo jedan datacentar, za više datacentra treba u konfiguraciji napraviti izmjene

