springboot集成hive实战

淡淡的烟草味﹌ 2022-12-25 11:56 331阅读 0赞

springboot集成hive实现基本的api调用

maven坐标

  1. <properties>
  2. <hadoop.version>3.3.0</hadoop.version>
  3. </properties>
  4. <!-- hadoop -->
  5. <dependency>
  6. <groupId>org.apache.hadoop</groupId>
  7. <artifactId>hadoop-common</artifactId>
  8. <version>${hadoop.version}</version>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.apache.hadoop</groupId>
  12. <artifactId>hadoop-streaming</artifactId>
  13. <version>${hadoop.version}</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>org.apache.hadoop</groupId>
  17. <artifactId>hadoop-yarn-common</artifactId>
  18. <version>${hadoop.version}</version>
  19. <exclusions>
  20. <exclusion>
  21. <groupId>com.google.guava</groupId>
  22. <artifactId>guava</artifactId>
  23. </exclusion>
  24. </exclusions>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.apache.hadoop</groupId>
  28. <artifactId>hadoop-distcp</artifactId>
  29. <version>${hadoop.version}</version>
  30. <scope>provided</scope>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.apache.hadoop</groupId>
  34. <artifactId>hadoop-mapreduce-client-core</artifactId>
  35. <version>${hadoop.version}</version>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.apache.hadoop</groupId>
  39. <artifactId>hadoop-hdfs</artifactId>
  40. <version>${hadoop.version}</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.apache.hadoop</groupId>
  44. <artifactId>hadoop-mapreduce-client-jobclient</artifactId>
  45. <version>${hadoop.version}</version>
  46. <scope>provided</scope>
  47. </dependency>
  48. <dependency>
  49. <groupId>com.alibaba</groupId>
  50. <artifactId>druid</artifactId>
  51. <version>1.2.1</version>
  52. </dependency>
  53. <!-- hive依赖 -->
  54. <dependency>
  55. <groupId>org.apache.hive</groupId>
  56. <artifactId>hive-jdbc</artifactId>
  57. <version>2.3.0</version>
  58. </dependency>
  59. <!-- 中文分词器 -->
  60. <dependency>
  61. <groupId>cn.bestwu</groupId>
  62. <artifactId>ik-analyzers</artifactId>
  63. <version>5.1.0</version>
  64. </dependency>

配置:

  1. spring:
  2. application:
  3. name: hadoop-demo
  4. datasource:
  5. hive: #hive数据源
  6. url: jdbc:hive2://192.168.150.119:10000/default
  7. type: com.alibaba.druid.pool.DruidDataSource
  8. username: winterchen
  9. password: winterchen
  10. driver-class-name: org.apache.hive.jdbc.HiveDriver
  11. common-config: #连接池统一配置,应用到所有的数据源
  12. initialSize: 1
  13. minIdle: 1
  14. maxIdle: 5
  15. maxActive: 50
  16. maxWait: 10000
  17. timeBetweenEvictionRunsMillis: 10000
  18. minEvictableIdleTimeMillis: 300000
  19. validationQuery: select 'x'
  20. testWhileIdle: true
  21. testOnBorrow: false
  22. testOnReturn: false
  23. poolPreparedStatements: true
  24. maxOpenPreparedStatements: 20
  25. filters: stat
  26. @Data
  27. @NoArgsConstructor
  28. @AllArgsConstructor
  29. @Component
  30. @ConfigurationProperties(prefix = "spring.datasource.hive", ignoreUnknownFields = false)
  31. public class HiveJdbcProperties {
  32. private String url;
  33. private String type;
  34. private String username;
  35. private String password;
  36. private String driverClassName;
  37. }
  38. @Data
  39. @NoArgsConstructor
  40. @AllArgsConstructor
  41. @Component
  42. @ConfigurationProperties(prefix = "spring.datasource.common-config", ignoreUnknownFields = false)
  43. public class DataSourceCommonProperties {
  44. private int initialSize = 10;
  45. private int minIdle;
  46. private int maxIdle;
  47. private int maxActive;
  48. private int maxWait;
  49. private int timeBetweenEvictionRunsMillis;
  50. private int minEvictableIdleTimeMillis;
  51. private String validationQuery;
  52. private boolean testWhileIdle;
  53. private boolean testOnBorrow;
  54. private boolean testOnReturn;
  55. private boolean poolPreparedStatements;
  56. private int maxOpenPreparedStatements;
  57. private String filters;
  58. private String mapperLocations;
  59. private String typeAliasPackage;
  60. }
  61. @Slf4j
  62. @Configuration
  63. @EnableConfigurationProperties({ HiveJdbcProperties.class, DataSourceCommonProperties.class})
  64. public class HiveDruidConfiguration {
  65. @Autowired
  66. private HiveJdbcProperties hiveJdbcProperties;
  67. @Autowired
  68. private DataSourceCommonProperties dataSourceCommonProperties;
  69. @Bean("hiveDruidDataSource") //新建bean实例
  70. @Qualifier("hiveDruidDataSource")//标识
  71. public DataSource dataSource(){
  72. DruidDataSource datasource = new DruidDataSource();
  73. //配置数据源属性
  74. datasource.setUrl(hiveJdbcProperties.getUrl());
  75. datasource.setUsername(hiveJdbcProperties.getUsername());
  76. datasource.setPassword(hiveJdbcProperties.getPassword());
  77. datasource.setDriverClassName(hiveJdbcProperties.getDriverClassName());
  78. //配置统一属性
  79. datasource.setInitialSize(dataSourceCommonProperties.getInitialSize());
  80. datasource.setMinIdle(dataSourceCommonProperties.getMinIdle());
  81. datasource.setMaxActive(dataSourceCommonProperties.getMaxActive());
  82. datasource.setMaxWait(dataSourceCommonProperties.getMaxWait());
  83. datasource.setTimeBetweenEvictionRunsMillis(dataSourceCommonProperties.getTimeBetweenEvictionRunsMillis());
  84. datasource.setMinEvictableIdleTimeMillis(dataSourceCommonProperties.getMinEvictableIdleTimeMillis());
  85. datasource.setValidationQuery(dataSourceCommonProperties.getValidationQuery());
  86. datasource.setTestWhileIdle(dataSourceCommonProperties.isTestWhileIdle());
  87. datasource.setTestOnBorrow(dataSourceCommonProperties.isTestOnBorrow());
  88. datasource.setTestOnReturn(dataSourceCommonProperties.isTestOnReturn());
  89. datasource.setPoolPreparedStatements(dataSourceCommonProperties.isPoolPreparedStatements());
  90. try {
  91. datasource.setFilters(dataSourceCommonProperties.getFilters());
  92. } catch (SQLException e) {
  93. log.error("Druid configuration initialization filter error.", e);
  94. }
  95. return datasource;
  96. }
  97. }

注册jdbcTemplate

  1. @Configuration
  2. public class HiveJdbcConfiguration {
  3. @Bean("hiveJdbcTemplate")
  4. @Qualifier("hiveJdbcTemplate")
  5. public JdbcTemplate jdbcTemplate(@Qualifier("hiveDruidDataSource") DataSource dataSource) {
  6. return new JdbcTemplate(dataSource);
  7. }
  8. }

基本api调用

  1. package com.winterchen.hadoopdemo.service;
  2. import java.util.List;
  3. public interface HiveService {
  4. Object select(String hql);
  5. List<String> listAllTables();
  6. List<String> describeTable(String tableName);
  7. List<String> selectFromTable(String tableName);
  8. }
  9. package com.winterchen.hadoopdemo.service.impl;
  10. import com.winterchen.hadoopdemo.service.HiveService;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.beans.factory.annotation.Qualifier;
  14. import org.springframework.jdbc.core.JdbcTemplate;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.util.StringUtils;
  17. import javax.sql.DataSource;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.sql.Statement;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;
  24. @Slf4j
  25. @Service
  26. public class HiveServiceImpl implements HiveService {
  27. @Autowired
  28. @Qualifier("hiveJdbcTemplate")
  29. private JdbcTemplate hiveJdbcTemplate;
  30. @Autowired
  31. @Qualifier("hiveDruidDataSource")
  32. private DataSource hiveDruidDataSource;
  33. @Override
  34. public Object select(String hql) {
  35. return hiveJdbcTemplate.queryForObject(hql, Object.class);
  36. }
  37. @Override
  38. public List<String> listAllTables() {
  39. List<String> result = new ArrayList<>();
  40. try {
  41. Statement statement = hiveDruidDataSource.getConnection().createStatement();
  42. String sql = "show tables";
  43. log.info("Running: " + sql);
  44. ResultSet resultSet = statement.executeQuery(sql);
  45. while (resultSet.next()) {
  46. result.add(resultSet.getString(1));
  47. }
  48. return result;
  49. } catch (SQLException throwables) {
  50. log.error(throwables.getMessage());
  51. }
  52. return Collections.emptyList();
  53. }
  54. @Override
  55. public List<String> describeTable(String tableName) {
  56. if (StringUtils.isEmpty(tableName)){
  57. return Collections.emptyList();
  58. }
  59. List<String> result = new ArrayList<>();
  60. try {
  61. Statement statement = hiveDruidDataSource.getConnection().createStatement();
  62. String sql = "describe " + tableName;
  63. log.info("Running" + sql);
  64. ResultSet resultSet = statement.executeQuery(sql);
  65. while (resultSet.next()) {
  66. result.add(resultSet.getString(1));
  67. }
  68. return result;
  69. } catch (SQLException throwables) {
  70. log.error(throwables.getMessage());
  71. }
  72. return Collections.emptyList();
  73. }
  74. @Override
  75. public List<String> selectFromTable(String tableName) {
  76. if (StringUtils.isEmpty(tableName)){
  77. return Collections.emptyList();
  78. }
  79. List<String> result = new ArrayList<>();
  80. try {
  81. Statement statement = hiveDruidDataSource.getConnection().createStatement();
  82. String sql = "select * from " + tableName;
  83. log.info("Running" + sql);
  84. ResultSet resultSet = statement.executeQuery(sql);
  85. int columnCount = resultSet.getMetaData().getColumnCount();
  86. String str = null;
  87. while (resultSet.next()) {
  88. str = "";
  89. for (int i = 1; i < columnCount; i++) {
  90. str += resultSet.getString(i) + " ";
  91. }
  92. str += resultSet.getString(columnCount);
  93. log.info(str);
  94. result.add(str);
  95. }
  96. return result;
  97. } catch (SQLException throwables) {
  98. log.error(throwables.getMessage());
  99. }
  100. return Collections.emptyList();
  101. }
  102. }

hive本身就是基于hadoop的MapReduce的一层封装,所以对于hive的操作都是查询和新增等操作,其他的api请参考jdbctemplate接口

源码地址:

WinterChenS/springboot-learning-experience

发表评论

表情:
评论列表 (有 0 条评论,331人围观)

还没有评论,来说两句吧...

相关阅读