Spring boot 集成ClickHouse

版本

  • JDK:1.8
  • Spring Boot:2.1.4.RELEASE
  • ClickHouse:19.6.2.1
  • clickhouse-jdbc:0.1.53

pom.xml


        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            ru.yandex.clickhouse
            clickhouse-jdbc
            0.1.53
        
    

application.properties

#clickhouse JDBC
spring.clickhouse.address=jdbc:clickhouse://192.168.163.139:8123,jdbc:clickhouse://192.168.163.138:8123,jdbc:clickhouse://192.168.163.140:8123
spring.clickhouse.username=default
spring.clickhouse.password=
spring.clickhouse.db=default
spring.clickhouse.socketTimeout=6000

ClickHouseConfig.java

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.*;
/**
 * 
 * @author luchunyang
 * @date 2019-05-26
 *
 */
@Component
public class ClickHouseConfig {
    private static final Logger LOG = LoggerFactory.getLogger(ClickHouseConfig.class);
    private static String clickhouseAddress;
     
 private static String clickhouseUsername;
 
 private static String clickhousePassword;
 
 private static String clickhouseDB;
 
 private static Integer clickhouseSocketTimeout;
 
 @Value("${spring.clickhouse.address}")
 public void setClickhouseAddress(String address) {
    ClickHouseConfig.clickhouseAddress = address;
 }
 @Value("${spring.clickhouse.username}")
 public void setClickhouseUsername(String username) {
    ClickHouseConfig.clickhouseUsername = username;
 }
 @Value("${spring.clickhouse.password}")
 public void setClickhousePassword(String password) {
    ClickHouseConfig.clickhousePassword = password;
 }
 @Value("${spring.clickhouse.db}")
 public void setClickhouseDB(String db) {
    ClickHouseConfig.clickhouseDB = db;
 }
 @Value("${spring.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);
        String[] url=clickhouseAddress.split(",");
        for (int i = 0; i < url.length; i++) {
            ClickHouseDataSource clickHouseDataSource = new ClickHouseDataSource(url[i], properties);
            try {
                conn = clickHouseDataSource.getConnection();
                return conn;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return null;
    }
    public static List exeSql(String sql) {
        Connection connection = getConn();
        try {
            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;
        } catch (SQLException e) {
            LOG.error("ExeSql:{}", sql);
            e.printStackTrace();
        }
        return null;
    }
}

Controller.java

import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Controller {
    @GetMapping(value = "/select")
    public void select() {
        String sql = "select * from dim_movie limit 10";
        List result = ClickHouseConfig.exeSql(sql);
        result.stream().forEach(System.out::println);
    }
}

可配置参数

 // connection settings
 private boolean async;
 private int bufferSize;
 private int apacheBufferSize;
 private int socketTimeout;
 private int connectionTimeout;
 private int dataTransferTimeout;
 private int keepAliveTimeout;
 private int timeToLiveMillis;
 private int defaultMaxPerRoute;
 private int maxTotal;
 private String host;
 private int port;
 private boolean ssl;
 private String sslRootCertificate;
 private String sslMode;
 //additional
 private int maxCompressBufferSize;
 private boolean useServerTimeZone;
 private String useTimeZone;
 private boolean useServerTimeZoneForDates;
 private boolean useObjectsInArrays;
 // queries settings
 private Integer maxParallelReplicas;
 private String totalsMode;
 private String quotaKey;
 private Integer priority;
 private String database;
 private boolean compress;
 private boolean decompress;
 private boolean extremes;
 private Integer maxThreads;
 private Integer maxExecutionTime;
 private Integer maxBlockSize;
 private Integer maxRowsToGroupBy;
 private String profile;
 private String user;
 private String password;
 private String httpAuthorization;
 private boolean distributedAggregationMemoryEfficient;
 private Long maxBytesBeforeExternalGroupBy;
 private Long maxBytesBeforeExternalSort;
 private Long maxMemoryUsage;
 private Long maxMemoryUsageForUser;
 private Long maxMemoryUsageForAllQueries;
 private Long preferredBlockSizeBytes;
 private Long maxQuerySize;
 private boolean sessionCheck;
 private String sessionId;
 private Long sessionTimeout;
 private Long insertQuorum;
 private Long insertQuorumTimeout;
 private Long selectSequentialConsistency;
 private Boolean enableOptimizePredicateExpression;

你可能感兴趣的:(Spring boot 集成ClickHouse)