SpringBoot2 ClickHouse database integration, high-performance data analysis

This article Source: GitHub · Click here || GitEE · Click here

A, ClickHouse Profile

1. Introduction basis

Yandex open source data analysis of the database, called ClickHouse, time-series data for streaming or lots of storage. ClickHouse should not be used as a general-purpose database, but real-time distributed processing platform as an ultra-high performance mass data fast query data in aggregate query terms (such as GROUP BY), ClickHouse queries very quickly.

2, data analysis capabilities

  • OLAP scene features
· 大多数是读请求
· 数据总是以相当大的批(> 1000 rows)进行写入
· 不修改已添加的数据
· 每次查询都从数据库中读取大量的行,但是同时又仅需要少量的列
· 宽表,即每个表包含着大量的列
· 较少的查询(通常每台服务器每秒数百个查询或更少)
· 对于简单查询,允许延迟大约50毫秒
· 列中的数据相对较小: 数字和短字符串(例如,每个URL 60个字节)
· 处理单个查询时需要高吞吐量(每个服务器每秒高达数十亿行)
· 事务不是必须的
· 对数据一致性要求低
· 每一个查询除了一个大表外都很小
· 查询结果明显小于源数据,换句话说,数据被过滤或聚合后能够被盛放在单台服务器的内存中
  • Column data storage

(1), line data

SpringBoot2 ClickHouse database integration, high-performance data analysis

(2), columnar data

SpringBoot2 ClickHouse database integration, high-performance data analysis

(3) Comparative Analysis

Query analysis class, usually only a small portion of the table read column. It can be read only in the data column in the database. Data is always packaged into bulk read, so compression is very easy. While the data are stored in columns which are more easily compressed. This further reduces the volume of I / O. Since the reduction of I / O, which will help the system more data is cached.

Second, the integration framework SpringBoot

The case basis: Druid connection pooling and mybatis integration. Druid 1.1.10 version of SQL Parser provides support for the start clickhouse.

1, dependent on the core

<dependency>
    <groupId>ru.yandex.clickhouse</groupId>
    <artifactId>clickhouse-jdbc</artifactId>
    <version>0.1.53</version>
</dependency>

2, the data source associated

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    click:
      driverClassName: ru.yandex.clickhouse.ClickHouseDriver
      url: jdbc:clickhouse://127.0.0.1:8123/default
      initialSize: 10
      maxActive: 100
      minIdle: 10
      maxWait: 6000

3, Druid connection pool configuration

@Configuration
public class DruidConfig {
    @Resource
    private JdbcParamConfig jdbcParamConfig ;
    @Bean
    public DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(jdbcParamConfig.getUrl());
        datasource.setDriverClassName(jdbcParamConfig.getDriverClassName());
        datasource.setInitialSize(jdbcParamConfig.getInitialSize());
        datasource.setMinIdle(jdbcParamConfig.getMinIdle());
        datasource.setMaxActive(jdbcParamConfig.getMaxActive());
        datasource.setMaxWait(jdbcParamConfig.getMaxWait());
        return datasource;
    }
}

4, class parameters

@Component
@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 ;
    // 省略 GET 和 SET
}

Such integration code is complete.

Third, the operation case presentations

1, Mapper Interface

public interface UserInfoMapper {
    // 写入数据
    void saveData (UserInfo userInfo) ;
    // ID 查询
    UserInfo selectById (@Param("id") Integer id) ;
    // 查询全部
    List<UserInfo> selectList () ;
}

Here demonstrates a simple three 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" />
    </resultMap>
    <sql id="Base_Column_List">
        id,user_name,pass_word,phone,email,create_day
    </sql>
    <insert id="saveData" parameterType="com.click.house.entity.UserInfo" >
        INSERT INTO cs_user_info
        (id,user_name,pass_word,phone,email,create_day)
        VALUES
        (#{id,jdbcType=INTEGER},#{userName,jdbcType=VARCHAR},#{passWord,jdbcType=VARCHAR},
        #{phone,jdbcType=VARCHAR},#{email,jdbcType=VARCHAR},#{createDay,jdbcType=VARCHAR})
    </insert>
    <select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from cs_user_info
        where id = #{id,jdbcType=INTEGER}
    </select>
    <select id="selectList" resultMap="BaseResultMap" >
        select
        <include refid="Base_Column_List" />
        from cs_user_info
    </select>
</mapper>

Here is a string create_day way in converting to note here under.

3, the control layer interface

@RestController
@RequestMapping("/user")
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService ;
    @RequestMapping("/saveData")
    public String saveData (){
        UserInfo userInfo = new UserInfo () ;
        userInfo.setId(4);
        userInfo.setUserName("winter");
        userInfo.setPassWord("567");
        userInfo.setPhone("13977776789");
        userInfo.setEmail("winter");
        userInfo.setCreateDay("2020-02-20");
        userInfoService.saveData(userInfo);
        return "sus";
    }
    @RequestMapping("/selectById")
    public UserInfo selectById () {
        return userInfoService.selectById(1) ;
    }
    @RequestMapping("/selectList")
    public List<UserInfo> selectList () {
        return userInfoService.selectList() ;
    }
}

Fourth, the source address

GitHub·地址
https://github.com/cicadasmile/middle-ware-parent
GitEE·地址
https://gitee.com/cicadasmile/middle-ware-parent

SpringBoot2 ClickHouse database integration, high-performance data analysis

Guess you like

Origin blog.51cto.com/14439672/2444313