4.4. .Net#

4.4.1. Requirements#

Before you can get started, please ensure that:

  • You have the latest version of the QuasarDB client library installed on your computer

  • You have access to a running QuasarDB cluster.

The rest of this document assumes you have a cluster up and running under qdb://

4.4.2. Installing libraries#

<PackageReference Include="quasardb" Version="3.14.0" />

4.4.3. Importing libraries#

Most languages require you to import the relevant QuasarDB modules before you can use them, so we start out with them.

using Quasardb;
using Quasardb.TimeSeries;

4.4.4. Connection management#

Establishing a connection with the QuasarDB cluster is easy. You need the URI of at least one of your nodes, and the client will automatically detect all nodes in the cluster.

A QuasarDB cluster operates in either a secure or an insecure mode. If you do not know whether your cluster is running in secure mode, please ask your system administrator. Insecure connection#

var c = new QdbCluster("qdb://"); Secure connection#

In case of a secure connection, we need to provide a few additional parameters:

  • A username;

  • A user private key;

  • A cluster public key.

More information on QuasarDB’s security mechanisms can be found in our security manual.

If you do not know the values of these parameters, please ask your system administrator.

var sc = new QdbCluster(secureClusterURI, clusterPublicKey, userName, userPrivateKey);

4.4.5. Creating a table#

Before we can store timeseries data, we need to create a table. A table is uniquely identified by a name (e.g. “stocks” or “sensors”) and can have 1 or more columns.

In this example we will create a table “stocks” with three columns, “open”, “close” and “volume”. The respective types of the columns are two double precision floating point values and a 64-bit signed integer.

// First we acquire a reference to a table which not exist
var alias = RandomGenerator.CreateUniqueAlias(); //= "stocks";
var ts = c.Table(alias);

// Initialize our column definitions
var columns = new QdbColumnDefinition[]{
        new QdbDoubleColumnDefinition("open"),
        new QdbDoubleColumnDefinition("close"),
        new QdbInt64ColumnDefinition("volume")};

// Now create the table with the default shard size
ts.Create(columns); Attaching tags#

QuasarDB allows you to manage your tables by attaching tags to them. For more information about tags, see Managing tables with Tags.

In the example below, we will attach the tag nasdaq to the “stocks” table we created.


4.4.6. A word about API types#

Now that we have our tables in place, it’s time to start interacting with actual data. On a high-level, QuasarDB provides two different APIs for you to insert data:

  • A row-based API, where you insert data on a row-by-row basis. This API is referred to as our “batch inserter”. This API provides stronger guarantees in terms of consistency.

  • A column-based API, where you insert pure timeseries data per column. This data is typically aligned per timestamp, and therefor assumes unique timestamps.

If you’re unsure which API is best for you, start out with the row-based insertion API, the batch inserter.

You should now continue with either the row oriented or the column oriented tutorials.

4.4.7. Row oriented API# Batch inserter#

The QuasarDB batch inserter provides you with a row-oriented interface to send data to the QuasarDB cluster. The data is buffered client-side and sent in batches, ensuring efficiency and consistency.

The batch writer has various modes of operation, each with different tradeoffs:

Insertion mode


Use case(s)


Transactional insertion mode that employs Copy-on-Write

General purpose


Transactional insert that does not employ Copy-on-Write. Newly written data may be visible to queries before the transaction is fully completed.

Streaming data, many small incremental writes


Data is buffered in-memory in the QuasarDB daemon nodes before writing to disk. Data from multiple sources is buffered together, and periodically flushed to disk.

Streaming data where multiple processes simultaneously write into the same table(s)

Truncate (a.k.a. “upsert”)

Replaces any existing data with the provided data.

Replay of historical data

When in doubt, we recommend you use the default insertion mode.

The steps involved in using the batch writer API is as follows:

  1. Initialize a local batch inserter instance, providing it with the tables and columns you want to insert data for. Note that specifying multiple tables is supported: this will allow you to insert data into multiple tables in one atomic operation.

  2. Prepare/buffer the batch you want to insert. Buffering locally before sending ensures that the tranmission of the data is happening at maximum throughput, ensuring server-side efficiency.

  3. Push the batch to the cluster.

  4. If necessary, go back to step 2 to send additional batches.

We recommend you use batch sizes as large as possible: between 50k and 500k rows is optimal.

In the example below we will insert two different rows for two separate days into our “stocks” table.

// We initialize a writer our batch writer.
var writer = ts.Writer();
// Alternatively we could select specific columns
// var writer = ts.Writer(new QdbColumnDefinition[]{
//    new QdbDoubleColumnDefinition("open")

// Insert the first row: to start a new row, we must provide it with a mandatory
// timestamp that all values for this row will share. QuasarDB will use this timestamp
// as its primary index.
writer.StartRow(new DateTime(2019, 02, 01));

// We now set the values for our columns by their relative offsets: column 0 below
// refers to the first column we provide in the columns variable above.
writer.SetDouble(0, 3.40);
writer.SetDouble(1, 3.50);
writer.SetInt64(2, 10000);

// We tell the batch writer to start a new row before we can set the values for the
// next row.
writer.StartRow(new DateTime(2019, 02, 02));

writer.SetDouble(0, 3.50);
writer.SetDouble(1, 3.55);
writer.SetInt64(2, 7500);

// Now that we're done, we push the buffer as one single operation.
writer.Push(); Bulk reader#

On the other side of the row-oriented API we have the “bulk reader”. The bulk reader provides streaming access to a single table, optionally limited by certain columns and/or certain time ranges.

If you want to have efficient row-oriented access to the raw data in a table, this is the API you want to use. If you want to execute aggregates, complex where clauses and/or multi-table joins, please see the query API.

The example below will show you how to read our stock data for just a single day.

// We can initialize a bulk reader based directly from our table.
var reader = ts.Reader();
ts.Reader(new QdbTimeInterval(new DateTime(2019, 02, 01), new DateTime(2019, 02, 02)));

// The bulk reader is exposed as a regular .Net Enumerable
foreach (var row in reader)
    // Each row has a timestamp which you can access as a Timespec:
    Console.WriteLine($"row timestamp: {row.Timestamp}");

    // Note that the offsets of the values array align with the offsets we used
    // when creating the table, i.e. 0 means "open", 1 means "close" and 2 means
    // "volume":
    var openValue = row[0].DoubleValue;
    var closealue = row[1].DoubleValue;
    var volumeValue = row[2].Int64Value;

The next section will show you how to store and retrieve the same dataset using the column-oriented API. If this is irrelevant to you, it’s safe to skip directly to the query API.

4.4.8. Column oriented API#

The other high level APIs QuasarDB offers are the column-oriented API. These APIs are more lightweight than the row-oriented APIs, and provides a good alternative if your dataset is shaped correctly. Storing timeseries#

To store a single timeseries, all you have to do is provide a sequence of timestamp / value pairs, and which column you want to store them as.

// Prepare some data to be inserted
var opens = new QdbDoublePointCollection { { new DateTime(2019, 02, 01), 3.40 }, { new DateTime(2019, 02, 02), 3.50 } };
var closes = new QdbDoublePointCollection { { new DateTime(2019, 02, 01), 3.50 }, { new DateTime(2019, 02, 02), 3.55 } };
var volumes = new QdbInt64PointCollection { { new DateTime(2019, 02, 01), 10000 }, { new DateTime(2019, 02, 02), 7500 } };

// Retrieve the different columns from our table
var openCol = ts.DoubleColumns["open"];
var closeCol = ts.DoubleColumns["close"];
var volumeCol = ts.Int64Columns["volume"];

// Insert data for each column
volumeCol.Insert(volumes); Retrieving timeseries#

To retrieve a single timeseries, you provide a column and one or more timerange(s). Our examples below show how to retrieve all three columns for a single day.

// using the same columns we used for the insertion
// we can retrieve the points from a specific range
var range = new QdbTimeInterval(new DateTime(2019, 02, 01), new DateTime(2019, 02, 02));


// you can now inspect the values in the enumerable Points
var resultPoints = openCol.Points();

4.4.9. Queries#

If you are looking for more flexible control over the kind of calculations performed on a dataset, or want to push certain computations to the cluster, QuasarDB offers an SQL-like query language for you to interact with your data. Please see our query language documentation.

In the example below, we will show you how to execute a simple query that calculates to total traded volume for the entire dataset.

// Execute the query
var r = c.Query("SELECT SUM(volume) FROM " + alias);

// The rows are exposed as a regular .Net Enumerable
var columnNames = r.ColumnNames;
var rows = r.Rows;
foreach (var row in rows)
    Console.WriteLine($"{columnNames[0]}: {row[0].Value}");

// Since we only expect one row, we also access it like this:
var aggregateResult = rows[0]["SUM(volume)"].Int64Value;
Console.Write($"SUM(volume): {aggregateResult}");

4.4.10. Dropping a table#

It’s easy to drop a table with QuasarDB, and is immediately visible to all clients.

// Use the earlier reference of the table we acquired to remove it:

4.4.11. Reference#