一、加入clickhouse jar包依赖
ru.yandex.clickhouse
clickhouse-jdbc
0.1.53
二、配置Clickhouse数据库连接属性配置文件,clickhouse默认没有密码
spring:
datasource:
clickhouse:
address: jdbc:clickhouse://127.0.0.1:8123
username: default
password:
db: system
socketTimeout: 600000
三、编写Clickhouse数据库连接操作工具类
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import ru.yandex.clickhouse.ClickHouseConnection;
import ru.yandex.clickhouse.ClickHouseDataSource;
import ru.yandex.clickhouse.settings.ClickHouseProperties;
import java.sql.*;
import java.util.*;
@Slf4j
@Component
public class ClickHouseConfig {
private static String clickhouseAddress;
private static String clickhouseUsername;
private static String clickhousePassword;
private static String clickhouseDB;
private static Integer clickhouseSocketTimeout;
@Value("${spring.datasource.clickhouse.address}")
public void setClickhouseAddress(String address) {
ClickHouseConfig.clickhouseAddress = address;
}
@Value("${spring.datasource.clickhouse.username}")
public void setClickhouseUsername(String username) {
ClickHouseConfig.clickhouseUsername = username;
}
@Value("${spring.datasource.clickhouse.password}")
public void setClickhousePassword(String password) {
ClickHouseConfig.clickhousePassword = password;
}
@Value("${spring.datasource.clickhouse.db}")
public void setClickhouseDB(String db) {
ClickHouseConfig.clickhouseDB = db;
}
@Value("${spring.datasource.clickhouse.socketTimeout}")
public void setClickhouseSocketTimeout(Integer socketTimeout) {
ClickHouseConfig.clickhouseSocketTimeout = socketTimeout;
}
public static Connection getConn() {
ClickHouseConnection conn = null;
ClickHouseProperties properties = new ClickHouseProperties();
properties.setUser(clickhouseUsername);
properties.setPassword(clickhousePassword);
properties.setDatabase(clickhouseDB);
properties.setSocketTimeout(clickhouseSocketTimeout);
ClickHouseDataSource clickHouseDataSource = new ClickHouseDataSource(clickhouseAddress,properties);
try {
conn = clickHouseDataSource.getConnection();
return conn;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static List
四、是用测试方法执行sql查询数据
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ClickHouseConfigTest {
@Test
public void exeSql() {
log.info("===========测试开始============");
String sql="select cluster,shard_num from clusters";
List> result= ClickHouseConfig.exeSql(sql);
log.info("===========查询技术============");
log.info("clickhouse查询结果为:{}",result);
}
}
测试成功
该案例基于:Druid连接池和mybatis进行整合。Druid 1.1.10 版本 SQL Parser对clickhouse的开始提供支持。
ru.yandex.clickhouse
clickhouse-jdbc
0.1.53
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: 6000
@Configuration
public 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;
}
}
@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
}
这样整合代码就完成了。
public interface UserInfoMapper {
// 写入数据
void saveData (UserInfo userInfo) ;
// ID 查询
UserInfo selectById (@Param("id") Integer id) ;
// 查询全部
List selectList () ;
}
这里就演示简单的三个接口。
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})
这里 create_day 是以字符串的方式在转换,这里需要注意下。
@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() ;
}
}