列式数据库clickhouse的JDBC连接Demo

Posted 整合侠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了列式数据库clickhouse的JDBC连接Demo相关的知识,希望对你有一定的参考价值。

列式数据库clickhouse的JDBC连接Demo

1、JDBC代码:

import ru.yandex.clickhouse.ClickHouseConnection;
import ru.yandex.clickhouse.ClickHouseDataSource;
import ru.yandex.clickhouse.settings.ClickHouseProperties;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * clickhouse连接.
 *
 * @author lizhm
 * @date 2023-02-23 16:19
 */
public class CkUtil 

    @Value("$clickhouse.ip")
    String ip = "127.0.0.1";
    @Value("$clickhouse.port")
    String port = "8123";
    @Value("$clickhouse.dbname")
    String db = "default";
    int socketTimeout = 600000;

    /**
     * conn.
     *
     * @return Connection
     */
    public Connection conn() 
        ClickHouseProperties properties = new ClickHouseProperties();
        properties.setDatabase(db);
        properties.setSocketTimeout(socketTimeout);
        ClickHouseDataSource clickHouseDataSource = new ClickHouseDataSource("jdbc:clickhouse://" + ip + ":" + port,
                properties);
        ClickHouseConnection conn = null;
        try 
            conn = clickHouseDataSource.getConnection();
            return conn;
         catch (SQLException e) 
            e.printStackTrace();
        
        return null;
    

    /**
     * close.
     *
     * @param closes the closes
     */
    public void close(AutoCloseable... closes) 
        for (AutoCloseable close : closes) 
            if (close != null) 
                try 
                    close.close();
                 catch (Exception e) 
                    e.printStackTrace();
                 finally 
                    close = null;
                
            
        
    

2、pom.xml

<properties>
    <clickhouse.version>0.1.52</clickhouse.version>
</properties>
<dependency>
    <groupId>ru.yandex.clickhouse</groupId>
    <artifactId>clickhouse-jdbc</artifactId>
    <version>$clickhouse.version</version>
</dependency>

 

以上是关于列式数据库clickhouse的JDBC连接Demo的主要内容,如果未能解决你的问题,请参考以下文章

clickhouse 列式存储数据库介绍

列式存储?OLAP?ClickHouse究竟是何方神圣

列式存储?OLAP?ClickHouse究竟是何方神圣

列式存储?OLAP?ClickHouse究竟是何方神圣

列式存储?OLAP?ClickHouse究竟是何方神圣

[2]Clickhouse列式存储的明日之星:创建和添加数据