Using ScyllaDB with Go.
gocql
is the driver to use ScyllaDB with Golang.
gocqlx
is a wrapper for gocql that provides developer productivity without sacrificing performance.
Firstly, be sure to have both Docker, Docker Compose and Golang installed.
Git clone this repository:
git clone https://github.com/Guilospanck/scylladb-and-go.git
Change directory into it:
cd scylladb-and-go/
Then put Scylla cluster UP by using docker-compose:
# docker compose v1
sudo docker-compose -f docker-compose.yml up -d --build
# docker compose v2
sudo docker compose -f docker-compose.yml up -d --build
After some time (may take really some time, like 5min total) you'll be able to have a Scylla cluster up and running. To verify that:
sudo docker exec -it scylla-node1 nodetool status
You should see something like:
Sometimes you may see some error like "Could not connect. Connection refused". In order to change that, do:
sudo nano /etc/sysctl.conf
# Then add this line at the end of the file
fs.aio-max-nr = 1048576
# save it and to activate the config
sudo sysctl -p /etc/sysctl.conf
# after that, kill the containers and put them up again:
sudo docker-compose kill
sudo docker-compose rm -f
sudo docker-compose -f docker-compose.yml up -d --build
Once you have all the above section setted up, follow this one.
Change directory into the project directory
cd scylladb-and-go/
And then run
go run .
Using gocqlx
to create our queries we have the following:
type T any
type IQueryBuilder[t T] interface {
Insert(insertData *t) error
Delete(dataToBeDeleted *t) error
DeleteAllFromPartitioningKey(dataToBeDeleted *t) error
Select(dataToGet *t) ([]t, error)
Get(dataToGet *t) ([]t, error)
SelectAll() ([]t, error)
}
Insert
: will insert a new item into the databaseDelete
: will delete an item based on the Primary Key (Partitioning Key + Clustering Key)DeleteAllFromPartitioningKey
: will delete an item based on the Partitioning KeySelect
: will return data based on the Partitioning KeyGet
: will return data based on the Primary Key (PK + CK)SelectAll
: will return all data from table
You can use either the simple curl
function or the Insomnia
program.
If using Insomnia, be sure to go to Applications > Preferences and then uncheck "Validate certificates" under "Request/Response" section. This is needed in order to make requests to the HTTPS localhost. To make requests from Edge, navigate to
edge://flags/#allow-insecure-localhost
and allow it.
Endpoint used to insert tracking data.
➡️ Request
- curl
curl --location --request POST 'https://localhost:4444/api/v1/tracking' \ --header 'Content-Type: application/json' \ --data-raw '{ "firstName": "Post", "lastName": "Test", "timestamp": "2022-04-10 09:10:00 +0000 UTC", "location": "Brazil", "speed": 10, "heat": 25, "telepathyPowers": 222 }'
- Insomnia: run
Insert tracking data
Endpoint used to delete tracking data by Primary Key (Partition Key + Clustering Key).
➡️ Request
- curl
curl --location --request DELETE 'https://localhost:4444/api/v1/tracking' \ --header 'Content-Type: application/json' \ --data-raw '{ "firstName": "Post", "lastName": "Test", "timestamp": "2022-04-10 09:10:00 +0000 UTC" }'
- Insomnia: run
Delete tracking data by Primary Key
Endpoint used to delete tracking data by Partition Key.
➡️ Request
- curl
curl --location --request DELETE 'https://localhost:4444/api/v1/tracking/bypartition' \ --header 'Content-Type: application/json' \ --data-raw '{ "firstName": "Post", "lastName": "Test" }'
- Insomnia: run
Delete all tracking data by Partition Key
Endpoint used to get tracking data by Primary Key.
➡️ Request
- curl
curl --location --request GET 'https://localhost:4444/api/v1/tracking' \ --header 'Content-Type: application/json' \ --data-raw '{ "firstName": "Post", "lastName": "Test", "timestamp": "2022-04-10 09:10:00 +0000 UTC" }'
- Insomnia: run
Get by Primary Key
Endpoint used to get tracking data by Partition Key.
➡️ Request
- curl
curl --location --request GET 'https://localhost:4444/api/v1/tracking/bypartition' \ --header 'Content-Type: application/json' \ --data-raw '{ "firstName": "Post", "lastName": "Test" }'
- Insomnia: run
Get All by Partition Key
Endpoint used to get all tracking data.
➡️ Request
- curl
curl --location --request GET 'https://localhost:4444/api/v1/tracking/all'
- Insomnia: run
Get All