InfluxCommunity / influxdb3-csharp

The C# .NET Client that provides a simple and convenient way to interact with InfluxDB 3.
https://InfluxCommunity.github.io/influxdb3-csharp/
MIT License
24 stars 6 forks source link

Convert query results into line protocol #16

Closed Anaisdg closed 1 year ago

Anaisdg commented 1 year ago

Use Case

Users who want to query data from InfluxDB Cloud, transform it, and then write it back into InfluxDB. For example users who want to perform downsampling need a way to query the data, downsample it, convert it back into line protocol, and then write it.

Expected behavior

Convert the query results into line protocol.

Actual behavior

Feature request, doesn't exist yet. Thank you!

Additional info

No response

bednar commented 1 year ago

We've added an example of downsampling, along with a query to a structured object. For more details, please check out this example: https://github.com/InfluxCommunity/influxdb3-csharp/blob/main/Examples/Downsampling/DownsamplingExample.cs#L10

using System;
using System.Threading;
using System.Threading.Tasks;
using InfluxDB3.Client;

namespace InfluxDB3.Examples.Downsampling;

public class DownsamplingExample
{
    static async Task Main(string[] args)
    {
        const string host = "https://us-east-1-1.aws.cloud2.influxdata.com";
        const string token = "my-token";
        const string database = "my-database";

        using var client = new InfluxDBClient(host: host, token: token, database: database);

        //
        // Write data
        //
        await client.WriteRecordAsync("stat,unit=temperature avg=24.5,max=45.0");
        Thread.Sleep(1_000);

        await client.WriteRecordAsync("stat,unit=temperature avg=28,max=40.3");
        Thread.Sleep(1_000);

        await client.WriteRecordAsync("stat,unit=temperature avg=20.5,max=49.0");
        Thread.Sleep(1_000);

        //
        // Query downsampled data
        //
        const string downsamplingQuery = @"SELECT
            date_bin('5 minutes', ""time"") as window_start,
            AVG(""avg"") as avg,
            MAX(""max"") as max
        FROM ""stat""
        WHERE
              ""time"" >= now() - interval '1 hour'
        GROUP BY window_start
            ORDER BY window_start ASC;
        ";

        //
        // Execute downsampling query into pointValues
        //
        await foreach (var row in client.QueryPoints(downsamplingQuery))
        {
            var timestamp = row.GetField<DateTimeOffset>("window_start") ?? throw new InvalidOperationException();
            Console.WriteLine($"{timestamp}: avg is {row.GetDoubleField("avg")}, max is {row.GetDoubleField("max")}");

            //
            // write back downsampled date to 'stat_downsampled' measurement
            //
            var downsampledPoint = row
                .AsPoint("stat_downsampled")
                .RemoveField("window_start")
                .SetTimestamp(timestamp);

            await client.WritePointAsync(downsampledPoint);
        }
    }
}