SimpleHiveConnectionServiceImpl.java 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package com.primeton.damp.bigdata;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.lang.StringUtils;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7. import java.util.Properties;
  8. /**
  9. *
  10. * Hive 无认证,以账号和密码的方式获得连接
  11. *
  12. *
  13. * <pre>
  14. *
  15. * Created by zhaopx.
  16. * User: zhaopx
  17. * Date: 2020/4/21
  18. * Time: 18:02
  19. *
  20. * </pre>
  21. *
  22. * @author zhaopx
  23. */
  24. @Slf4j
  25. public class SimpleHiveConnectionServiceImpl implements HiveConnectionService {
  26. /**
  27. * Hive 数据源
  28. */
  29. final Properties params;;
  30. public SimpleHiveConnectionServiceImpl(Properties params) {
  31. this.params = params;
  32. }
  33. @Override
  34. public Connection getConnection() throws SQLException {
  35. String hUrl = "jdbc:hive2://ip:port/default";
  36. if(StringUtils.isNotBlank(params.getProperty("hiveUrl"))) {
  37. hUrl = params.getProperty("hiveUrl");
  38. } else {
  39. hUrl = hUrl.replace("ip", params.getProperty("hiveIp"));
  40. hUrl = hUrl.replace("port", params.getProperty("hivePort"));
  41. hUrl = hUrl.replace("default", params.getProperty("hiveDbName"));
  42. }
  43. log.info("测试连接:{}", hUrl);
  44. try {
  45. Class.forName("org.apache.hive.jdbc.HiveDriver");
  46. } catch (ClassNotFoundException e) {
  47. throw new SQLException("找不到Hive驱动:org.apache.hive.jdbc.HiveDriver.", e);
  48. }
  49. String hiveDbUser = params.getProperty("hiveDbUser");
  50. String hivePassword = params.getProperty("hivePassword");
  51. log.info("extract hive url: {} use user: {}:******", hUrl, hiveDbUser);
  52. return DriverManager.getConnection(hUrl, hiveDbUser, hivePassword);
  53. }
  54. }