• springBoot集成ClickHouse


    在上一节中Linux中安装clickhouse

    我们安装好了Clickhouse,接下来我们就要集成到项目中。

    一:在Ck中新建一个表

    1. create table user
    2. (
    3. userId Int32,
    4. appId String,
    5. version String,
    6. regTime Date
    7. )
    8. engine = MergeTree PARTITION BY toYYYYMM(regTime) ORDER BY userId SETTINGS index_granularity = 8192;
    9. INSERT INTO default.user (userId, appId, version, regTime) VALUES (1, 'RR', '3.6', '2022-08-26');

    这样后面就可以新增数据。由于其不擅长按行删除数据虽然支持,不支持数据修改操作,也不支持事务(想要快就要舍弃事务处理)。适合做OLAP数据分析。所以我们主要用于查询和新增。接下来就是整个过程。

    二:引入jar包

    1. <dependency>
    2. <groupId>ru.yandex.clickhouse</groupId>
    3. <artifactId>clickhouse-jdbc</artifactId>
    4. <version>0.1.53</version>
    5. </dependency>

    三:准备配置文件

    单独写一个配置文件,不过需要在application.properties中添加引用。

    spring.profiles.include=clickhouse

    准备一个application-clickhouse.properties的配置文件

    1. //连接池
    2. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
    3. //ck连接
    4. clickhouse.driverClassName=ru.yandex.clickhouse.ClickHouseDriver
    5. clickhouse.url=jdbc:clickhouse://ip:8123
    6. clickhouse.username=default
    7. clickhouse.password=password
    8. clickhouse.db=system
    9. clickhouse.socketTimeout=600000
    10. clickhouse.initialSize=10
    11. clickhouse.maxActive=100
    12. clickhouse.minIdle=10
    13. clickhouse.maxWait=6000
    14. //mapper
    15. mybatis.mapper-locations=classpath:mapping/mysql/*Mapper.xml
    16. mybatis.type-aliases-package=com.zhm.entity

    四:数据源配置

    先写一个配置po

    1. package com.zhm.config;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.context.annotation.PropertySource;
    4. import org.springframework.stereotype.Component;
    5. /**
    6. * @create 2022/8/26 14:43
    7. */
    8. @Component
    9. @PropertySource("classpath:application-clickhouse.properties")
    10. public class ChParam {
    11. private String driverClassName;
    12. private String url;
    13. private String password;
    14. private Integer initialSize;
    15. private Integer maxActive;
    16. private Integer minIdle;
    17. private Integer maxWait;
    18. @Value("${clickhouse.driverClassName}")
    19. public void setDriverClassName(String driverClassName) {
    20. this.driverClassName = driverClassName;
    21. }
    22. @Value("${clickhouse.url}")
    23. public void setUrl(String url) {
    24. this.url = url;
    25. }
    26. @Value("${clickhouse.password}")
    27. public void setPassword(String password) {
    28. this.password = password;
    29. }
    30. @Value("${clickhouse.initialSize}")
    31. public void setInitialSize(Integer initialSize) {
    32. this.initialSize = initialSize;
    33. }
    34. @Value("${clickhouse.maxActive}")
    35. public void setMaxActive(Integer maxActive) {
    36. this.maxActive = maxActive;
    37. }
    38. @Value("${clickhouse.minIdle}")
    39. public void setMinIdle(Integer minIdle) {
    40. this.minIdle = minIdle;
    41. }
    42. @Value("${clickhouse.maxWait}")
    43. public void setMaxWait(Integer maxWait) {
    44. this.maxWait = maxWait;
    45. }
    46. public String getDriverClassName() {
    47. return driverClassName;
    48. }
    49. public String getUrl() {
    50. return url;
    51. }
    52. public String getPassword() {
    53. return password;
    54. }
    55. public Integer getInitialSize() {
    56. return initialSize;
    57. }
    58. public Integer getMaxActive() {
    59. return maxActive;
    60. }
    61. public Integer getMinIdle() {
    62. return minIdle;
    63. }
    64. public Integer getMaxWait() {
    65. return maxWait;
    66. }
    67. }

    在写一个DruidConfig

    1. package com.zhm.config;
    2. /**
    3. * @create 2022/8/26 14:46
    4. */
    5. import com.alibaba.druid.pool.DruidDataSource;
    6. import org.springframework.beans.factory.annotation.Autowired;
    7. import org.springframework.context.annotation.Bean;
    8. import org.springframework.context.annotation.Configuration;
    9. import javax.sql.DataSource;
    10. @Configuration
    11. public class DruidConfig {
    12. @Autowired
    13. private ChParam chParam;
    14. @Bean
    15. public DataSource dataSource() {
    16. DruidDataSource datasource = new DruidDataSource();
    17. datasource.setUrl(chParam.getUrl());
    18. datasource.setDriverClassName(chParam.getDriverClassName());
    19. datasource.setInitialSize(chParam.getInitialSize());
    20. datasource.setMinIdle(chParam.getMinIdle());
    21. datasource.setMaxActive(chParam.getMaxActive());
    22. datasource.setMaxWait(chParam.getMaxWait());
    23. datasource.setPassword(chParam.getPassword());
    24. return datasource;
    25. }
    26. }

    五:准备POJO

    由于一中已经确定了表名和列名所以pojo就可以确定。而且三:准备配置文件 中也确定了其位置

    mybatis.type-aliases-package=com.zhm.entity
    所以需要在com.zhm.entity下创建一个pojo

    1. package com.zhm.entity;
    2. import com.fasterxml.jackson.annotation.JsonFormat;
    3. import java.util.Date;
    4. /**
    5. * @create 2022/8/26 14:52
    6. */
    7. public class ClickUser {
    8. private Integer userId;
    9. private String appId;
    10. private String version;
    11. @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    12. private Date regTime;
    13. public Integer getUserId() {
    14. return userId;
    15. }
    16. public void setUserId(Integer userId) {
    17. this.userId = userId;
    18. }
    19. public String getAppId() {
    20. return appId;
    21. }
    22. public void setAppId(String appId) {
    23. this.appId = appId;
    24. }
    25. public String getVersion() {
    26. return version;
    27. }
    28. public void setVersion(String version) {
    29. this.version = version;
    30. }
    31. public Date getRegTime() {
    32. return regTime;
    33. }
    34. public void setRegTime(Date regTime) {
    35. this.regTime = regTime;
    36. }
    37. }

    六:准备Mapper

    由于在三的配置文件中已经确定了引用位置

    mybatis.mapper-locations=classpath:mapping/mysql/*Mapper.xml

    所以 需要把Mapper.xml放到resource下的mapping/mysql/目录下。

    1. "1.0" encoding="UTF-8"?>
    2. "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    3. "com.zhm.mapper.ClickUserMapper">
    4. "insertUser" parameterType="com.zhm.entity.ClickUser">
    5. INSERT INTO `default`.`user` (`userId`, `appId`, `version`, `regTime`)
    6. VALUES (#{userId}, #{appId}, #{version}, #{regTime})

    有了mapper.xml必然有mapper

    1. import com.zhm.entity.ClickUser;
    2. import java.util.List;
    3. /**
    4. * @create 2022/8/26 14:47
    5. */
    6. public interface ClickUserMapper {
    7. List queryUser();
    8. void insertUser(ClickUser clickUser);
    9. }

    七:使用

    先准备一个service

    1. /**
    2. * @create 2022/8/5 9:35
    3. */
    4. @Service
    5. public class ChickHouserService {
    6. private static final Logger logger =LoggerFactory.getLogger(ChickHouserService.class);
    7. @Resource
    8. private ClickUserMapper clickUserMapper;
    9. public Result getMapperData(ClickUser clickUser) {
    10. logger.info("进入操作");
    11. try {
    12. logger.info("===========先查询============");
    13. List list= clickUserMapper.queryUser();
    14. logger.info("clickhouse查询结果为:{}",JSONObject.toJSON(list));
    15. logger.info("===========在新增============");
    16. clickUserMapper.insertUser(clickUser);
    17. logger.info("===========再次查询============");
    18. List rlist= clickUserMapper.queryUser();
    19. logger.info("clickhouse查询结果为:{}",JSONObject.toJSON(rlist));
    20. return Result.sendSuccess("操作结束");
    21. } catch (Exception e) {
    22. logger.error("操作异常,{}", e);
    23. return Result.sendFailure("操作异常");
    24. }
    25. }
    26. }

    最后写一个通用的controller

    1. @Resource
    2. private ChickHouserService chickHouserService ;
    3. @ApiOperation(value = "处理表里的数据,标记去年的数据今年有没有买")
    4. @RequestMapping(value = "/checkLastData", method = RequestMethod.POST)
    5. public Result index(@RequestBody ClickUser clickUser) {
    6. return chickHouserService .getMapperData(clickUser);
    7. }

    这样整个就完成了springBoot集成clickhouse了

  • 相关阅读:
    为什么说做亚马逊站外引流必须有独立站着陆页
    jQuery事件与特效
    python 装饰器
    生产环境调试APNS的方式(测试同学说线上APP突然接收不到通知了。)
    数学建模--K-means聚类的Python实现
    数字创新的风口:创业者如何在Web3时代抢占先机
    怎么压缩pdf文件大小?详细压缩步骤
    java计算机毕业设计跨境电商网站源码+系统+数据库+lw文档+mybatis+运行部署
    TS编译选项——编译TS文件同时对JS文件进行编译
    SAP UI5 Page 控件的构造函数参数讲解
  • 原文地址:https://blog.csdn.net/wszhm123/article/details/126544278