java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot配置Clickhouse

SpringBoot配置Clickhouse的示例代码

作者:元宇宙-Metaverse

这篇文章主要介绍了SpringBoot配置Clickhouse的示例代码,代码简单易懂,对大家的学习或工作具有一定的参考价值,需要的朋友可以参考下

一、加入clickhouse jar包依赖

 <dependency>
        <groupId>ru.yandex.clickhouse</groupId>
        <artifactId>clickhouse-jdbc</artifactId>
        <version>0.1.53</version>
    </dependency>

二、配置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<Map<String,String>> 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<Map<String,String>> list = new ArrayList<>();
            while(results.next()){
                Map<String,String> 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";
            List<Map<String,String>> result= ClickHouseConfig.exeSql(sql);
            log.info("===========查询技术============");
            log.info("clickhouse查询结果为:{}",result);
    }
}

测试成功

转自:https://www.dianjilingqu.com/

到此这篇关于SpringBoot配置Clickhouse的文章就介绍到这了,更多相关SpringBoot配置Clickhouse内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文