【influxdb】C# 操作方法 influxDBHelper.cs

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);
        }
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值