JDBC连接Hive

来源:互联网 发布:1hhhh域名升级访问 编辑:程序博客网 时间:2024/05/01 15:27

1 启动Hive

启动Hive命令:

bin/hivebin/hiveserver2

注意:这里是两个服务同时启动
hiveserver2是启动服务,为jdbc的访问提供服务。
如果是使用maven管理jar包,可以引入hive提供的jdbc驱动

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

jdbc的驱动类:org.apache.hive.jdbc.HiveDriver
java代码:

    public static void main(String args[]) {        Connection connection = null;        Statement statement = null;        try {            Class.forName("org.apache.hive.jdbc.HiveDriver");            connection =      DriverManager.getConnection("jdbc:hive2://172.16.61.58:10000/default","","");            statement = connection.createStatement();            statement.execute("create table test_hive (id int, name string, age int, addr string)");        } catch (Exception e) {            e.printStackTrace();        } finally {        }    }

在hive启动的命令行中可以使用命令:show tables;查看所有的表格,如果java程序运行成功即可看到刚刚建成的表格。

2 遇到的问题

2.1 Failed with exception java.io.IOException:java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:user.name}

Hive运行错误,更改hive-site.xml文件中的以下配置:

<property>    <name>hive.exec.local.scratchdir</name>    <value>/Users/kangkang/hadoop/hive-1.2.0/tmp/${system:user.name}</value>    <description>Local scratch space for Hive jobs</description>  </property>

<property>    <name>hive.exec.local.scratchdir</name>    <value>/Users/kangkang/hadoop/hive-1.2.0/tmp/${user.name}</value>    <description>Local scratch space for Hive jobs</description>  </property>

注意我这里的路径配置的是绝对路径,如果有朋友前面也使用了system配置路径,也相应要把system去掉。

参考连接

2.2 2 Could not establish connection to hadoop0:10000/default: Connection refused: connect

错误原因,未启动hiveserver2服务。

执行命令:hive --service hiveserver2

0 0