需求:项目中要使用ClickHouse做数据库。
具体实现:
1,在pom.xml中添加clickhouse依赖
bash
<!-- https://mvnrepository.com/artifact/com.clickhouse/clickhouse-jdbc -->
<dependency>
<groupId>com.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.3.2</version>
</dependency>
2, 配置文件中添加clickhouse的配置属性:
bash
spring:
datasource:
clickhouse:
driverClassName: ru.yandex.clickhouse.ClickHouseDirver
url: jdbc:clickhouse:IP:端口号/数据库
user: 用户名
password: 密码
#这部分配置用于kerberos认证
clickhouse:
security:
enabled: true
isMachineUser: false
3,配置Clickhouse连接
bash
@Configuration
public class ClickHouseConfig {
@Value("{spring.datasource.clickhouse.url}")
private String clickhouseUrl;
@Value("{spring.datasource.clickhouse.user}")
private String clickhouseUser;
@Value("{spring.datasource.clickhouse.password}")
private String clickhousePassword;
@Value("{clickhouse.seurity.enabled}")
private Boolean isSecurity;
@Value("{clickhouse.isMachineUser}")
private Boolean isMachineUser;
@Bean
public DataSource dataSource() {
ClickHouseProperties clickHouseProperties = new ClickHouseProperties();
clickHouseProperties .setSocketTimeout(60000);
//下面三行用于开启kerberos认证
if(isSecurity & isMachineUser) {
clickHouseProperties.setMachineUser(true);
clickHouseProperties.setMachineUserKeytabPath(System.getProperty("user.dir") + File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator + "user.keytab");
}
clickHouseProperties.setSsl(true);
clickHouseProperties.setSslMode("none");
clickHouseProperties.setUser(clickhouseUser);
clickHouseProperties.setPassword(clickhousePassword);
ClickHouseDataSource clickHouseDataSource = new ClickHouseDataSource (clickhouseUrl, clickHouseProperties);
return clickHouseDataSource;
}
}
4,实现具体的Mapper。
其他的操作就跟mysql的操作一致了。
注意:clickhouse跟mysql的SQL语法不一致。