SpringBoot配置Clickhouse的示例代码

网友投稿 1229 2022-10-29

SpringBoot配置Clickhouse的示例代码

SpringBoot配置Clickhouse的示例代码

一、加入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> exeSql(String sql){

log.info("cliockhouse 执行sql:" + sql);

Connection connection = getConn();

Statement statement = connection.createStatement();

ResultSet results = statement.executeQuery(sql);

ResultSetMetaData rsmd = results.getMetaData();

List> list = new ArrayList<>();

while(results.next()){

Map row = new HashMap<>();

for(int i = 1;i<=rsmd.getColumnCount();i++){

row.put(rsmd.getColumnName(i),results.getString(rsmd.getColumnName(i)));

}

list.add(row);

}

return 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";

http:// List> result= ClickHouseConfig.exeSql(sql);

log.info("===========查询技术============");

log.info("clickhouse查询结果为:{}",result);

}

}

测试成功

转自:https://dianjilingqu.com/

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:vanex 更好用的基于mobx的数据流管理框架
下一篇:自定义持久层框架MyORMFramework(三)—框架实现
相关文章

 发表评论

暂时没有评论,来抢沙发吧~