Skip to content

A .NET library for querying InfluxDB in a type safe way

License

Notifications You must be signed in to change notification settings

noelhx/influxql-dotnet

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

InfluxQL.net Build status NuGet Version

A C# client for querying data from InfluxDB in a type safe way.

By building up InfluxQL statements using a fluent syntax not only is valid, properly escaped, InfluxQL produced the types are tracked through and SELECT or GROUP BY projections.

Building InfluxQL statements using fluent syntax.

// SELECT \"level description\" AS level_description, water_level FROM h2o_feet
var query = InfluxQuery.From(h2o_feet).Select(fields => fields);

Note escaping and alias for field "level description".

We can do SELECT projection to just return the water_level.

// SELECT water_level FROM h2o_feet
var waterLevelQuery = InfluxQuery.From(h2o_feet).Select(fields => new { fields.water_level });

Retrive the results from Influx.

var client = new InfluxQLClient(new Uri("http:/localhost:8086"), "NOAA_water_database");

var results = await client.Query(waterLevelQuery.Statement);

foreach (var (time, values) in results)
{
    Console.WriteLine($"{time} {values.water_level}");
}

Note the strong typing for returning a single series of points based on query statement passed in to Query method.

The type retuned in the results from issuing this query is an instance of the anonymous type we define in the select statement meaning tring to reference level_description in the results will cause an complie error.

Doing a GROUP BY will retun multiple series:

using static InfluxDB.InfluxQL.Aggregations;
...
// SELECT MEAN(water_level) AS mean FROM h2o_feet GROUP BY location
var query = InfluxQuery.From(h2o_feet)
	.Select(fields => new { mean = MEAN(fields.water_level) })
	.GroupBy(tags => new { tags.location });

var results = await client.Query(query.Statement);

foreach (var series in results)
{
    foreach (var point in series.Points)
    {
        Console.WriteLine($"{series.Tags.location}, {point.values.mean}, {point.time});
    }
}

We are importing InfluxDB.InfluxQL.Aggregations static class so we can use the MEAN function without prefixing. We could have written Aggregations.MEAN((fields.water_level).

About

A .NET library for querying InfluxDB in a type safe way

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%