Tutorial
Setting up the environment
This tutorial will walk you through the most important commands of zeeSQL
and how to use them.
The simplest way to follow this tutorial is to launch an instance of zeeSQL
using docker.
The next thing you will need to follow the tutorial is the redis-cli
.
You can connect to the same docker container and start the redis-cli
with:
At this point you are inside the redis-cli
, ready to interact with Redis
and zeeSQL
.
Creating a database in zeeSQL
The very first step when working with zeeSQL
it is to create a new database.
The database can be created with a single command.
This command will create a new database, and it will associate it with the Redis key DB
.
Any time we want to interact with this database, we will pass DB
to the zeeSQL
commands.
It is possible to create more than one database, you can create as many as you like, since they are very lightweight.
Sending commands to the database
After creating a database, we want to interact with it. It is possible to interact with the database sending it commands.
We have successfully sent our first command to zeeSQL
and get our first RESULT
, the integer 1.
Modify the database structure
A database without tables is not very useful. We will now create a table to store information about users.
In the table we want to store the username, its score and the user email. The username and the email will be text fields, while the score will be an integer field.
The operation was successfully and 0 rows have been modified.
However, we now have a table where we can store information about the user.
Add data to the database
We can now start to add users to our table.
Each insert was successful and each one added one more row to the database.
Query the database
After having added data to the database, we want to query those data back.
We can ask for the score of the user jsmith
In this case the score is an integer, and it is of value 3.
Or to know what users have a score greater than 5
In this other case the usernames are of type TEXT and are: DuffyAlone
and far
.
Since we are not modifying the database, instead of EXECUTING a command with EXEC
we can just QUERY.
QUERY
does not work when trying to modify the database.
Modify the data
Our users, keep using our platform, are increasing their score. We can increase the score with an update.
In this example, we first increase the score of the user jsmith
of one, and then we query the same score.
Delete the data
Eventually our user will leave the platform, in this case we can delete them.
Use arguments for your queries
Up to now, we send only SQL queries that contains all the parameters. It is also possible to send a query with placeholders followed by arguments. This is useful to avoid SQL injections attacks and to avoid string constructions at runtime.
In our example we can increment the score of a player by a specific amount.
The first argument is ?1
(not ?0
) and the second argument is ?2
.
Using secondary indexes (or search by values) in Redis
Now that we have understood how to deal with standard zeeSQL databases and how to query them, we can move forward.
Now we will introduce zeeSQL secondary indexes, or how to search and project Redis hash
data.
In Redis, it is common to store values as hash. Each hash is univocally identify by a key, and it has one of more field. Each field has a value associated with.
Redis already provide fast access to elements by their key. But it is not possible to search keys from their values.
With zeeSQL we can solve this problem, but automatically push the hashes keys and values to a specific table.
We will work with a simple telemetric system. We have different sensors, each sensor send a timestamp, a temperature value and a humidity value.
Saving the data into Redis
As first step let's see how we model the data in raw Redis, using Redis Hashes.
The key is in the form sensor:$sendor_id:$timestamp
and the other fields contains the telemetries' data.
Creating an index
We now want to store the information in the sensor in an SQL table, for easier access.
The command creates a new secondary index associate with the table sensors
. The index will be concerned only for the hashes which key start with the prefix sensor:
(*
being a catch-all). The schema used by the index will have 4 rows, each of them will be an INTEGER, and the name of those columns are respectively timestamp
. sensor
, temperature
and humidity
.
The creation of an index, imply the creation of the table in the database. If the table already exists, it is assumed to contain the correct columns.
Reading data from the index
As soon as the index is created, the Redis keys space is scanned and the hashes are added to the table. Which means that we can immediately query the table.
Modify the table of the index
It is possible to add, remove and update values to the index table manually. Do not do that, the data between Redis and zeeSQL will go out of sync.
Adding hashes
The index continuously listens to the HASH commands of Redis and keeps the table in sync.
In this example we added the sensor with ID 3, after the secondary index was already in place.
Removing hashes
Similarly, if a hash is deleted, the correct row is deleted from the table.
As you can see we delete a hash, and the correct row was deleted also from the table.
Updating hashes
As with deletion, updating a hash is also reflected on the index table.
In this case we update a hash, and also the table was updated.
Querying the secondary index table
The secondary index table, it is just a plain SQL table. zeeSQL does not add any index on the table. User is free to add the SQL indexes it desired to the secondary index table.
Without any index, each query will go through a full table scan.
End
I hope that this tutorial was helpful :)
If you have any question, feel free to contact me simone@redbeardlab.com or on github: RedBeardLab/zeeSQL-doc
Last updated