ASP. NET Core2 Method Tutorial for Reading and Writing InfluxDB Time Series Database
- 2021-11-01 02:56:08
- OfStack
Preface
In many of our applications, we will encounter a kind of data based on a series of times to be processed. These data points can be connected into lines through time sequence, and then multi-latitude reports can be made through data statistics, and data prediction and alarm can also be realized through machine learning. Time series database is used to store and manage this kind of time series data. Time series database 1 generally supports the basic functions of fast writing, persistence and multi-latitude aggregation query of time series data.
Introduction to InfluxDB
InfluxDB is a high-performance data storage platform based on time series data, which can capture, compress and query time series data with high throughput. InfluxDB is written in Go language, which will be compiled into a binary file without external dependence to run, supporting Java, JavaScript, c # and other languages. InfluxDB supports query languages like SQL, as well as regular expressions, arithmetic expressions, and time series specific functions to speed up data processing. The following are the websites related to InfluxDB:
InfluxDB official website: https://www.influxdata.com/
InfluxDB Official Document: https://docs.influxdata.com/influxdb/
InfluxDB Official Download: https://portal.influxdata.com/downloads
InfluxDB Client Tools Download: https://docs.influxdata.com/influxdb/v1.6/tools/api_client_libraries/
Features:
Unstructured (schemeless): It can be any number of columns You can set the save time of metric Support time-related correlation functions (such as min, max, sum, count, mean, median, etc.) to facilitate statistics Support storage strategy: can be used for data deletion. (influxDB does not provide data deletion and modification methods) Support continuous query: It is a group of statements started automatically and regularly in the database, which can reduce the system occupancy of InfluxDB when matched with the storage strategy. Native HTTP support, built-in HTTP API Support syntax similar to sql Supports setting the number of copies of data in the cluster Support sampling data periodically and writing to another measurement, which is convenient for granular storage of data. It comes with web management interface for easy use (login method: http://: 8083)InfluxDB Operation
Here will be a brief introduction to how to operate InfluxDB, through these operations can basically meet the needs of the work. InfluxDB can be operated through command line tools or open source client tools. What I use here is an open source tool named "InfluxDBStudio" based on C #. The code for common operations is as follows:
# Display user
show users
# Create a user
create user "username" with password 'password'
# Create Administrator Rights User
create user "username" with password 'password' with all privileges
# Delete user
drop user "username"
# Create a database
create database "db_name"
# Show all databases
show databases
# Delete a database
drop database "db_name"
# Working with a database
use db_name
# Displays all the tables in the database
show measurements
# Create a table and specify the table name directly when inserting data, where test Is the table name
insert test,host=127.0.0.1,monitor_name=test count=1
# Delete table
drop measurement "measurement_name"
# Query data
select * from test order by time desc
# View the data saving strategy for the current database ( Retention Policies )
show retention policies on "db_name"
# Create a new data saving policy
#rp_name : Policy name
#db_name Specific database name;
#3w : Save 3 Zhou, 3 The data before weeks will be deleted, influxdb Has various event parameters, such as: h (Hours), d (Days), w (Week)
#replication 1 Number of copies, 1 Be general 1 That's enough
#default Set as default policy
create retention policy "rp_name" on "db_name" duration 3w replication 1 default
# Modify the data saving policy
alter retention policy "rp_name" on "db_name" duration 30d default
# Delete data saving policy
drop retention policy "rp_name"
# View continuous queries for the database ( Continous Queries )
show continuous queries
# Create a new continuous query ( Continous Queries )
#cq_name Continuous query name
#db_name : Database name
#sum(count) : Calculate the sum
#table_name Current table name
#new_table_name : The name of the table where the new data is stored
#30m : The time interval is 30 Minutes
create continous query cq_name on db_name begin select sum(count) into new_table_name from table_name group by time(30m) end
# Delete Continuous Queries
drop continous query cp_name on db_name
Realization
Through the above introduction and operation, we are basically familiar with InfluxDB database, so let's take a look at how to read and write InfluxDB database in ASP. NET Core2 program. Here, we use a third-party library named "InfluxData. Net" to communicate with InfluxDB interactively. Based on the library "InfluxData. Net", we can easily complete the reading and writing operations of the database. After referencing the "InfluxData. Net" class library through the Nuget manager in the ASP. NET Core2 project, type the following implementation code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using InfluxData.Net.Common.Enums;
using InfluxData.Net.InfluxDb;
using InfluxData.Net.InfluxDb.Models;
using Microsoft.AspNetCore.Mvc;
namespace WebApplication1.Controllers
{
public class InfoController : Controller
{
// Declaration InfluxDbClient
private InfluxDbClient clientDb;
public InfoController()
{
// Connect InfluxDb Adj. API Address, account number, password
var infuxUrl = "http://localhost:8086/";
var infuxUser = "admin";
var infuxPwd = "admin";
// Create InfluxDbClient Instances
clientDb = new InfluxDbClient(infuxUrl, infuxUser, infuxPwd, InfluxDbVersion.Latest);
}
/// <summary>
/// From InfluxDB Read data from
/// </summary>
public async void GetData()
{
// Incoming query commands, supporting multiple
var queries = new[]
{
" SELECT * FROM Reading WHERE time> now() - 24h "
};
var dbName = "code-hub";
// Query data from the specified library
var response = await clientDb.Client.QueryAsync(queries, dbName);
// Get Serie Collection object (returns the results of executing multiple queries)
var series = response.ToList();
// Take out the diaphragm 1 The query result of the command is 1 Set of
var list = series[0].Values;
// Extracts the first from the collection 1 Bar data
var info_model = list.FirstOrDefault();
}
/// <summary>
/// To InfluxDB Write data in
/// </summary>
public async void AddData()
{
// Based on InfluxData.Net.InfluxDb.Models.Point Entity preparation data
var point_model = new Point()
{
Name = "Reading",// Table name
Tags = new Dictionary<string, object>()
{
{ "Id", 158}
},
Fields = new Dictionary<string, object>()
{
{ "Val", "webInfo" }
},
Timestamp = DateTime.UtcNow
};
var dbName = "code-hub";
// Writes data from the specified library, supporting passing in a collection of multiple objects
var response = await clientDb.Client.WriteAsync(point_model, dbName);
}
}
}
Summarize
1. InfluxDB is a professional time series database, which can help us process the time series data in application more efficiently.
2. When using InfluxDB library, we should first understand some special functions of the library, such as data preservation strategy, continuous query and so on.
3. Through the "InfluxData. Net" class library, we can quickly and simply help us to read and write InfluxDB in ASP. NET Core2 program.