# SpringBoot Jdbc连接HIve

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.项目引入相关的依赖


<dependency>
    <groupId>com.zaxxergroupId>
    <artifactId>HikariCPartifactId>
    <version>2.6.1version>
    <exclusions>
        <exclusion>
            <artifactId>slf4j-apiartifactId>
            <groupId>org.slf4jgroupId>
        exclusion>
    exclusions>
dependency>
<dependency>
    <groupId>org.apache.hivegroupId>
    <artifactId>hive-jdbcartifactId>
    <version>2.3.3version>
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如下


<property>
    <name>javax.jdo.option.ConnectionUserNamename>
    <value>rootvalue>
property>

<property>
    <name>javax.jdo.option.ConnectionPasswordname>
    <value>Mysql1234!value>
property>

<property>
    <name>hive.server2.thrift.portname>
    <value>10000value>
property>
<property>
    <name>hive.server2.thrift.bind.hostname>
    <value>192.168.111.100value>
property>

4.测试代码如下

@RequestMapping("/test1")
public List<Record> num(){
    List<Record> records = Db.use(Multidb.hive).find("select * from myhive");
    return records;
}

你可能感兴趣的