Sping Boot Hive

本文详细介绍了如何使用Hive JDBC驱动与Spring Boot框架进行集成,实现从Hive数据库中查询数据的功能。通过配置Maven依赖、创建数据源及JdbcTemplate实例,演示了在Spring Boot应用中执行Hive SQL查询的具体步骤。

前提

装好了 Hive
执行 bin/hiveserver2 开着别动,再开一个终端
lsof -i:10000 看是否打开了端口
bin/beeline -u jdbc:hive2://localhost:10000
就连上了

Maven

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-hadoop</artifactId>
			<version>2.5.0.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.apache.hive</groupId>
			<artifactId>hive-jdbc</artifactId>
			<version>1.2.2</version>
		</dependency>

Config

@Configuration
class Config {

    @Bean
    fun hiveDriver() = HiveDriver()

    @Bean
    fun hiveDataSource(driver: HiveDriver, @Value("\${hive.url}") hiveUrl: String) = SimpleDriverDataSource(driver, hiveUrl)
}

Main

@Component
class Main(val hive: JdbcTemplate) : CommandLineRunner {
    override fun run(vararg args: String?) {
        val b = hive.queryForList("select * from hello3")
        b.forEach { it -> println(it) }
    }
}

Properties

hive.url=jdbc:hive2://localhost:10000
<think>我们参考了用户提供的引用内容,但用户的问题是关于Spring Boot集成Hive JDBC的实现方法。我们之前已经给出了两种验证Hive SQL的方法,但用户现在的问题转向了集成。因此,我们需要调整回答方向,专注于Spring Boot集成Hive JDBC的步骤。 根据引用[1]和引用[2],集成步骤大致如下: 1. 创建Spring Boot工程,添加必要的依赖(hive-jdbc,Druid连接池) 2. 配置Druid连接池和application.yml 3. 编写Controller进行测试 下面我们将详细说明每个步骤。 </think># Spring Boot集成Hive JDBC的实现指南 ## 一、项目依赖配置 ### 1. Maven依赖配置 在`pom.xml`中添加以下依赖: ```xml <dependencies> <!-- Spring Boot Starter --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Hive JDBC驱动 --> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <version>3.1.2</version> <!-- 与您的Hive版本匹配 --> </dependency> <!-- Druid连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.16</version> </dependency> <!-- Spring Data JPA (可选) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> </dependencies> ``` ## 二、配置连接参数 ### 1. application.yml 配置 ```yaml spring: datasource: type: com.alibaba.druid.pool.DruidDataSource druid: url: jdbc:hive2://your-hive-server:10000/default driver-class-name: org.apache.hive.jdbc.HiveDriver username: hive-user password: your-password initial-size: 3 min-idle: 3 max-active: 10 # 需匹配YARN队列配置 max-wait: 60000 test-on-borrow: true test-while-idle: true validation-query: SELECT 1 ``` ### 2. YARN资源配置参考 在YARN的`fair-scheduler.xml`中确保资源配置匹配连接池大小: ```xml <?xml version="1.0" encoding="UTF-8" ?> <allocations> <queue name="hive"> <maxResources>4096 mb,4 vcores</maxResources> <maxRunningApps>2</maxRunningApps> <!-- 与max-active对应 --> <minResources>2048 mb,2 vcores</minResources> </queue> </allocations> ``` ## 三、实现数据访问层 ### 1. 创建Hive数据源配置类 ```java @Configuration public class HiveConfig { @Bean @ConfigurationProperties("spring.datasource.druid") public DataSource hiveDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean public JdbcTemplate hiveJdbcTemplate(@Qualifier("hiveDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } } ``` ### 2. 创建Repository接口 ```java @Repository public class HiveRepository { @Autowired private JdbcTemplate hiveJdbcTemplate; public List<String> executeQuery(String sql) { return hiveJdbcTemplate.query(sql, (rs, rowNum) -> { // 处理结果集 StringBuilder row = new StringBuilder(); ResultSetMetaData metaData = rs.getMetaData(); for(int i=1; i<=metaData.getColumnCount(); i++) { row.append(rs.getString(i)).append(" | "); } return row.toString(); }); } public void executeUpdate(String sql) { hiveJdbcTemplate.execute(sql); } } ``` ## 四、实现Controller层 ```java @RestController @RequestMapping("/api/hive") public class HiveController { @Autowired private HiveRepository hiveRepository; @PostMapping("/query") public ResponseEntity<?> executeQuery(@RequestBody String sql) { try { List<String> results = hiveRepository.executeQuery(sql); return ResponseEntity.ok(results); } catch (Exception e) { return ResponseEntity .status(HttpStatus.INTERNAL_SERVER_ERROR) .body("查询失败: " + e.getMessage()); } } @PostMapping("/update") public ResponseEntity<?> executeUpdate(@RequestBody String sql) { try { hiveRepository.executeUpdate(sql); return ResponseEntity.ok("执行成功"); } catch (Exception e) { return ResponseEntity .status(HttpStatus.INTERNAL_SERVER_ERROR) .body("执行失败: " + e.getMessage()); } } } ``` ## 五、前端页面示例(Thymeleaf) ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Hive查询工具</title> </head> <body> <h1>Hive SQL执行器</h1> <form id="queryForm" th:action="@{/api/hive/query}" method="post"> <textarea name="sql" rows="5" cols="80" placeholder="输入Hive SQL查询语句"></textarea><br> <button type="submit">执行查询</button> </form> <h2>查询结果</h2> <div th:if="${results != null}"> <table border="1"> <tr th:each="row : ${results}"> <td th:text="${row}"></td> </tr> </table> </div> <script> document.getElementById('queryForm').addEventListener('submit', function(e) { e.preventDefault(); fetch(this.action, { method: 'POST', body: new FormData(this), headers: {'Content-Type': 'application/x-www-form-urlencoded'} }) .then(response => response.json()) .then(data => { // 处理结果展示 }); }); </script> </body> </html> ``` ## 六、常见问题解决方案 1. **JAR包冲突问题** - 使用Maven的dependency tree分析冲突 ```bash mvn dependency:tree -Dincludes=org.apache.hadoop ``` - 排除冲突依赖 ```xml <exclusions> <exclusion> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> </exclusion> </exclusions> ``` 2. **Thymeleaf检查报错** 在`application.yml`中禁用严格检查: ```yaml spring: thymeleaf: mode: HTML check-template-location: false ``` 3. **连接池配置优化** ```yaml spring: datasource: druid: validation-query: SELECT 1 test-while-idle: true test-on-borrow: true time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 ``` ## 七、性能优化建议 1. **查询优化** - 使用分区表和分桶表 - 启用向量化查询:`SET hive.vectorized.execution.enabled=true;` - 使用Tez执行引擎:`SET hive.execution.engine=tez;` 2. **连接池管理** - 根据YARN队列资源配置调整`max-active` - 设置合理的`min-idle`防止频繁重建连接 - 启用Druid监控 ```java @Bean public ServletRegistrationBean<StatViewServlet> druidServlet() { return new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); } ``` 集成完成后,您可以通过REST API执行Hive查询,或使用前端页面进行交互式操作[^1][^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值