QuestDB is written in Java and can be used as any other Java library. Moreover, it is a single JAR with no additional dependencies.
To include QuestDB in your project, use the following:
TableWriter facilitates table writes. To successfully create an instance
TableWriter, the table must:
- Already exist
- Have no other open writers against it as the
TableWriterconstructor will attempt to obtain an exclusive cross-process lock on the table.
CairoEngine is a resource manager for the embedded QuestDB. Its main function is
to facilitate concurrent access to pools of
A typical application will need only one instance of
instance will start when application starts and shuts down when application
closes. You will need to close
CairoEngine gracefully when the application
QuestDB provides a default configuration which only requires the
data directory to be specified. For a more advanced usage, the whole
CairoConfiguration interface can be overridden.
Execution context is a conduit for passing SQL execution artefacts to the execution site. This instance is not thread-safe and it must not be shared between threads.
The second argument is the number of threads that will be helping to execute SQL statements. Unless you are building another QuestDB server, this value should always be 1.
Before we start writing data using
TableWriter, the target table has to exist.
There are several ways to create new table ; using
SqlCompiler is the easiest.
As you will be able to see below, the table field types and indexes must match the code that is populating the table.
We use engine to create instance of
TableWriter. This will enable reusing this
TableWriter instance later, when we use the same method of creating table
The writer will hold exclusive lock on table
abc until it is closed. This lock
is both intra and inter-process. If you have two Java applications accessing the
same table only one will succeed at one time.
Although this operation semantically looks like a new object creation, the row instance is actually being re-used under the hood. A Timestamp is necessary to determine a partition for the new row. Its value has to be either increment or stay the same as the last row. When the table is not partitioned and does not have a designated timestamp column, timestamp value can be omitted.
There are put* methods for every supported data type. Columns are updated by an index as opposed to by name.
Column update order is not important and update can be sparse. All unset columns will default to NULL values.
Following method call:
Appended rows are not visible to readers until they are committed. An unneeded row can also be canceled if required.
A pending row is automatically cancelled when
writer.newRow() is called.
Consider the following scenario:
newRow() call would cancel all the updates to the row since the last
To make changes visible to readers, writer has to commit.
this job. Unlike traditional SQL databases, the size of the transaction does not
matter. You can commit anything between 1 and 1 trillion rows. We also spent
considerable effort to ensure
commit() is lightweight. You can drip one row at
a time in applications that require such behaviour.
We provide a single API for executing all kinds of SQL queries. The example
below focuses on
SELECT and how to fetch data from a cursor.
The steps to setup CairoEngine, execution context and SqlCompiler are the same as those we explained in writing data section. We will skip them here and focus on fetching data.
You can think of
RecordCursorFactory as PreparedStatement. This is the entity
that holds SQL execution plan with all of the execution artefacts. Factories are
designed to be reused and we strongly encourage caching them. You also need to
make sure that you close factories explicitly when you no longer need them.
Failing to do so can cause memory and/or other resources leak.
This instance allows iterating over the dataset produced by SQL. Cursors are relatively short-lived and do not imply fetching all the data. Note that you have to close a cursor as soon as enough data is fetched ; the closing process can happen at any time.
Cursors are not thread safe and cannot be shared between threads.
This is cursor's data access API. Record instance is obtained from the cursor outside of the fetch loop.
Record does not hold the data. Instead, it is an API to pull data when data is needed. Record instance remains the same while cursor goes over the data, making caching of records pointless.
QuestDB library provides fast and efficient way of sending line protocol
messages. Sender implementation entry point is
io.questdb.cutlass.line.udp.LineProtoSender, it is fully zero-GC and has
latency in a region of 200ns per message.
- Step 1: Create an instance of
|Network interface to use to send messages.|
|Destination IP address|
|Send buffer capacity to batch messages. Do not configure this buffer over the MTU size|
|UDP packet TTL. Set this number appropriate to how many VLANs your messages have to traverse before reaching the destination|
- Step 2: Create
entriesby building each entry's tagset and fieldset.
|Element||Description||Can be repeated|
|Specify which table the data is to be written into||no|
|Use to add a new key-value entry as metadata||yes|
|Use to add a new key-value entry as reading||yes|
|Specify the timestamp for the reading||no|
You can chain several tags and fields, e.g
Sender will send message as soon as send buffer is full. To send messages before
buffer fills up you can use
This example sends multicast messages to
22.214.171.124:9009. In this mode multiple
QuestDB instances can receive the same message.