当前位置: 首页 > article >正文

ClickHouse--13--springboot+mybatis配置clickhouse

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • ClickHouse
    • 1.添加maven依赖
    • 2.配属数据源
    • 3.参数配置
    • 4.Druid连接池配置
    • 5.entity
    • 6.Mapper接口
    • 7.Mapper.xml
    • 8.controller接口
    • 9.创建一个clickhouse表
    • 10.测试


ClickHouse

ClickHouse是俄罗斯的Yandex公司于2016年开源的用于数据分析(OLAP)的面向列的数据库管理系统(DBMS),它也是一款 MPP(Massively Parallel Processing)架构的列式存储数据库,它能够使用SQL查询实时生成分析数据报告。非常适合进行 OLAP 分析。但是也有不适合的场景,比如不擅长按行删除数据虽然支持,不支持数据修改操作,也不支持事务(想要快就要舍弃事务处理)

1.添加maven依赖

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.13</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <!-- clickHouse数据库 -->
        <dependency>
            <groupId>ru.yandex.clickhouse</groupId>
            <artifactId>clickhouse-jdbc</artifactId>
            <version>0.1.53</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

2.配属数据源

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
clickhouse:
  driverClassName: ru.yandex.clickhouse.ClickHouseDriver
  url: jdbc:clickhouse://192.168.55.249:8123/default
  password: ck的密码
  initialSize: 10
  maxActive: 100
  minIdle: 10
  maxWait: 6000
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.wyu.tt16clickhouse.entity
server:
  port: 8090

3.参数配置


import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Data
@Component
public class ChParam {
    private String driverClassName;
    private String url;
    private String password;
    private Integer initialSize;
    private Integer maxActive;
    private Integer minIdle;
    private Integer maxWait;

    @Value("${clickhouse.driverClassName}")
    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    @Value("${clickhouse.url}")
    public void setUrl(String url) {
        this.url = url;
    }

    @Value("${clickhouse.password}")
    public void setPassword(String password) {
        this.password = password;
    }

    @Value("${clickhouse.initialSize}")
    public void setInitialSize(Integer initialSize) {
        this.initialSize = initialSize;
    }

    @Value("${clickhouse.maxActive}")
    public void setMaxActive(Integer maxActive) {
        this.maxActive = maxActive;
    }

    @Value("${clickhouse.minIdle}")
    public void setMinIdle(Integer minIdle) {
        this.minIdle = minIdle;
    }

    @Value("${clickhouse.maxWait}")
    public void setMaxWait(Integer maxWait) {
        this.maxWait = maxWait;
    }
}


4.Druid连接池配置

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class DruidConfig {

    @Autowired
    private ChParam chParam;

    @Bean
    public DataSource dataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(chParam.getUrl());
        datasource.setDriverClassName(chParam.getDriverClassName());
        datasource.setInitialSize(chParam.getInitialSize());
        datasource.setMinIdle(chParam.getMinIdle());
        datasource.setMaxActive(chParam.getMaxActive());
        datasource.setMaxWait(chParam.getMaxWait());
        datasource.setPassword(chParam.getPassword());
        return datasource;
    }
}


5.entity

import lombok.Data;
import java.io.Serializable;
import java.util.Date;

@Data
public class User implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer userId;

    private String appId;

    private String version;

    private Date regTime;
}

6.Mapper接口

@Mapper
public interface UserMapper {

    List<User> queryUser();

    Integer insertUser(User user);

}

7.Mapper.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.wyu.tt16clickhouse.UserMapper">

    <select id="queryUser" resultType="com.wyu.tt16clickhouse.entity.User">
        select userId, appId, version, regTime from `default`.`user`
    </select>

    <insert id="insertUser" parameterType="com.wyu.tt16clickhouse.entity.User">
         INSERT INTO `default`.`user` (`userId`, `appId`, `version`, `regTime`)
         VALUES (#{userId}, #{appId}, #{version}, #{regTime})
    </insert>
</mapper>

8.controller接口

@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/queryUser")
    public Object query(){
        List userList = userMapper.queryUser();
        log.info(userList.toString());
        return userList.toString();
    }

    @RequestMapping("/insertUser")
    public Object insertUser(){
        User user = new User();
        user.setAppId("SS");
        user.setRegTime(new Date());
        user.setUserId(777744);
        user.setVersion("3.2");
        Integer flag = userMapper.insertUser(user);
        return flag;
    }
}


9.创建一个clickhouse表

create table user
(
    userId  Int32,
    appId   String,
    version String,
    regTime Date
)
    engine = MergeTree PARTITION BY toYYYYMM(regTime) ORDER BY userId SETTINGS index_granularity = 8192;
    
INSERT INTO default.user (userId, appId, version, regTime) VALUES (123457, 'RR', '3.6', '2020-01-07');
INSERT INTO default.user (userId, appId, version, regTime) VALUES (43234, 'HH', '2.5', '2020-06-06');
INSERT INTO default.user (userId, appId, version, regTime) VALUES (1234, 'TT', '2.4', '2020-07-24');
INSERT INTO default.user (userId, appId, version, regTime) VALUES (12345, 'RR', '2.5', '2020-07-29');
INSERT INTO default.user (userId, appId, version, regTime) VALUES (123456, 'TT', '2.1', '2020-07-09');
INSERT INTO default.user (userId, appId, version, regTime) VALUES (234561, 'GG', '3.0', '2020-07-31');  

10.测试

在这里插入图片描述


http://www.kler.cn/a/274370.html

相关文章:

  • 【自动化】Python SeleniumUtil 工具 开启开发者模式 自动安装油猴用户脚本等
  • JAVA开发时获取用户信息失败,分析后端日志信息
  • unity接入coze智能体
  • 省略内容在句子中间
  • vue3+TS+vueX的记录
  • 无人机航测VS传统测绘
  • 红与黑(c++题解)
  • 【复现】【免费】基于多时间尺度滚动优化的多能源微网双层调度模型
  • springboot校服订购系统
  • 阿里云发布 AI 编程助手 “通义灵码”——VSCode更强了 !!
  • 考研失败, 学点Java打小工_Day3_卫语句_循环
  • 阿里云2核4G4M轻量应用服务器价格165元一年
  • [QJS xmake] 非常简单地在Windows下编译QuickJS!
  • MySQL数据自动同步到Es
  • 关系数据库:关系数据结构基础与概念解析
  • 代码随想录算法训练营第二十八天|93. 复原 IP 地址,78. 子集,90. 子集 II
  • GPT能复制人类的决策和直觉吗?
  • Vue async (type = 0) => {}代码讲解
  • 前端 - 基础 表单标签 -- 表单元素( input - type属性) 文本框和密码框
  • Android逆向(二)-系统调试开关
  • 【深度学习】深度学习md笔记总结第1篇:深度学习课程,要求【附代码文档】
  • 数据资产管理解决方案:构建高效、安全的数据生态体系
  • Linux之线程同步
  • 系统运维中出现的问题,问题原因,解决办法
  • PB-03F模组蓝牙基础+主从机指令的使用
  • mapstruct学习笔记-pojo之间的转换