Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate ClickHouse Database in SpringBoot2

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

这篇文章主要介绍了SpringBoot2中如何整合ClickHouse数据库的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SpringBoot2中如何整合ClickHouse数据库文章都会有所收获,下面我们一起来看看吧。

一、ClickHouse简介1、基础简介

Yandex开源的数据分析的数据库,名字叫做ClickHouse,适合流式或批次入库的时序数据。ClickHouse不应该被用作通用数据库,而是作为超高性能的海量数据快速查询的分布式实时处理平台,在数据汇总查询方面(如GROUP BY),ClickHouse的查询速度非常快。

2、数据分析能力

OLAP场景特征

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

列式数据存储

(1)、行式数据

(2)、列式数据

(3)、对比分析

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

二、整合SpringBoot框架

该案例基于:Druid连接池和mybatis进行整合。Druid 1.1.10 版本 SQL Parser对clickhouse的开始提供支持。

1、核心依赖 ru.yandex.clickhouse clickhouse-jdbc 0.1.532、配属数据源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: 60003、Druid连接池配置@Configurationpublic 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、参数配置类@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}

这样整合代码就完成了。

三、操作案例演示1、Mapper接口public interface UserInfoMapper { // 写入数据 void saveData (UserInfo userInfo) ; // ID 查询 UserInfo selectById (@Param("id") Integer id) ; // 查询全部 List selectList () ;}

这里就演示简单的三个接口。

2、Mapper.xml文件 id,user_name,pass_word,phone,email,create_day 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}) select from cs_user_info where id = #{id,jdbcType=INTEGER} select from cs_user_info

这里 create_day 是以字符串的方式在转换,这里需要注意下。

3、控制层接口@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 selectList () { return userInfoService.selectList() ; }}关于"SpringBoot2中如何整合ClickHouse数据库"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"SpringBoot2中如何整合ClickHouse数据库"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report