首页 » 大数据 » 正文

[Java]Java通过JDBC连接运行Hive

Hadoop的版本繁多,一旦版本不对Hive的连接和操作可能会报各种各样的错误,这个时候需要用到上一篇博文提到的Maven来管理这些lib。

Hadoop以及Hive的版本查看:

hadoop version
hive --version

pom的配置如下:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.coinidea</groupId>
  <artifactId>hive </artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>hive.web Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <hadoop.version>2.5.0-cdh5.3.2</hadoop.version>
    <hive.version>0.13.1-cdh5.3.2</hive.version>
  </properties>
  <dependencies>
    <!--hadoop和hive-->
    <dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-jdbc</artifactId>
      <version>${hive.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-common</artifactId>
      <version>${hive.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-exec</artifactId>
      <version>${hive.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-metastore</artifactId>
      <version>${hive.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-service</artifactId>
      <version>${hive.version}</version>
    &lt;/dependency&gt;</p>

<pre><code>&amp;lt;dependency&amp;gt;
  &amp;lt;groupId&amp;gt;org.apache.hadoop&amp;lt;/groupId&amp;gt;
  &amp;lt;artifactId&amp;gt;hadoop-common&amp;lt;/artifactId&amp;gt;
  &amp;lt;version&amp;gt;${hadoop.version}&amp;lt;/version&amp;gt;
  &amp;lt;exclusions&amp;gt;
    &amp;lt;exclusion&amp;gt;
      &amp;lt;groupId&amp;gt;org.mortbay.jetty&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;jetty&amp;lt;/artifactId&amp;gt;
    &amp;lt;/exclusion&amp;gt;
    &amp;lt;exclusion&amp;gt;
      &amp;lt;groupId&amp;gt;org.mortbay.jetty&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;jetty-util&amp;lt;/artifactId&amp;gt;
    &amp;lt;/exclusion&amp;gt;
    &amp;lt;exclusion&amp;gt;
      &amp;lt;groupId&amp;gt;org.mortbay.jetty&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;jsp-2.1&amp;lt;/artifactId&amp;gt;
    &amp;lt;/exclusion&amp;gt;
    &amp;lt;exclusion&amp;gt;
      &amp;lt;groupId&amp;gt;org.mortbay.jetty&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;jsp-api-2.1&amp;lt;/artifactId&amp;gt;
    &amp;lt;/exclusion&amp;gt;
    &amp;lt;exclusion&amp;gt;
      &amp;lt;groupId&amp;gt;org.mortbay.jetty&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;servlet-api-2.1&amp;lt;/artifactId&amp;gt;
    &amp;lt;/exclusion&amp;gt;
    &amp;lt;exclusion&amp;gt;
      &amp;lt;groupId&amp;gt;javax.servlet&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;servlet-api&amp;lt;/artifactId&amp;gt;
    &amp;lt;/exclusion&amp;gt;
    &amp;lt;exclusion&amp;gt;
      &amp;lt;groupId&amp;gt;javax.servlet.jsp&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;jsp-api&amp;lt;/artifactId&amp;gt;
    &amp;lt;/exclusion&amp;gt;
    &amp;lt;!--tomcat7中自带jasper.jar--&amp;gt;
    &amp;lt;exclusion&amp;gt;
      &amp;lt;groupId&amp;gt;tomcat&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;jasper-compiler&amp;lt;/artifactId&amp;gt;
    &amp;lt;/exclusion&amp;gt;
    &amp;lt;exclusion&amp;gt;
      &amp;lt;groupId&amp;gt;tomcat&amp;lt;/groupId&amp;gt;
      &amp;lt;artifactId&amp;gt;jasper-runtime&amp;lt;/artifactId&amp;gt;
    &amp;lt;/exclusion&amp;gt;
  &amp;lt;/exclusions&amp;gt;
&amp;lt;/dependency&amp;gt;
&amp;lt;dependency&amp;gt;
  &amp;lt;groupId&amp;gt;cglib&amp;lt;/groupId&amp;gt;
  &amp;lt;artifactId&amp;gt;cglib-nodep&amp;lt;/artifactId&amp;gt;
  &amp;lt;version&amp;gt;2.2.2&amp;lt;/version&amp;gt;
&amp;lt;/dependency&amp;gt;
</code></pre>

<p>&lt;/dependencies&gt;
  &lt;build&gt;
    &lt;finalName&gt;hive &lt;/finalName&gt;
  &lt;/build&gt;
  &lt;repositories&gt;
    &lt;repository&gt;
      &lt;id&gt;cloudera&lt;/id&gt;
      &lt;url&gt;https://repository.cloudera.com/artifactory/cloudera-repos&lt;/url&gt;
      &lt;releases&gt;
        &lt;enabled&gt;true&lt;/enabled&gt;
      &lt;/releases&gt;
      &lt;snapshots&gt;
        &lt;enabled&gt;false&lt;/enabled&gt;
      &lt;/snapshots&gt;
    &lt;/repository&gt;
    &lt;repository&gt;
      &lt;id&gt;people.apache.snapshots&lt;/id&gt;
      &lt;url&gt;http://repository.apache.org/content/groups/snapshots-group/&lt;/url&gt;
      &lt;releases&gt;
        &lt;enabled&gt;false&lt;/enabled&gt;
      &lt;/releases&gt;
      &lt;snapshots&gt;
        &lt;enabled&gt;true&lt;/enabled&gt;
      &lt;/snapshots&gt;
    &lt;/repository&gt;
  &lt;/repositories&gt;
&lt;/project&gt;


新建HiveTest.java

package com.coinidea;</p>

<p>import java.sql.*;
public class HiveTest {
    /* Start server:  hive --service hiveserver2 --hiveconf hive.server2.thrift.port=10000 */
    private static String driverName = &quot;org.apache.hive.jdbc.HiveDriver&quot;;</p>

<pre><code>/**
 * @param args
 * @throws SQLException
 */
public static void main(String[] args) throws SQLException {
    try {
        Class.forName(driverName);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        System.exit(1);
    }
    //replace &amp;quot;hive&amp;quot; here with the name of the user the queries should run as
    Connection con = DriverManager.getConnection(&amp;quot;jdbc:hive2://192.168.*.*:10000/default&amp;quot;, &amp;quot;hive&amp;quot;, &amp;quot;hive&amp;quot;);
    Statement stmt = con.createStatement();
    String table_user = &amp;quot;test.user&amp;quot;;
    String user_sql = &amp;quot;select uri from &amp;quot; + table_user + &amp;quot; limit 10&amp;quot;;
    System.out.println(&amp;quot;Running: &amp;quot; + user_sql);
    ResultSet users = stmt.executeQuery(user_sql);
    while (users.next()) {
        System.out.println(&amp;quot;USER:&amp;quot;+users.getString(1));
    }
    stmt.close();
}
</code></pre>

<p>}

运行HiveTest之前,需要启动HiveServer2

hive --service hiveserver2 --hiveconf hive.server2.thrift.port=10000

发表评论

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax