springboot+mybatis配置clickhouse實(shí)現(xiàn)插入查詢功能
說明
ClickHouse 是一款用于大數(shù)據(jù)實(shí)時分析的列式數(shù)據(jù)庫管理系統(tǒng),在大數(shù)據(jù)量查詢時有著非常優(yōu)秀的性能,
但是也有缺點(diǎn),就是不支持事務(wù),不支持真正的刪除 / 更新,所以筆者只演示插入和查詢。
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數(shù)據(jù)庫 --> <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、配屬數(shù)據(jù)源
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource clickhouse: driverClassName: ru.yandex.clickhouse.ClickHouseDriver url: jdbc:clickhouse://106.55.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、參數(shù)配置
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、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>
6、Mapper接口
@Mapper
public interface UserMapper {
List<User> queryUser();
Integer insertUser(User user);
}
7.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;
}
}
8.創(chuàng)建一個clickhouse表,然后插入幾條數(shù)據(jù)測試一下
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');
9.測試

參考文章:SpringBoot2 整合 ClickHouse數(shù)據(jù)庫案例解析
總結(jié)
到此這篇關(guān)于springboot+mybatis配置clickhouse實(shí)現(xiàn)插入查詢功能的文章就介紹到這了,更多相關(guān)springboot+mybatis配置clickhouse內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Intellij IDEA 2017 debug斷點(diǎn)調(diào)試技巧(總結(jié))
這篇文章主要介紹了詳解Intellij IDEA 2017 debug斷點(diǎn)調(diào)試技巧(總結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳
這篇文章主要介紹了關(guān)于Java從本地文件復(fù)制到網(wǎng)絡(luò)文件上傳,File?和?IO?流其實(shí)是很相似的,都是將文件從一個地方轉(zhuǎn)移到另一個地方,這也是流的特點(diǎn)之一,需要的朋友可以參考下2023-04-04
詳解關(guān)于Windows10 Java環(huán)境變量配置問題的解決辦法
這篇文章主要介紹了關(guān)于Windows10 Java環(huán)境變量配置問題的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
SpringBoot集成Druid監(jiān)控頁面最小化配置操作
這篇文章主要介紹了SpringBoot集成Druid監(jiān)控頁面最小化配置操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
面試必問項(xiàng)之Set實(shí)現(xiàn)類:TreeSet
這篇文章主要介紹了Java TreeSet類的簡單理解和使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2021-07-07

