更新时间:2024-10-31 GMT+08:00
建立ClickHouse连接
本章节介绍建立ClickHouse连接样例代码。
以下代码片段在“ClickhouseJDBCHaDemo”类的initConnection方法中。在创建连接时传入表1中配置的user和password作为认证凭据,ClickHouse会带着用户名和密码在服务端进行安全认证。
MRS 3.3.0之前版本,示例代片段参考如下:
clickHouseProperties.setPassword(userPass); clickHouseProperties.setUser(userName); BalancedClickhouseDataSource balancedClickhouseDataSource = new BalancedClickhouseDataSource(JDBC_PREFIX + UriList, clickHouseProperties);
MRS 3.3.0及之后版本,示例代片段参考如下:
clickHouseProperties.setProperty(ClickHouseDefaults.USER.getKey(), userName); clickHouseProperties.setProperty(ClickHouseDefaults.PASSWORD.getKey(), userPass); try { clickHouseProperties.setProperty(ClickHouseClientOption.FAILOVER.getKey(), "21"); clickHouseProperties.setProperty(ClickHouseClientOption.LOAD_BALANCING_POLICY.getKey(), "roundRobin"); balancedClickhouseDataSource = new ClickHouseDataSource(JDBC_PREFIX + UriList, clickHouseProperties); } catch (Exception e) { LOG.error("Failed to create balancedClickHouseProperties."); throw e; }
父主题: 开发ClickHouse应用