Springboot集成ClickHouse及应用场景分析

ClickHouse应用场景:

1.绝大多数请求都是用于读访问的
2.数据需要以大批次(大于1000行)进行更新,而不是单行更新;或者根本没有更新操作
3.数据只是添加到数据库,没有必要修改
4.读取数据时,会从数据库中提取出大量的行,但只用到一小部分列
5.表很“宽”,即表中包含大量的列
6.查询频率相对较低(通常每台服务器每秒查询数百次或更少)
7.对于简单查询,允许大约50毫秒的延迟
8.列的值是比较小的数值和短字符串(例如,每个URL只有60个字节)
9.在处理单个查询时需要高吞吐量(每台服务器每秒高达数十亿行)
10.不需要事务
11.数据一致性要求较低
12.每次查询中只会查询一个大表。除了一个大表,其余都是小表
13.查询结果显著小于数据源。即数据有过滤或聚合。返回结果不超过单个服务器内存大小

行式存储对比列式存储:

(1)、行式数据

(2)、列式数据

(3)、对比分析

分析类查询,通常只需要读取表的一小部分列。在列式数据库中可以只读取需要的数据。数据总是打包成批量读取的,所以压缩是非常容易的。同时数据按列分别存储这也更容易压缩。这进一步降低了I/O的体积。由于I/O的降低,这将帮助更多的数据被系统缓存。

整合Springboot:

核心依赖(mybatis plus做持久层,druid做数据源):

<dependencies>
    <!--clickhouse-->
    <dependency>
        <groupId>ru.yandex.clickhouse</groupId>
        <artifactId>clickhouse-jdbc</artifactId>
        <version>0.3.1-patch</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.3.1</version>
    </dependency>
</dependencies>

配置yml文件:

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

mybatis-plus:
  mapper-locations: classpath*:mapper/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
    cache-enabled: true
    lazy-loading-enabled: true
    multiple-result-sets-enabled: true
    use-generated-keys: true
    default-statement-timeout: 60
    default-fetch-size: 100
  type-aliases-package: com.example.tonghp.entity

ClickHouse与Druid连接池配置类:

参数配置:

package com.example.tonghp.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
 * @author: tonghp
 * @create: 2021/07/26 16:23
 */
@Data
@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 ;
    private String username;
    private String password;
    // 省略 GET 和 SET
}

Druid连接池配置

package com.example.tonghp.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import javax.sql.DataSource;
import javax.swing.*;
/**
 * @author: tonghp
 * @create: 2021/07/26 16:22
 */
@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());
        datasource.setUsername(jdbcParamConfig.getUsername());
        datasource.setPassword(jdbcParamConfig.getPassword());
        return datasource;
    }
}

接下来配置实体类,mapper,service,controlle以及mapper.xml。与mybatisplus操作mysql一样的思路。

package com.example.tonghp.entity;

import lombok.Data;
import java.io.Serializable;
/**
 * @author: tonghp
 * @create: 2021/07/26 16:31
 */
@Data
public class UserInfo implements Serializable {
    private static final long serialVersionUID = 1L;
    private int id;
    private String userName;
    private String passWord;
    private String phone;
    private String email;
    private String createDay;
}
package com.example.tonghp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.tonghp.entity.UserInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
 * @author: tonghp
 * @create: 2021/07/26 16:32
 */
@Repository
public interface UserInfoMapper extends BaseMapper<UserInfo> {
    // 写入数据
    void saveData (UserInfo userInfo) ;
    // ID 查询
    UserInfo selectById (@Param("id") Integer id) ;
    // 查询全部
    List<UserInfo> selectList () ;
}

UserInfoMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.tonghp.mapper.UserInfoMapper">
    <resultMap id="BaseResultMap" type="com.example.tonghp.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.example.tonghp.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>

Service

package com.example.tonghp.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.tonghp.entity.UserInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @author: tonghp
 * @create: 2021/07/26 16:46
 */
public interface UserInfoService extends IService<UserInfo> {
    // 写入数据
    void saveData (UserInfo userInfo) ;
    // ID 查询
    UserInfo selectById (@Param("id") Integer id) ;
    // 查询全部
    List<UserInfo> selectList () ;
}

ServiceImpl

package com.example.tonghp.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tonghp.entity.UserInfo;
import com.example.tonghp.mapper.UserInfoMapper;
import com.example.tonghp.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
 * @author: tonghp
 * @create: 2021/07/26 16:48
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Autowired
    UserInfoMapper userInfoMapper;
    @Override
    public void saveData(UserInfo userInfo) {
        userInfoMapper.saveData(userInfo);
    }
    @Override
    public UserInfo selectById(Integer id) {
        return userInfoMapper.selectById(id);
    }
    @Override
    public List<UserInfo> selectList() {
        return userInfoMapper.selectList();
    }
}

Controller

package com.example.tonghp.controller;
import com.example.tonghp.entity.UserInfo;
import com.example.tonghp.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
/**
 * @author: tonghp
 * @create: 2021/07/26 16:45
 */
@RestController
@RequestMapping("user")
public class UserInfoController {
    @Autowired
    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() ;
    }
}

main()

package com.example.tonghp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.tonghp.mapper")
public class TonghpApplication {
    public static void main(String[] args) {
        SpringApplication.run(TonghpApplication.class, args);
    }
}

参考链接:

https://blog.csdn.net/weixin_46792649/article/details/115306384

https://www.cnblogs.com/ywjfx/p/14333974.html

https://www.cnblogs.com/cicada-smile/p/11632251.html

https://github.com/cicadasmile/middle-ware-parent

另外还有一种直接操作JDBC的:

https://blog.csdn.net/Alice_qixin/article/details/84957380

到此这篇关于Springboot集成ClickHouse的文章就介绍到这了,更多相关Springboot集成ClickHouse内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • springboot+mybatis配置clickhouse实现插入查询功能

    说明 ClickHouse 是一款用于大数据实时分析的列式数据库管理系统,在大数据量查询时有着非常优秀的性能, 但是也有缺点,就是不支持事务,不支持真正的删除 / 更新,所以笔者只演示插入和查询. 1.添加maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dep

  • SpringBoot2 整合 ClickHouse数据库案例解析

    一.ClickHouse简介 1.基础简介 Yandex开源的数据分析的数据库,名字叫做ClickHouse,适合流式或批次入库的时序数据.ClickHouse不应该被用作通用数据库,而是作为超高性能的海量数据快速查询的分布式实时处理平台,在数据汇总查询方面(如GROUP BY),ClickHouse的查询速度非常快. 2.数据分析能力 OLAP场景特征 · 大多数是读请求 · 数据总是以相当大的批(> 1000 rows)进行写入 · 不修改已添加的数据 · 每次查询都从数据库中读取大量的行,

  • springboot 整合 clickhouse的实现示例

    目录 前言 前置准备 使用jdbc方式操作clickhouse 与springboot的整合 代码完整整合步骤 前言 了解了clickhouse的基础概念和相关的理论之后,本篇将通过实例代码演示如何在Java代码中操作clickhouse,主要涉及的内容包括: 使用JDBC的方式操作clickhouseclickhouse与springboot的整合使用 前置准备 1.clickhouse服务确保已开启 2.为保证实验效果,提前创建一张表,并为该表插入一些实验数据 create table t_

  • Springboot集成ClickHouse及应用场景分析

    ClickHouse应用场景: 1.绝大多数请求都是用于读访问的2.数据需要以大批次(大于1000行)进行更新,而不是单行更新:或者根本没有更新操作3.数据只是添加到数据库,没有必要修改4.读取数据时,会从数据库中提取出大量的行,但只用到一小部分列5.表很“宽”,即表中包含大量的列6.查询频率相对较低(通常每台服务器每秒查询数百次或更少)7.对于简单查询,允许大约50毫秒的延迟8.列的值是比较小的数值和短字符串(例如,每个URL只有60个字节)9.在处理单个查询时需要高吞吐量(每台服务器每秒高达

  • SpringBoot集成EasyExcel的应用场景分析

    1.介绍 官网地址:https://www.yuque.com/easyexcel 特点: 1.Java领域解析.生成Excel比较有名的框架有Apache poi.jxl等.但他们都存在一个严重的问题就是 非常的耗内存.如果你的系统并发量不大的话可能还行,但是一旦并发上来后一定会OOM或 者JVM频繁的full gc. 2.EasyExcel是阿里巴巴开源的一个excel处理框架,以使用简单.节省内存著称.EasyExcel能大大减 少占用内存的主要原因是在解析Excel时没有将文件数据一次性

  • SpringBoot集成Redisson实现延迟队列的场景分析

    使用场景 1.下单成功,30分钟未支付.支付超时,自动取消订单 2.订单签收,签收后7天未进行评价.订单超时未评价,系统默认好评 3.下单成功,商家5分钟未接单,订单取消 4.配送超时,推送短信提醒 ...... 对于延时比较长的场景.实时性不高的场景,我们可以采用任务调度的方式定时轮询处理.如:xxl-job 今天我们采用一种比较简单.轻量级的方式,使用 Redis 的延迟队列来进行处理.当然有更好的解决方案,可根据公司的技术选型和业务体系选择最优方案.如:使用消息中间件Kafka.Rabbi

  • Springboot集成第三方jar快速实现微信、支付宝等支付场景

    前言 最近有个小型的活动外包项目,要集成一下支付功能,因为项目较小,按照微信官方文档的配置开发又极容易出错,加上个人又比较懒. 于是在gitee上找到一个封装好的各种支付场景业务,只需要自己将支付参数修改一下就能成功调起支付业务,实现真正的快速开发. 一.项目地址 官方网站:https://javen205.gitee.io/ijpay/ Gitee仓库: https://gitee.com/javen205/IJPay 官方示例程序源码:https://gitee.com/javen205/I

  • SpringBoot 过滤器、拦截器、监听器对比及使用场景分析

    一.关系图理解 二.区别 1.过滤器 过滤器是在web应用启动的时候初始化一次, 在web应用停止的时候销毁 可以对请求的URL进行过滤, 对敏感词过滤 挡在拦截器的外层 实现的是 javax.servlet.Filter 接口 ,是 Servlet 规范的一部分 在请求进入容器后,但在进入servlet之前进行预处理,请求结束是在servlet处理完以后 依赖Web容器 会多次执行 过滤器简介 过滤器的英文名称为 Filter, 是 Servlet 技术中最实用的技术.如同它的名字一样,过滤器

  • SpringBoot集成Mybatis-Plus多租户架构实现

    目录 一. 什么是多租户 二. 多租户架构以及数据隔离方案 1. 独立数据库 2. 共享数据库,独立 Schema 3. 共享数据库,共享 Schema,共享数据表 三.多租户架构适用场景? 四. 技术实现 正式进入主题 1. 创建Spring Boot项目 2. 单元测试 目前公司产品就是对外企业服务,入职后了解到SaaS模式和私有部署,当我第一次听到SaaS时,我不是很理解.经过查阅资料,以及在后续研发功能时,不断的加深了对多租户的理解. 那么接下来让我们问自己几个问题: 1.什么是多租户架

  • Springboot集成RabbitMQ死信队列的实现

    目录 关于死信队列 什么样的消息会进入死信队列? 场景分析 代码实现 场景模拟 生产者 消费者,设置死信队列监听 关于死信队列 在大多数的MQ中间件中,都有死信队列的概念.死信队列同其他的队列一样都是普通的队列.在RabbitMQ中并没有特定的"死信队列"类型,而是通过配置,将其实现. 当我们在创建一个业务的交换机和队列的时候,可以配置参数,指明另一个队列为当前队列的死信队列,在RabbitMQ中,死信队列(严格的说应该是死信交换机)被称为DLX Exchange.当消息"死

  • SpringBoot集成Mybatis并测试

    目录 1.SpringBoot链接druid连接池 2.SpringBoot集成Mybatis 2.1.引入Mybatis-generator 2.2.集成mybatis并测试 3.其它 3.1.自定义druid链接池 3.2.SpringBoot的Mybatis其它配置 首先我们先新建一个项目,需要选择以下依赖: 1.SpringBoot链接druid连接池 1.在pom文件中加入druid连接池场景启动器,如下所示: <!-- druid 场景启动器 --> <dependency&

  • SpringBoot集成P6Spy实现SQL日志的记录详解

    目录 P6Spy简介 应用场景 pom application.yml entity Mapper 启动类 测试类 P6Spy入门使用 spy.properties P6SPYConfig application.yml P6Spy简介 P6Spy是一个可以用来在应用程序中拦截和修改数据操作语句的开源框架. 通过P6Spy可以对SQL语句进行拦截,相当于一个SQL语句的记录器,这样我们可以用它来作相关的分析,比如性能分析. 应用场景 pom <dependencies> <depende

  • SpringBoot集成Zipkin实现分布式全链路监控

    Zipkin 简介 Zipkin is a distributed tracing system. It helps gather timing data needed to troubleshoot latency problems in service architectures. Features include both the collection and lookup of this data. If you have a trace ID in a log file, you ca

随机推荐