如何在ubuntu中集成java和hive?

时间:2016-01-12 07:07:44

标签: java sql-server eclipse hadoop jdbc

我是hadoop的新手。我正在使用ubuntu并安装了hadoop,hive和pig。我试图在java中创建一个接口,获取配置单元查询并在后台执行它。

我正在使用IDE Eclipse -mars

源代码

    package com.soniya.demo2;


    import java.sql.SQLException;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.sql.DriverManager;


    public class TestJdb
    {
      private static String driver = "org.hadoop.apache.hive.jdbc.HiveDriver";

      /**
       * @param args
       * @throws SQLException
       */
      public static void main(String[] args) throws SQLException {
          try {
          Class.forName(driver);
        } catch (ClassNotFoundException e) {
          e.printStackTrace();
          System.exit(1);
        }

        Connection connect = DriverManager.getConnection("jdbc:hive2://:16074/default","","");
        Statement state = connect.createStatement();
        String tableName = "test";
        state.executeQuery("drop table " + tableName);
        ResultSet res = state.executeQuery("create table " + tableName + " (key int, value string)");

       // Query to show tables
        String show = "show tables";
        System.out.println("Running show");
        res = state.executeQuery(show);
        if (res.next()) {
          System.out.println(res.getString(1));
        }

        // Query to describe table
        String describe = "describe " + tableName;
        System.out.println("Running describe");
        res = state.executeQuery(describe);
        while (res.next()) {
          System.out.println(res.getString(1) + "\t" + res.getString(2));
        }

      }
    }

我收到以下错误

java.lang.ClassNotFoundException: org.hadoop.apache.hive.jdbc.HiveDriver
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at com.soniya.demo2.TestJdb.main(TestJdb.java:21)

帮我解决这个问题。 我已经包含了像hive-jdbc,hadoop-common这样的jar文件,但是它没有用,也试过用hive lib jar文件仍然得到同样的错误。 提前致谢

1 个答案:

答案 0 :(得分:1)

尝试替换driverName" org.apache.hive.jdbc.HiveDriver"而不是" org.apache.hadoop.hive.jdbc.HiveDriver&#34 ;;