1、各存储类型连接-工具类

  1. 1、需引入各个存储类型sdk依赖
  2. 2Demo
  3. @Slf4j
  4. public class JdbcUtil {
  5. private static String PHOENIX_DRIVER = "org.apache.phoenix.queryserver.client.Driver";
  6. //private static final String PHOENIX_DRIVER = "org.apache.phoenix.jdbc.PhoenixDriver";
  7. private static final String HIVE_DRIVER = "org.apache.hive.jdbc.HiveDriver";
  8. private static final String ODPS_DRIVER = "com.aliyun.odps.jdbc.OdpsDriver";
  9. /**
  10. * 获取odps连接
  11. * @param record 数据源信息
  12. * @return
  13. * @throws Exception
  14. */
  15. public static Connection getOdpsConnection(DataSourceConfDO record) throws Exception {
  16. //odps.url=jdbc:odps:http://service.cn.maxcompute.aliyun.com/api?project=city_brain_dev1
  17. ReqParamCheckUtil.objCheck(record, "数据源连接信息为空");
  18. String url = "jdbc:odps:" + record.getEndpoint() + "?project=" + record.getProjectName();
  19. Class.forName(ODPS_DRIVER);
  20. Connection conn = DriverManager.getConnection(url, record.getUserName(), record.getPassword());
  21. log.debug("获取odps连接信息,连接成功");
  22. return conn;
  23. }
  24. /**
  25. * 获取hive连接
  26. *
  27. * @param record 数据源信息
  28. * @return
  29. * @throws Exception
  30. */
  31. public static Connection getHiveConnection(DataSourceConfDO record) throws Exception {
  32. ReqParamCheckUtil.objCheck(record, "数据源连接信息为空");
  33. Class.forName(HIVE_DRIVER);
  34. Connection conn = DriverManager.getConnection(record.getEndpoint(), record.getUserName(), record.getPassword());
  35. log.debug("获取hive连接信息,连接成功");
  36. return conn;
  37. }
  38. /**
  39. * 获取phoenix连接
  40. *
  41. * @param record 数据源信息
  42. * @return
  43. * @throws Exception
  44. */
  45. public static Connection getPhoenixConnection(DataSourceConfDO record) throws Exception {
  46. ReqParamCheckUtil.objCheck(record, "数据源连接信息为空");
  47. Class.forName(PHOENIX_DRIVER);
  48. Connection conn = DriverManager.getConnection(record.getEndpoint(), record.getUserName(), record.getPassword());
  49. log.debug("获取phoenix连接信息,连接成功");
  50. return conn;
  51. }
  52. /**
  53. * 获取hbase连接信息
  54. * @param dataSourceConf
  55. * @return
  56. * @throws IOException
  57. */
  58. public static org.apache.hadoop.hbase.client.Connection getHBaseConnect(DataSourceConfDO dataSourceConf) throws IOException {
  59. ReqParamCheckUtil.objCheck(dataSourceConf, "数据源连接信息为空");
  60. System.setProperty("hadoop.home.dir", "/");
  61. Configuration conf = HBaseConfiguration.create();
  62. conf.set("hbase.zookeeper.quorum", dataSourceConf.getEndpoint());
  63. conf.set("hbase.zookeeper.property.clientPort", dataSourceConf.getPort());
  64. conf.set("log4j.logger.org.apache.hadoop.hbase", "WARN");
  65. org.apache.hadoop.hbase.client.Connection conn = ConnectionFactory.createConnection(conf);
  66. return conn;
  67. }
  68. /**
  69. * 获取ots连接信息
  70. * @param dataSourceConf
  71. * @return
  72. */
  73. public static SyncClient getOtsClient(DataSourceConfDO dataSourceConf) {
  74. ReqParamCheckUtil.objCheck(dataSourceConf, "数据源连接信息为空");
  75. ClientConfiguration clientConfiguration = new ClientConfiguration();
  76. clientConfiguration.setConnectionTimeoutInMillisecond(5000);
  77. clientConfiguration.setSocketTimeoutInMillisecond(5000);
  78. clientConfiguration.setRetryStrategy(new AlwaysRetryStrategy());
  79. SyncClient client = new SyncClient(
  80. dataSourceConf.getEndpoint(),
  81. dataSourceConf.getUserName(),
  82. dataSourceConf.getPassword(),
  83. dataSourceConf.getProjectName(),
  84. clientConfiguration);
  85. return client;
  86. }
  87. }