1.添加相关的maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>clickhouse</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>clickhouse</name>
<description>clickhouse</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--引入clickhouse依赖-->
<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.3.1-patch</version>
</dependency>
<!-- 引入阿里巴巴连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<!-- <dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version></version>
</dependency>-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<builder>paketobuildpacks/builder-jammy-base:latest</builder>
</image>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.编写相关的配置文件。
server:
port: 8889
#clickhouse连接信息
com:
example:
clickhouse:
driverClass: ru.yandex.clickhouse.ClickhouseDriver
host: jdbc://clickhouse://192.168.1.234:8123/system
username: default
password: admin
3.使用阿里巴巴连接池,进行clickhouse数据库连接。
package com.example.clickhouse.config;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
/**
* @className:@ClickhouseConfig
* @Description:TODO
* @Author:
* @Version:1.0
**/
@Configuration
@Data
public class ClickhouseConfig {
private Logger logger = LoggerFactory.getLogger(ClickhouseConfig.class);
@Value("${com.example.clickhouse.host}")
private String host;
@Value("${com.example.clickhouse.username}")
private String username;
@Value("${com.example.clickhouse.password}")
private String password;
@Value("${com.example.clickhouse.driverClass}")
private String driverClass;
@Bean
public DataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(host);
druidDataSource.setDriverClassName(driverClass);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
druidDataSource.setInitialSize(15);
logger.info("clickhouse数据库连接成功!");
return druidDataSource;
}
}
4.如果运行代码提示下面信息,证明clickhouse数据库连接成功了。
"C:\Program Files\Java\jdk1.8.0_161\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=60617 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:E:\IDEA\IntelliJ IDEA 2018.2.5\lib\idea_rt.jar=60618:E:\IDEA\IntelliJ IDEA 2018.2.5\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_161\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_161\jre\lib\rt.jar;D:\project\clickhouse\target\classes;D:\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot-starter\2.6.4\spring-boot-starter-2.6.4.jar;D:\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot\2.6.4\spring-boot-2.6.4.jar;D:\apache-maven-3.5.4\repository\org\springframework\spring-context\5.3.16\spring-context-5.3.16.jar;D:\apache-maven-3.5.4\repository\org\springframework\spring-aop\5.3.16\spring-aop-5.3.16.jar;D:\apache-maven-3.5.4\repository\org\springframework\spring-beans\5.3.16\spring-beans-5.3.16.jar;D:\apache-maven-3.5.4\repository\org\springframework\spring-expression\5.3.16\spring-expression-5.3.16.jar;D:\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot-autoconfigure\2.6.4\spring-boot-autoconfigure-2.6.4.jar;D:\apache-maven-3.5.4\repository\org\springframework\boot\spring-boot-starter-logging\2.6.4\spring-boot-starter-logging-2.6.4.jar;D:\apache-maven-3.5.4\repository\ch\qos\logback\logback-classic\1.2.10\logback-classic-1.2.10.jar;D:\apache-maven-3.5.4\repository\ch\qos\logback\logback-core\1.2.10\logback-core-1.2.10.jar;D:\apache-maven-3.5.4\repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.1\log4j-to-slf4j-2.17.1.jar;D:\apache-maven-3.5.4\repository\org\apache\logging\log4j\log4j-api\2.17.1\log4j-api-2.17.1.jar;D:\apache-maven-3.5.4\repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\apache-maven-3.5.4\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\apache-maven-3.5.4\repository\org\springframework\spring-core\5.3.16\spring-core-5.3.16.jar;D:\apache-maven-3.5.4\repository\org\springframework\spring-jcl\5.3.16\spring-jcl-5.3.16.jar;D:\apache-maven-3.5.4\repository\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;D:\apache-maven-3.5.4\repository\ru\yandex\clickhouse\clickhouse-jdbc\0.3.1-patch\clickhouse-jdbc-0.3.1-patch.jar;D:\apache-maven-3.5.4\repository\org\apache\httpcomponents\httpclient\4.5.13\httpclient-4.5.13.jar;D:\apache-maven-3.5.4\repository\org\apache\httpcomponents\httpcore\4.4.15\httpcore-4.4.15.jar;D:\apache-maven-3.5.4\repository\commons-codec\commons-codec\1.15\commons-codec-1.15.jar;D:\apache-maven-3.5.4\repository\org\apache\httpcomponents\httpmime\4.5.13\httpmime-4.5.13.jar;D:\apache-maven-3.5.4\repository\org\lz4\lz4-java\1.7.1\lz4-java-1.7.1.jar;D:\apache-maven-3.5.4\repository\com\fasterxml\jackson\core\jackson-core\2.13.1\jackson-core-2.13.1.jar;D:\apache-maven-3.5.4\repository\com\fasterxml\jackson\core\jackson-databind\2.13.1\jackson-databind-2.13.1.jar;D:\apache-maven-3.5.4\repository\com\fasterxml\jackson\core\jackson-annotations\2.13.1\jackson-annotations-2.13.1.jar;D:\apache-maven-3.5.4\repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\apache-maven-3.5.4\repository\com\alibaba\druid\1.2.6\druid-1.2.6.jar;D:\apache-maven-3.5.4\repository\org\projectlombok\lombok\1.18.22\lombok-1.18.22.jar" com.example.clickhouse.ClickhouseApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.6.4)
2023-11-14 19:54:40.369 INFO 29664 --- [ main] c.e.clickhouse.ClickhouseApplication : Starting ClickhouseApplication using Java 1.8.0_161 on DESKTOP-ITTFGNT with PID 29664 (D:\project\clickhouse\target\classes started by admin in D:\project\clickhouse)
2023-11-14 19:54:40.372 INFO 29664 --- [ main] c.e.clickhouse.ClickhouseApplication : The following 1 profile is active: "dev"
2023-11-14 19:54:41.053 INFO 29664 --- [ main] c.e.clickhouse.config.ClickhouseConfig : clickhouse数据库连接成功!
2023-11-14 19:54:41.255 INFO 29664 --- [ main] c.e.clickhouse.ClickhouseApplication : Started ClickhouseApplication in 1.652 seconds (JVM running for 2.992)
2023-11-14 19:54:41.262 INFO 29664 --- [ionShutdownHook] com.alibaba.druid.pool.DruidDataSource : {dataSource-0} closing ...
Process finished with exit code 0