| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package com.primeton.dsp.datarelease.data.bdata;
- import com.primeton.dsp.datarelease.server.model.DspHiveResource;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang.StringUtils;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- /**
- *
- * Hive 无认证,以账号和密码的方式获得连接
- *
- *
- * <pre>
- *
- * Created by zhaopx.
- * User: zhaopx
- * Date: 2020/4/21
- * Time: 18:02
- *
- * </pre>
- *
- * @author zhaopx
- */
- @Slf4j
- public class SimpleHiveConnectionServiceImpl implements HiveConnectionService {
- /**
- * Hive 数据源
- */
- final DspHiveResource hiveResource;
- public SimpleHiveConnectionServiceImpl(DspHiveResource hiveResource) {
- this.hiveResource = hiveResource;
- }
- @Override
- public Connection getConnection() throws SQLException {
- String hUrl = "jdbc:hive2://ip:port/default";
- if(StringUtils.isNotBlank(hiveResource.getHiveUrl())) {
- hUrl = hiveResource.getHiveUrl();
- } else {
- hUrl = hUrl.replace("ip", hiveResource.getHiveIp());
- hUrl = hUrl.replace("port", hiveResource.getHivePort()+"");
- hUrl = hUrl.replace("default", hiveResource.getHiveDbName());
- }
- log.info("测试连接:{}", hUrl);
- try {
- Class.forName("org.apache.hive.jdbc.HiveDriver");
- } catch (ClassNotFoundException e) {
- throw new SQLException("找不到Hive驱动:org.apache.hive.jdbc.HiveDriver.", e);
- }
- return DriverManager.getConnection(hUrl, hiveResource.getHiveDbUser() , hiveResource.getHivePassword());
- }
- }
|