SpringBoot2 integrates ClickHouse database to achieve high performance data query and analysis

This article source code: GitHub. Click here || GitEE. Click here

Introduction to ClickHouse

1. Basic Introduction

Yandex open source data analysis database, named ClickHouse, is suitable for streaming or batch warehousing of sequential data. ClickHouse should not be used as a general database, but as a distributed real-time processing platform for fast queries of massive data with super high performance. In data aggregation queries (such as GROUP BY), ClickHouse's queries are very fast.

2. Data Analysis Ability

  • OLAP Scenario Features
· Most are read requests
 · Data is always written in fairly large batches (> 1000 rows)
· Do not modify the added data
 · Each query reads a large number of rows from the database, but only a small number of columns are needed at the same time.
· Wide tables, that is, each table contains a large number of columns
 · Less queries (usually hundreds of queries per server per second or less)
· For simple queries, delays of about 50 milliseconds are allowed
 · Data in columns is relatively small: numbers and short strings (for example, 60 bytes per URL)
· Processing a single query requires high throughput (billions of rows per second per server)
· Transactions are not necessary
 · Low requirement for data consistency
 · Each query is small except for a large table
 · Query results are significantly smaller than source data, in other words, data can be filtered or aggregated and stored in the memory of a single server.
  • Formula Data Storage

(1) Linear data

(2) Formula data

(3) Contrastive analysis

Analytical class queries usually require only a small number of columns of the table to be read. Only the data needed can be read in the column database. Data is always packaged and read in batches, so compression is very easy. It is also easier to compress data by column. This further reduces the volume of I/O. As I/O decreases, this will help more data to be cached by the system.

Integrating SpringBook Framework

The case is based on the integration of Druid connection pool and mybatis. The Druid 1.1.10 version of SQL Parser supports the start of clickhouse.

1. Core Dependence


2. Accessory Data Source

    type: com.alibaba.druid.pool.DruidDataSource
      driverClassName: ru.yandex.clickhouse.ClickHouseDriver
      url: jdbc:clickhouse://
      initialSize: 10
      maxActive: 100
      minIdle: 10
      maxWait: 6000

3. Druid connection pool configuration

public class DruidConfig {
    private JdbcParamConfig jdbcParamConfig ;
    public DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();
        return datasource;

4. Parametric Configuration Class

@ConfigurationProperties(prefix = "spring.datasource.click")
public class JdbcParamConfig {
    private String driverClassName ;
    private String url ;
    private Integer initialSize ;
    private Integer maxActive ;
    private Integer minIdle ;
    private Integer maxWait ;
    // Eliminate GET and SET

This completes the integration of the code.

3. Demonstration of operation cases

1. Mapper interface

public interface UserInfoMapper {
    // Write data
    void saveData (UserInfo userInfo) ;
    // ID query
    UserInfo selectById (@Param("id") Integer id) ;
    // Query all
    List<UserInfo> selectList () ;

Here are three simple interfaces.

2. Mapper.xml file

<mapper namespace="com.click.house.mapper.UserInfoMapper">
    <resultMap id="BaseResultMap" type="com.click.house.entity.UserInfo">
        <id column="id" jdbcType="INTEGER" property="id" />
        <result column="user_name" jdbcType="VARCHAR" property="userName" />
        <result column="pass_word" jdbcType="VARCHAR" property="passWord" />
        <result column="phone" jdbcType="VARCHAR" property="phone" />
        <result column="email" jdbcType="VARCHAR" property="email" />
        <result column="create_day" jdbcType="VARCHAR" property="createDay" />
    <sql id="Base_Column_List">
    <insert id="saveData" parameterType="com.click.house.entity.UserInfo" >
        INSERT INTO cs_user_info
    <select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        <include refid="Base_Column_List" />
        from cs_user_info
        where id = #{id,jdbcType=INTEGER}
    <select id="selectList" resultMap="BaseResultMap" >
        <include refid="Base_Column_List" />
        from cs_user_info

Here create_day is converted as a string, which needs attention.

3. Control Layer Interface

public class UserInfoController {
    private UserInfoService userInfoService ;
    public String saveData (){
        UserInfo userInfo = new UserInfo () ;
        return "sus";
    public UserInfo selectById () {
        return userInfoService.selectById(1) ;
    public List<UserInfo> selectList () {
        return userInfoService.selectList() ;

4. Source code address


Tags: Java Database Druid github SQL

Posted on Tue, 08 Oct 2019 21:59:48 -0700 by ubersmuck