package com.primeton.damp.bigdata; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; /** * * Hive 无认证,以账号和密码的方式获得连接 * * *
 *
 * Created by zhaopx.
 * User: zhaopx
 * Date: 2020/4/21
 * Time: 18:02
 *
 * 
* * @author zhaopx */ @Slf4j public class SimpleHiveConnectionServiceImpl implements HiveConnectionService { /** * Hive 数据源 */ final Properties params;; public SimpleHiveConnectionServiceImpl(Properties params) { this.params = params; } @Override public Connection getConnection() throws SQLException { String hUrl = "jdbc:hive2://ip:port/default"; if(StringUtils.isNotBlank(params.getProperty("hiveUrl"))) { hUrl = params.getProperty("hiveUrl"); } else { hUrl = hUrl.replace("ip", params.getProperty("hiveIp")); hUrl = hUrl.replace("port", params.getProperty("hivePort")); hUrl = hUrl.replace("default", params.getProperty("hiveDbName")); } log.info("测试连接:{}", hUrl); try { Class.forName("org.apache.hive.jdbc.HiveDriver"); } catch (ClassNotFoundException e) { throw new SQLException("找不到Hive驱动:org.apache.hive.jdbc.HiveDriver.", e); } String hiveDbUser = params.getProperty("hiveDbUser"); String hivePassword = params.getProperty("hivePassword"); log.info("extract hive url: {} use user: {}:******", hUrl, hiveDbUser); return DriverManager.getConnection(hUrl, hiveDbUser, hivePassword); } }