在springboot中如何集成clickhouse進(jìn)行讀寫操作
上篇文章講了如何在docker中搭建clickhouse,本篇記錄一下在springboot中如何集成clickhouse并進(jìn)行讀寫
1、引入依賴
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--升級 druid驅(qū)動 1.1.10支持ClickHouse-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.13</version>
</dependency>2、編寫數(shù)據(jù)源配置
@Configuration
public class DruidConfig {
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:clickhouse://localhost:8123/test");
dataSource.setInitialSize(10);
dataSource.setMaxActive(100);
dataSource.setMinIdle(10);
dataSource.setMaxWait(-1);
return dataSource;
}
}3、編寫表實體類
和mysql一模一樣寫法,對應(yīng)的表我通過DBeaver已經(jīng)創(chuàng)建好了

package cn.yufire.sync.sls.getway.logs.pojo;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.*;
import java.io.Serializable;
@Builder
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "getway_logs")
@Data
public class GetwayLogs implements Serializable {
/**
* id
*/
@TableField(value = "id")
private Long id;
/**
* 接口名稱:網(wǎng)關(guān)上定義的名稱
*/
@TableField(value = "api_name")
private String apiName;
/**
* 響應(yīng)體
*/
@TableField(value = "response_body")
private String responseBody;
/**
* 調(diào)用環(huán)境:TEST、RELEASE、PRE
*/
@TableField(value = "api_stage_name")
private String apiStageName;
/**
* 錯誤碼
*/
@TableField(value = "error_code")
private String errorCode;
/**
* 請求類型
*/
@TableField(value = "http_method")
private String httpMethod;
/**
* 接口請求地址
*/
@TableField(value = "api_path")
private String apiPath;
/**
* 接口請求全地址
*/
@TableField(value = "api_full_path")
private String apiFullPath;
/**
* 請求時間
*/
@TableField(value = "request_time")
private String requestTime;
/**
* 請求體
*/
@TableField(value = "request_body")
private String requestBody;
/**
* 網(wǎng)關(guān)請求阿里云創(chuàng)建
*/
@TableField(value = "getway_request_id")
private String getwayRequestId;
/**
* 阿里云網(wǎng)關(guān)應(yīng)用id
*/
@TableField(value = "getway_app_id")
private String getwayAppId;
/**
* 請求協(xié)議,HTTP、HTTPS、...
*/
@TableField(value = "request_protocol")
private String requestProtocol;
/**
* 客戶端調(diào)用產(chǎn)生的隨機字符串
*/
@TableField(value = "client_nonce")
private String clientNonce;
/**
* 網(wǎng)關(guān)應(yīng)用名稱
*/
@TableField(value = "getway_app_name")
private String getwayAppName;
/**
* 網(wǎng)關(guān)分組id
*/
@TableField(value = "getway_group_id")
private String getwayGroupId;
/**
* 客戶端ip
*/
@TableField(value = "client_ip")
private String clientIp;
/**
* 網(wǎng)關(guān)綁定域名
*/
@TableField(value = "getway_bind_domain")
private String getwayBindDomain;
/**
* 請求體大小
*/
@TableField(value = "request_size")
private Integer requestSize;
/**
* 響應(yīng)體大小
*/
@TableField(value = "response_size")
private Integer responseSize;
/**
* 后端應(yīng)用響應(yīng)HTTP狀態(tài)碼
*/
@TableField(value = "app_response_code")
private Integer appResponseCode;
/**
* 分組名稱
* */
@TableField(value = "apiGroupName")
private String apiGroupName;
}
4、創(chuàng)建一個mapper類進(jìn)行增刪改查
因為我使用clickhouse只需要批量插入,所以就寫了一個批量插入的sql,當(dāng)然mybatis-plus也有自帶的批量插入的方法,但是不是一條sql執(zhí)行的,而是通過批量執(zhí)行多條 SQL 語句來實現(xiàn)的,所以就手寫了一個批次插入的sql,這里有一個坑,批量插入clickhouse會報錯,單條插入沒問題,百度了發(fā)現(xiàn)批量插入各種小問題,需要在 Values前加一個FORMAT ,即 insert into x(xx,xx) FORMAT Values (),處理后就沒有報錯了
package cn.yufire.sync.sls.getway.logs.mapper;
import cn.yufire.sync.sls.getway.logs.pojo.GetwayLogs;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import java.util.List;
public interface GetwayLogsMapper extends BaseMapper<GetwayLogs> {
/**
* 批量插入網(wǎng)關(guān)日志
*
* @param list 數(shù)據(jù)
* @return
*/
@Insert("<script>insert into getway_logs(" +
"api_name," +
"response_body," +
"api_stage_name," +
"error_code," +
"http_method," +
"api_path," +
"api_full_path," +
"request_time," +
"request_body," +
"getway_request_id," +
"getway_app_id," +
"request_protocol," +
"client_nonce," +
"getway_app_name," +
"getway_group_id," +
"client_ip," +
"getway_bind_domain," +
"request_size," +
"response_size," +
"app_response_code" +
") FORMAT Values " +
" <foreach collection=\"list\" item=\"item\" index=\"index\" separator=\",\">\n" +
" (" +
"#{item.apiName}," +
"#{item.responseBody}," +
"#{item.apiStageName}," +
"#{item.errorCode}," +
"#{item.httpMethod}," +
"#{item.apiPath}," +
"#{item.apiFullPath}," +
"#{item.requestTime}," +
"#{item.requestBody}," +
"#{item.getwayRequestId}," +
"#{item.getwayAppId}," +
"#{item.requestProtocol}," +
"#{item.clientNonce}," +
"#{item.getwayAppName}," +
"#{item.getwayGroupId}," +
"#{item.clientIp}," +
"#{item.getwayBindDomain}," +
"#{item.requestSize}," +
"#{item.responseSize}," +
"#{item.appResponseCode}" +
")\n" +
" </foreach>" +
"</script>")
Integer batchInsert(List<GetwayLogs> list);
}
在業(yè)務(wù)中調(diào)用
@Autowired
private GetwayLogsMapper getwayLogsMapper;
log.info("批量插入至數(shù)據(jù)庫中...,是否添加成功:{}", getwayLogsMapper.batchInsert(logs));
可以看到增刪改查操作和操作mysql的一模一樣,也就是說給clickhouse當(dāng)成mysql用就行

在springboot中集成clickhouse中也遇到了一些報錯
ClickHouse exception, code: 1002
使用了clickhouse的依賴和jpa依賴總是跑不起來,一直報沒有驅(qū)動,給了驅(qū)動也不對
clickhouse集成springboot報錯Unable to determine Dialect to use [name=ClickHouse, majorVersion=22]; user must register resolver or explicitly set 'hibernate.dialect'
百度了各種最后看到這個,直接給clickhouse的依賴和jpa的依賴去掉就給clickhouse當(dāng)成mysql結(jié)果就正常了

總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
JDK8的lambda方式List轉(zhuǎn)Map的操作方法
account是一個返回本身的lambda表達(dá)式,其實還可以使用Function接口中的一個默認(rèn)方法代替,使整個方法更簡潔優(yōu)雅,這篇文章主要介紹了JDK8的lambda方式List轉(zhuǎn)Map,需要的朋友可以參考下2022-07-07
SpringBoot+Vue+JWT的前后端分離登錄認(rèn)證詳細(xì)步驟
這篇文章主要介紹了SpringBoot+Vue+JWT的前后端分離登錄認(rèn)證,其實創(chuàng)建后端springboot工程也很簡單,本文安裝idea步驟一步步給大家詳細(xì)介紹,需要的朋友可以參考下2021-09-09
Java生產(chǎn)者和消費者例子_動力節(jié)點Java學(xué)院整理
生產(chǎn)者-消費者(producer-consumer)問題,也稱作有界緩沖區(qū)(bounded-buffer)問題,兩個進(jìn)程共享一個公共的固定大小的緩沖區(qū)。下文通過實例給大家介紹java生產(chǎn)者和消費者,感興趣的朋友一起學(xué)習(xí)吧2017-05-05
springmvc利用jquery.form插件異步上傳文件示例
本篇文章主要介紹了springmvc利用jquery.form插件異步上傳文件示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01
Java實現(xiàn)矩陣順時針旋轉(zhuǎn)90度的示例
今天小編就為大家分享一篇Java實現(xiàn)矩陣順時針旋轉(zhuǎn)90度的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
圖解Java經(jīng)典算法折半查找的原理與實現(xiàn)
折半查找法也叫做?分查找,顧名思義就是把數(shù)據(jù)分成兩半,再判斷所查找的key在哪?半中,再重復(fù)上述步驟知道找到?標(biāo)key,下面這篇文章主要介紹了圖解Java經(jīng)典算法折半查找的原理與實現(xiàn)2022-09-09

