druid整合clickhouse、hive数据源

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;
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高并发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值