ClickHouse大数据领域企业级应用实践和探索总结
共 14189字,需浏览 29分钟
· 2021-01-15
点击上方蓝色字体,选择“设为星标”
回复”资源“获取更多资源
ClickHouse简介
为什么ClickHouse能够异军突起
Vertica的2.63倍(Vertica是一款收费的列式存储数据库)
InfiniDB的17倍(可伸缩的分析数据库引擎,基于Mysql搭建)
MonetDB的27倍(开源的列式数据库)
Hive的126倍
MySQL的429倍
Greenplum的10倍
Spark的1倍
ROLAP(关系型的联机分析处理,和它一起比较的还有OLTP联机事务处理,我们常见的ERP,CRM系统就属于OLTP)
在线实时查询
完整的DBMS(关系数据库)
列式存储(区别与HBase,ClickHouse的是完全列式存储,HBase具体说是列族式存储)
不需要任何数据预处理
支持批量更新
拥有完善的SQl支持和函数
支持高可用(多主结构,在后面的结构设计中会讲到)
不依赖Hadoop复杂生态(像ES一样,开箱即用)
不支持事务(这其实也是大部分OLAP数据库的缺点)
不擅长根据主键按行粒度查询(但是支持这种操作)
不擅长按行删除数据(但是支持这种操作)
核心概念和原理
Shard :集群内划分为多个分片或分组(Shard 0 … Shard N),通过 Shard 的线性扩展能力,支持海量数据的分布式存储计算。
Node :每个 Shard 内包含一定数量的节点(Node,即进程),同一 Shard 内的节点互为副本,保障数据可靠。ClickHouse 中副本数可按需建设,且逻辑上不同 Shard 内的副本数可不同。
ZooKeeper Service :集群所有节点对等,节点间通过 ZooKeeper 服务进行分布式协调。
Column与Field
DataType
Block与Block流
第一类用于处理数据定义的DDL操作
第二类用于处理关系运算的相关操作
第三类则是与表引擎呼应,每一种表引擎都拥有与之对应的BlockInputStream实现
Table
Parser与Interpreter
Functions 与Aggregate Functions
Cluster与Replication
ClickHouse的核心特性
简单入门
ru.yandex.clickhouse
clickhouse-jdbc
0.2.4
com.github.housepower
clickhouse-native-jdbc
2.5.2
驱动类加载路径不同,分别为 ru.yandex.clickhouse.ClickHouseDriver 和 com.github.housepower.jdbc.ClickHouseDriver
默认连接端口不同,分别为 8123 和 9000
连接协议不同,官方驱动使用 HTTP 协议,而三方驱动使用 TCP 协议
Class.forName("com.github.housepower.jdbc.ClickHouseDriver");
Connection connection = DriverManager.getConnection("jdbc:clickhouse://192.168.60.131:9000");
Statement statement = connection.createStatement();
statement.executeQuery("create table test.example(day Date, name String, age UInt8) Engine=Log");
PreparedStatement pstmt = connection.prepareStatement("insert into test.example values(?, ?, ?)");
// insert 10 records
for (int i = 0; i < 10; i++) {
pstmt.setDate(1, new Date(System.currentTimeMillis()));
pstmt.setString(2, "panda_" + (i + 1));
pstmt.setInt(3, 18);
pstmt.addBatch();
}
pstmt.executeBatch();
Statement statement = connection.createStatement();
String sql = "select * from test.jdbc_example";
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
// ResultSet 的下标值从 1 开始,不可使用 0,否则越界,报 ArrayIndexOutOfBoundsException 异常
System.out.println(rs.getDate(1) + ", " + rs.getString(2) + ", " + rs.getInt(3));
}
ck-master :) show tables;
SHOW TABLES
┌─name─────────┐
│ hits │
│ jdbc_example │
└──────────────┘
ck-master :) select * from example;
SELECT *
FROM jdbc_example
┌────────day─┬─name─────┬─age─┐
│ 2019-04-25 │ panda_1 │ 18 │
│ 2019-04-25 │ panda_2 │ 18 │
│ 2019-04-25 │ panda_3 │ 18 │
│ 2019-04-25 │ panda_4 │ 18 │
│ 2019-04-25 │ panda_5 │ 18 │
│ 2019-04-25 │ panda_6 │ 18 │
│ 2019-04-25 │ panda_7 │ 18 │
│ 2019-04-25 │ panda_8 │ 18 │
│ 2019-04-25 │ panda_9 │ 18 │
│ 2019-04-25 │ panda_10 │ 18 │
└────────────┴──────────┴─────┘
企业级应用
清空A_temp表,将最新的数据从Hive通过ETL导入到A_temp表
将A rename 成A_temp_temp
将A_temp rename成 A
将A_temp_temp rename成 A_tem
清空A_temp表,将最近3个月的数据从Hive通过ETL导入到A_temp表
将A表中3个月之前的数据select into到A_temp表
将A rename 成A_temp_temp
将A_temp rename成 A
将A_temp_temp rename成 A_tem
趣头条实战 | 基于Flink+ClickHouse构建实时数据平台