列式数据库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>

 

posted @ 2023-02-27 09:48  整合侠  阅读(491)  评论(0编辑  收藏  举报