该篇内容由个人博客同步更新!转载请注明出处!

我不喜欢拿一堆数据的运行耗时来对比各个解决方案的性能等,有时候看一些测评长篇大论写耗时的一些对比,有时就差个 几百毫秒 我觉得也没啥必要,关键是好用就行,一切从简,我写博客也喜欢一切从简。
.net操作clickhouse的库比较少,大多数都是基于clickhouse.ado的一个封装,下面也主要介绍一下clickhouse.ado的使用,以及自己封装的一个库的使用。

前言

clickhouse适用于大数据量分析,我的应用场景是每十秒从公交轨迹中取固定时间段数据分析一些情况,电脑配置就是普通的开发配置,总体数据轨迹量在3亿左右,处理的数据时间段在一天以内,取出的数据量在2.3万条左右。大家可以当个借鉴!

具体操作

一、简单的查询和新增以及批量新增(clickhouse不推荐数据的编辑和删除此处就不再举例)

public class demo
{
        private clickhouseconnection getconnection(string cstr= "compress=true;checkcompressedhash=false;compressor=lz4;host=ch-test.flippingbook.com;port=9000;database=default;user=andreya;password=123")
        {
            var settings = new clickhouseconnectionsettings(cstr);
            var cnn = new clickhouseconnection(settings);
            cnn.open();
            return cnn;
        }
        /*查询*/
        public void select()
        {
            using (var cnn = getconnection())
            {
                var reader = cnn.createcommand("select * from test").executereader()
                ......省略
            }
        }
        /*增加*/
        public void insert()
        {
            using (var cnn = getconnection())
            {
                var cmd = cnn.createcommand("insert into test (date,x, arr)values ('2017-01-01',1,['a','b','c'])");
                cmd.executenonquery();
            }
        }
        /*批量新增*/
        public void insertbulk()
        {
            using (var cnn = getconnection())
            {
                var cmd = cnn.createcommand("insert into test (date,x, values.name,values.value)values @bulk;");
                cmd.parameters.add(new clickhouseparameter
                {
                    dbtype = dbtype.object,
                    parametername = "bulk",
                    value = new[]
                    {
                        new object[] {datetime.now, 1, new[] {"aaaa@bbb.com", "awdasdas"}, new[] {"dsdsds", "dsfdsds"}},
                        new object[] {datetime.now.addhours(-1), 2, new string[0], new string[0]},
                    }
                });
                cmd.executenonquery();
            }
        }
}

二、鉴于使用原始方法读取数据后转换的方式太麻烦,分页等也需要自己实现,所以写了一个帮助类,方便操作clickhouse,点击跳转

使用方式也很简单,如下:

public historymodel gethistories(string busid, string begindt, string enddt)
        {
            using (var helper = new clickhousehelper())
            {
                try
                {
                    historymodel historymodel = new historymodel();
                    historymodel.histories = helper .executelist<historiesmodel>($"select mile,speed,lon,lat,direct,termtime from its.gps_mergetree where termtime >='{begindt}' and termtime<='{enddt}' and busid={busid} order by termtime");
                    historymodel.inouts = helper .executelist<inoutmodel>($"select * from its.inout_t where adtime>='{begindt}' and adtime<='{enddt}' and busid={busid} order by recvtime");
                    //clickhouse中取出来的时间默认会有时区的问题,这里需要手动转下本地的时区
                    historymodel.histories.foreach(u => u.termtime = datetime.parse(u.termtime).tolocaltime().tostring("yyyy-mm-dd hh:mm:ss"));
                    historymodel.inouts.foreach(u => u.recvtime = u.recvtime.tolocaltime());
                    return historymodel;
                }
                catch (exception e)
                {
                    ckhelper.dispose();
                    console.writeline(e);
                    throw;
                }
            }
        }

三、一些小问题记录

  1. 时区问题
    clickhosue中取出来的时候会多8个小时,之前一度怀疑安装时服务器时区不对,但实际上都是正确的,只能手动将时间通过tolocaltime转成本地时区
  2. 批量插数据
    批量插数据的时候如果传入一个list的话,对应的类需要增加getenumerator方法,就像这样
public class demo
{
     public string obu { get; set; }
     public int busid { get; set; }
     public string buscode { get; set; }
     public ienumerator getenumerator()
        {
            yield return obu;
            yield return busid;
            yield return buscode;
            .....
        }
}
  1. 类型统一问题
    具体参考我的这篇文章

微信关注我哦!(转载注明出处)