InfluxDBClient
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DMCCommon.influxdb
{
public class InfluxDBClient
{
string _baseAddress = "http://127.0.0.1:8086";
string _username = "";
string _password = "";
/// <summary>
/// 读
/// </summary>
/// <param name="database"></param>
/// <param name="sql">
/// sql = "CREATE DATABASE mydb";
/// sql = "select * from test";
/// </param>
/// <returns></returns>
public string Query(string database, string sql)
{
string pathAndQuery = string.Format("/query?db={0}&q={1}", database, sql);
string url = _baseAddress + pathAndQuery;
string result = HttpHelper.Get(url, _username, _password);
return result;
}
/// <summary>
/// 写
/// </summary>
/// <param name="database"></param>
/// <param name="sql">
/// string sql = "test,name=测试,count=1 value=10";
/// 插入表test 索引name 列count,value
/// </param>
public string Write(string database, string sql)
{
string pathAndQuery = string.Format("/write?db={0}&precision=s", database);
string url = _baseAddress + pathAndQuery;
string result = HttpHelper.Post(url, sql, _username, _password);
return result;
}
}
}
HttpHelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace DMCCommon.influxdb
{
class HttpHelper
{
/// <summary>
///
/// </summary>
/// <param name="uri"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
public static string Get(string uri, string username, string password)
{
string result = string.Empty;
WebClient client = new WebClient();
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
client.Credentials = GetCredentialCache(uri, username, password);
client.Headers.Add("Authorization", GetAuthorization(username, password));
}
return client.DownloadString(uri);
}
/// <summary>
///
/// </summary>
/// <param name="uri"></param>
/// <param name="paramStr"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
public static string Post(string uri, string paramStr, string username, string password)
{
string result = string.Empty;
WebClient client = new WebClient();
// 采取POST方式必须加的Header
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
byte[] postData = Encoding.UTF8.GetBytes(paramStr);
if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
{
client.Credentials = GetCredentialCache(uri, username, password);
client.Headers.Add("Authorization", GetAuthorization(username, password));
}
byte[] responseData = client.UploadData(uri, "POST", postData); // 得到返回字符流
return Encoding.UTF8.GetString(responseData);// 解码
}
private static CredentialCache GetCredentialCache(string uri, string username, string password)
{
string authorization = string.Format("{0}:{1}", username, password);
CredentialCache credCache = new CredentialCache();
credCache.Add(new Uri(uri), "Basic", new NetworkCredential(username, password));
return credCache;
}
private static string GetAuthorization(string username, string password)
{
string authorization = string.Format("{}:{1}", username, password);
return "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(authorization));
}
}
}
Extentions
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DMCCommon.influxdb
{
public static class Extentions
{
/// <summary>
/// 将当前时间转换成unix时间戳形式
/// </summary>
/// <param name="datetime"></param>
/// <returns></returns>
public static long ToUnixTimestamp(this DateTime datetime)
{
return (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
}
}
}
demo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DMCCommon.influxdb
{
/// <summary>
/// 示例用法
/// </summary>
class demo
{
public void test()
{
InfluxDBClient client = new InfluxDBClient();
string sql = "";
var result = "";
sql = "CREATE DATABASE mydb";
result = client.Query("mydb", sql);
sql = "test,name=测试,count=1 value=10";
result = client.Write("mydb", sql);
sql = "select * from test";
result = client.Query("mydb", sql);
}
}
}