1.maven依赖
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.3.6</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty.orbit</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>tomcat</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
2.实体类
@Data
@ApiModel(value = "数据源实体类")
public class OfflineDb {
@ApiModelProperty(value = "主机名")
private String host;
@ApiModelProperty(value = "端口")
private String port;
@ApiModelProperty(value = "用户名")
private String username;
@ApiModelProperty(value = "密码")
private String password;
@ApiModelProperty(value = "驱动类,clickhouse:ru.yandex.clickhouse.ClickHouseDriver,hive:org.apache.hive.jdbc.HiveDriver")
private String driver;
@ApiModelProperty(value = "类型,clickhouse、hive2")
private String type;
}
3.Druid
@Slf4j
@Component
public class DbConnectUtil {
public static JdbcTemplate getJdbcTemplate(OfflineDb offlineDb){
try {
//组装数据源
DruidDataSource ds = new DruidDataSource();
ds.setUrl("jdbc:" + offlineDb.getType() + "://" + offlineDb.getHost() + ":" + offlineDb.getPort());
ds.setUsername(offlineDb.getUsername());
ds.setDriverClassName(offlineDb.getDriver());
if (StrUtil.isNotBlank(offlineDb.getPassword())) {
ds.setPassword(offlineDb.getPassword());
}
ds.setTestWhileIdle(false);
ds.setMaxWait(10000); //最大等待时间
ds.setMaxActive(20);
ds.setConnectionErrorRetryAttempts(0);
ds.setBreakAfterAcquireFailure(true);
JdbcTemplate jdbcTemplate = new JdbcTemplate(ds);
return jdbcTemplate;
}catch (Exception e){
log.error("组装数据源异常:{}", e);
throw e;
}
}
}