欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

# SpringBoot Jdbc连接HIve

程序员文章站 2022-05-02 11:41:37
...

SpringBoot Jdbc连接HIve


Error: Could not open client transport with JDBC Uri: jdbc:hive2://192.168.159.131:10000:

这个是因为hive中的hiveServer2没有启动起来,启动的命令是:

hive --service hiveserver2
@Bean方式配置Hive数据库

1.项目引入相关的依赖

<!--Hive数据源配置时候的错误-->
<dependency>
    <groupId>com.zaxxer</groupId>
    <artifactId>HikariCP</artifactId>
    <version>2.6.1</version>
    <exclusions>
        <exclusion>
            <artifactId>slf4j-api</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>2.3.3</version>
</dependency>

2.配置Hive数据源

@Configuration
public class ActiveRecordPluginConfig {
    private Logger log = LoggerFactory.getLogger(ActiveRecordPluginConfig.class);
    public static ActiveRecordPlugin arp,arpHive = null;
    public static HikariCpPlugin hikariCpPlugin=null;
    public static Engine engine= null;

    /**
     * 配置Hive数据源
     * */
    @Bean
    public void HiveConfig(){
        try{
            //JfinalProp读取配置文件信息
            Prop prop = PropKit.use("hive.config");
            String url = prop.get("hiveurl");
            String username = prop.get("username");
            String password = prop.get("password");
            String dirverClass=prop.get("DriverClass");
            hikariCpPlugin=new HikariCpPlugin(url,username,password);
            hikariCpPlugin.setDriverClass(dirverClass);
            hikariCpPlugin.start();
            if(arpHive==null) {
                arpHive = new ActiveRecordPlugin(Multidb.hive, hikariCpPlugin);
                arpHive.setShowSql(true);
                arpHive.start();
                log.info("Hive数据源配置完成!");
            }else{
                log.info("Hive数据源已经存在");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("Hive数据源配置失败!");
        }
    }
}

3.Hive的配置信息如下

#bigdatacar 为Hive中的数据库
hiveurl=jdbc:hive2://192.168.111.100:10000/bigdatacar
username=root
password=Mysql1234!
DriverClass=org.apache.hive.jdbc.HiveDriver

Linux修改Hive的用户和密码,通过修改hive安装目录下/conf/hive-site.xml如下

<!--Hive访问的用户名-->
<property>
    <name>javax.jdo.option.ConnectionUserName</name>
    <value>root</value>
</property>
<!--Hive访问的密码-->
<property>
    <name>javax.jdo.option.ConnectionPassword</name>
    <value>Mysql1234!</value>
</property>
<!--hive端口号-->
<property>
    <name>hive.server2.thrift.port</name>
    <value>10000</value>
</property>
<property>
    <name>hive.server2.thrift.bind.host</name>
    <value>192.168.111.100</value>
</property>

4.测试代码如下

@RequestMapping("/test1")
public List<Record> num(){
    List<Record> records = Db.use(Multidb.hive).find("select * from myhive");
    return records;
}
相关标签: 后端开发