SpringBoot 3 集成Hive 3

前提条件:

运行环境:Hadoop 3.* + Hive 3.* + MySQL 8 ,如果还未安装相关环境,请参考:Hive 一文读懂

Centos7 安装Hadoop3 单机版本(伪分布式版本)

SpringBoot 2 集成Hive 3

pom.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>SpringBootCase</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SpringBoot-Hive3</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>3.1.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-api</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-core</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j</artifactId>
                    <groupId>log4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
                <exclusion>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-runner</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

</project>

配置application.properties

复制代码
server.port=8083
# hive 驱动名称
spring.datasource.driver-class-name=org.apache.hive.jdbc.HiveDriver
# hive 数据库地址 = jdbc:hive2://hive 服务器地址:10000/default(默认数据库名称)
spring.datasource.url=jdbc:hive2://192.168.43.11:10000/default
# hive 服务器用户名
spring.datasource.username=root
# hive  服务器密码
spring.datasource.password=123456

编写Controller和应用入口

我这边编写一个简单的Controller,打印Hive 默认数据库包含数据库名称。

复制代码
package cn.zzg.hive.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hive")
public class HiveController {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/list")
    public List<Map<String, Object>> list() {
        String sql = "show databases";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }

}

package cn.zzg.hive;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

效果截图:

SpringBoot 2 集成Hive 3 遇到的问题

**问题一:**Class path contains multiple SLF4J bindings,日志依赖重复冲突。

造成此问题的原因是:spring boot 默认日志为logback, 而引用的hive-jdbc 及其关联jar 使用的日志为 log4j ,造成SLF4J 绑定冲突。

解决办法:移除冲突的日志:log4j

复制代码
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-api</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-core</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j</artifactId>
                    <groupId>log4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>log4j-slf4j-impl</artifactId>
                    <groupId>org.apache.logging.log4j</groupId>
                </exclusion>

问题二:SpringBoot 自带容器Tomcat 与Hive JDBC 关联Jetty 容器冲突

复制代码
An attempt was made to call the method org.apache.tomcat.util.ExceptionUtils.preload()V but it does not exist. Its class, org.apache.tomcat.util.ExceptionUtils, is available from the following locations:

    jar:file:/E:/maven_repository/org/eclipse/jetty/jetty-runner/9.3.20.v20170531/jetty-runner-9.3.20.v20170531.jar!/org/apache/tomcat/util/ExceptionUtils.class
    jar:file:/E:/maven_repository/org/apache/tomcat/embed/tomcat-embed-core/9.0.12/tomcat-embed-core-9.0.12.jar!/org/apache/tomcat/util/ExceptionUtils.class

解决办法:移除Hive JDBC 依赖的Jetty 容器。

复制代码
              <exclusion>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-runner</artifactId>
                </exclusion>

问题三:通过JDBC 连接Hive 数据库提示:

复制代码
java.net.ConnectException: Connection refused 

造成此类 问题的原因:hiveserver2 服务没有正常启动。

解决办法: 切换至hive 安装目录的bin/ 文件夹下(/usr/local/hive/bin),执行如下命令:

复制代码
# 方式一
hiveserver2 &
# 方式二
hive --service hiveserver2

问题四:访问Hive 数据库,提示无权限问题:

造成此类问题的原因:hadoop 没有配置权限导致。

解决办法:切换至hadoop 安装目录的/etc文件夹下(/usr/local/hadoop/etc/hadoop/core-site.xml),添加如下配置:

复制代码
         <property>
                <name>hadoop.proxyuser.root.hosts</name>
                <value>*</value>
        </property>
        <property>
                <name>hadoop.proxyuser.root.groups</name>
                <value>*</value>
        </property>
相关推荐
神奇侠202413 小时前
Hive SQL常见操作
hive·hadoop·sql
SelectDB技术团队16 小时前
从 ClickHouse、Druid、Kylin 到 Doris:网易云音乐 PB 级实时分析平台降本增效
大数据·数据仓库·clickhouse·kylin·实时分析
itachi-uchiha19 小时前
Docker部署Hive大数据组件
大数据·hive·docker
viperrrrrrrrrr720 小时前
大数据学习(131)-Hive数据分析函数总结
大数据·hive·学习
Leo.yuan1 天前
API是什么意思?如何实现开放API?
大数据·运维·数据仓库·人工智能·信息可视化
qq_408413391 天前
spark 执行 hive sql数据丢失
hive·sql·spark
TDengine (老段)1 天前
TDengine 替换 Hadoop,彻底解决数据丢失问题 !
大数据·数据库·hadoop·物联网·时序数据库·tdengine·涛思数据
workflower1 天前
量子比特实现方式
数据仓库·服务发现·需求分析·量子计算·软件需求
yt948322 天前
如何在IDE中通过Spark操作Hive
ide·hive·spark
青春之我_XP2 天前
【基于阿里云搭建数据仓库(离线)】Data Studio创建资源与函数
大数据·数据仓库·sql·dataworks·maxcompute·data studio