ClickHouse & OLAP

install

  • docker install
  • connect by DBeaver
    • connect error: cannot create driver instance
      • 原因:DBeaver下载ClickHouse驱动失败
      • 解决方法:增加国内源的maven地址。窗口->首选项->DBeaver->驱动 -> Maven。添加阿里云的maven地址http://maven.aliyun.com/nexus/content/groups/public/,并将其移到最上面,再次下载ok
        在这里插入图片描述
        在这里插入图片描述

语法

数据同步

  • MySQL
    • 数据库同步
      • clickhouse创建db
        • clickhouse db
          CREATE database test_db;
        • clickhouse db of MySQL engin
          CREATE DATABASE db_name ENGINE = MySQL('ip:port', 'db_name', 'user_name', 'passwd')
      • clickhouse创建table。查看clickhouse数据类型:select * from system.data_type_families where case_insensitive=1;
      • 同步
        insert into test_db.test select * from mysql_db.test1 t
      • 优势
        • 操作简单
        • 外表
      • 劣势
        • 当MySQL表数据量比较大时,很容易出现timeout
        • 无法增量导入数据
        • 在复杂查询中,特别是有JOIN的情况下,访问外表是相当慢的,甚至不可能完成
    • 基于Altinity的工具
      • 安装clickhouse-mysql: pip install clickhouse-mysql
      • 同步:cmdshell下执行
        • clickhouse-mysql --src-host=mysql_ip --src-port=mysql_port --src-user=mysql_user --src-password=mysql_pwd --migrate-table --src-schema=mysql_schema --src-tables=mysql_table --src-tables-where-clauses="1=1 and cinemaId is not null" --dst-host=ch_ip --dst-port=9011 --dst-user=ch_user --dst-password=ch_pwd --dst-schema=ch_schema --dst-create-table --dst-table=ch_table
      • 优势
        • 支持--src-tables-where-clauses
        • 支持大数据量同步
        • 支持增量同步--binlog-position-file
      • 劣势
        • 需要命令行执行
        • 需要安装 clickhouse-mysql
        • 是否仅支持MySQL还需要验证

性能比较

  • MySQL与clickhouse
    • MySQL,耗时:2.046s
      select b.month, b.cinemaName, sum(b.total) as total
      
      from (
      
      select substr(a.date, 1, 7) as month, a.cinemaName, a.avgViewBox * viewInfo as total
      from table a
      where a.cinemaName != '全国'
      and a.viewInfo not like '%万'
      
      union all
      
      select substr(a.date, 1, 7) as month, a.cinemaName,
      a.avgViewBox * SUBSTR(a.viewInfo, 1, LENGTH(a.viewInfo) - 3) * 10000 as total
      from table a
      where a.cinemaName != '全国'
      and a.viewInfo like '%万'
      
      ) b
      
      group by b.month, b.cinemaName
      
    • clickhouse,耗时:48ms
      select b.year_month, b.cinemaName, sum(b.total_info)
      
      from (
      
      select substr(m.date, 1, 7) as year_month , m.cinemaName , 
      multiply(toFloat32OrZero(m.avgViewBox), toInt32OrZero(m.viewInfo)) as total_info
      from test_db.table m 
      where viewInfo not like '%万'
      and cinemaName != '全国'
      
      union all
      
      select substr(m.date, 1, 7) as year_month, m.cinemaName,
      multiply(toFloat32OrZero(m.avgViewBox), 
      oInt32(multiply(toFloat32OrZero(substring(m.viewInfo, 1, LENGTH(m.viewInfo)-3)), 10000))) as total_info
      from test_db.table m 
      where viewInfo like '%万'
      and cinemaName != '全国'
      
      ) b
      
      group by b.year_month, b.cinemaName
      

Pyhton

  • 读取
from clickhouse_driver import Client


ch_host = ip
ch_port = port
ch_user = user
ch_passwd = pwd

sql = """
select * from test_db.test
"""
# sql = 'show databases;'

client = Client(host=ch_host, port=ch_port, user=ch_user, password=ch_passwd)

res = client.execute(query=sql)  #  list
  • 批量插入
arr = ['a', 'b', 'c', 'd', 'e']

data = []
for i in range(100):
    data.append([random.choice(arr), i])

try:
    client.execute('INSERT INTO test_db.test  VALUES', data, types_check=True)
except Exception as e:
    print('write data error: {}'.format(e))

OLAP及实时数仓架构

在这里插入图片描述
在这里插入图片描述

  • ODS 层:保存原始数据,对非结构化的数据进行结构化处理,轻度清洗,几乎不删除原始数据。来源:binlog 日志、埋点日志和应用程序日志
  • DWD 层:实时明细数据层,以业务过程作为建模驱动,基于每个具体的业务过程特点,构建最细粒度的明细层事实表;可以结合企业的数据使用特点,将明细事实表的某些重要维度属性字段做适当冗余,也即宽表化处理;
  • DM 层:以数据域+业务域的理念建设公共汇总层,对于DM层比较复杂,需要综合考虑对于数据落地的要求以及具体的查询引擎来选择不同的存储方式,分为轻度汇总层和高度汇总层,同时产出,高度汇总层数据用于前端比较简单的KV查询, 提升查询性能,比如实时大屏,实时报表等,数据的时效性要求为秒级,轻度汇总层Kafka中宽表实时写入OLAP存储引擎,用于前端产品复杂的OLAP查询场景,满足自助分析和产出复杂报表的需求,对数据的时效性要求可容忍到分钟级
  • pyflink

应用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值