How to start with InfluxDB OSS v2 and C# Client Library for Windows and Docker

PeterMilovcik - Apr 10 - - Dev Community

What is InfluxDB OSS v2?

InfluxDB OSS (Open Source Software) v2 is the latest version of the InfluxDB time series database platform. It is designed to handle high write and query loads, making it a suitable tool for storing and analyzing large amounts of time-stamped data. This version combines capabilities from the previous versions of InfluxDB 1.x (like storage engine, query language) and other components like Chronograf (visualization) and Kapacitor (processing, alerting, and downsampling). It also introduces Flux, a new scripting and query language that supports analytics across measurements.

Step 1: Install Docker Desktop on Windows

Before you begin, you need Docker Desktop installed on your Windows machine. Docker Desktop provides an easy-to-use interface and the Docker CLI (Command Line Interface) necessary for running containerized applications like InfluxDB.

  • Download Docker Desktop: Go to the Docker Desktop: The #1 Containerization Tool for Developers | Docker and download the latest version of Docker Desktop for Windows.
  • Installation: Run the installer and follow the on-screen instructions. Make sure to enable the WSL 2 feature if prompted, as it's required for Docker Desktop to run efficiently on Windows.
  • Verification: After installation, open a terminal (you can use PowerShell or the Windows Command Prompt) and type docker --version to verify that Docker has been installed correctly. You should see the Docker version displayed.

Step 2: Pull and Run the InfluxDB Docker Image

First, ensure you have Docker Desktop installed. Then, use the terminal to pull the latest version of InfluxDB, version 2.7 as of today (2024-04-10). Verify the latest version on Docker Hub.

  1. Pull the InfluxDB Image: Execute docker pull influxdb:2.7 in the terminal to download the InfluxDB 2.7 image.
  2. Run the Container: Use the command below to start your InfluxDB container:
docker run --name influxdb2 -d -p 8086:8086 -v influxdb2-data:/var/lib/influxdb2 -v influxdb2-config:/etc/influxdb2 influxdb:2.7
Enter fullscreen mode Exit fullscreen mode
  • --name influxdb2: Assigns the container a name (influxdb2) for easy reference.
  • -d: Runs the container in detached mode, meaning it runs in the background.
  • -p 8086:8086: Maps port 8086 inside the container to port 8086 on your host, making the InfluxDB UI accessible at http://localhost:8086.
  • -v: Creates volumes for persistent data storage and configuration settings, ensuring your data is saved outside the container.

This command sets up InfluxDB version 2.7 in a container, making it ready for configuration and use.

Step 3: Configure InfluxDB

After starting the container, you'll need to configure InfluxDB.

  • Access the InfluxDB UI: Open your web browser and go to http://localhost:8086. You should see the InfluxDB welcome screen.

InfluxDB Welcome Page

  • Initial Setup: Follow the prompts to create an initial user, organization, and bucket. Remember the credentials you set here; you'll need them for accessing InfluxDB later.
  • Secure Your InfluxDB: It's crucial to secure your InfluxDB instance. Make sure to note down the generated token during setup, as it's needed for API authentication.

Step 4: Creating a Simple Console Application with .NET 8 Using the InfluxDB C# Client Library

  • Create a New Console Application: Open your terminal or command prompt, navigate to the desired directory, and run following command to create a new .NET 8 console application.
dotnet new console -n InfluxDB2Demo
Enter fullscreen mode Exit fullscreen mode
  • Install the InfluxDB Client Library: Use the .NET CLI command to add the InfluxDB client package to your project.
cd InfluxDB2Demo
dotnet add package InfluxDB.Client
Enter fullscreen mode Exit fullscreen mode
  • Open console application in your favorite IDE, e.g., Visual Studio Code:
code .
Enter fullscreen mode Exit fullscreen mode

Open Program.cs file to continue.

  • Initialize the Client:

To initialize the InfluxDB client in a C# application:

  • Reference the InfluxDB Client Namespace: Add using InfluxDB.Client; at the top of your program file.
  • Create the Client Instance: Instantiate the InfluxDBClient by calling its constructor with two arguments: the URL to your InfluxDB instance and your InfluxDB token.
  • Write Data:
    • By POCO: Define a class with your data schema, instantiate it, and write it using writeApi.WriteMeasurement().
  • Query Data: Write a Flux query string to retrieve data from your bucket and use influxDBClient.GetQueryApi().QueryAsync() to execute the query and process the results.
using InfluxDB.Client;
using InfluxDB.Client.Api.Domain;
using InfluxDB.Client.Core;

internal class Program
{
    private static async Task Main(string[] args)
    {
        var url = "http://localhost:8086";
        var token = "Your_InfluxDB_Token";
        using var client = new InfluxDBClient(url, token);

        // Write data
        using var writeApi = client.GetWriteApi();
        var temperature = new Temperature { Location = "south", Value = Random.Shared.Next(-30, 40), Time = DateTime.UtcNow };
        writeApi.WriteMeasurement(temperature, WritePrecision.Ns, "bucket_name", "org_id");

        // Query data
        var flux = "from(bucket:\"bucket_name\") |> range(start: 0)";
        var fluxTables = await client.GetQueryApi().QueryAsync(flux, "org_id");
        fluxTables.ForEach(fluxTable =>
        {
            var fluxRecords = fluxTable.Records;
            fluxRecords.ForEach(fluxRecord =>
            {
                Console.WriteLine($"{fluxRecord.GetTime()}: {fluxRecord.GetValue()}");
            });
        });
    }
}

[Measurement("temperature")]
class Temperature
{
    [Column("location", IsTag = true)] public string? Location { get; set; }
    [Column("value")] public double Value { get; set; }
    [Column(IsTimestamp = true)] public DateTime Time { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

NOTE: Ensure you replace "Your_InfluxDB_Token" with your actual InfluxDB API token created in Step 3, similarly also bucket_name and org_id.

This simplified guide helps you set up a basic application to interact with InfluxDB.
For detailed documentation and examples, refer to the influxdata/influxdb-client-csharp: InfluxDB 2.x C# Client (github.com).


You've taken a big step by learning how to set up and start using InfluxDB OSS v2 with Docker and C#. This guide is just the beginning. There's a lot more you can do:

  1. Learn More About Flux: Get better at using Flux for more complex data tasks.
  2. Learn More About Data Retention: Learn about data retention and how to keep your database running smoothly as it grows.
  3. Learn More About Buckets in InfluxDB: Get better understanding of how and where time series data is stored.
  4. Join the Community: There's a big community out there. Join forums, share ideas, and get help.

Remember, practice makes perfect. Keep exploring and experimenting with InfluxDB to get the most out of it. For more guides and support, check out the InfluxDB official page.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .