HiveServer2 JDBC Clinet:无法连接到MetaStore

时间:2014-03-08 04:55:08

标签: java jdbc hive

我无法运行Hive JDBC客户端代码。 我找不到类引起的错误:javax.jdo.JDOFatalUserException:找不到类org.datanucleus.api.jdo.JDOPersistenceManagerFactory。

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

public class HiveJdbcClient {
  private static String driverName = "org.apache.hive.jdbc.HiveDriver";

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

    System.out.println("Getting Connection ");
    Connection con; 

    try {
      con = DriverManager.getConnection("jdbc:hive2:////localhost:10000/default", "hive", "");

      Statement stmt = con.createStatement();
      String tableName = "testHiveDriverTable";
      stmt.executeQuery("drop table " + tableName);
      ResultSet res = stmt.executeQuery("create table " + tableName+ " (key int, value string)");
      // show tables
      String sql = "show tables '" + tableName + "'";
      System.out.println("Running: " + sql);
      res = stmt.executeQuery(sql);
      if (res.next()) {
        System.out.println(res.getString(1));
      }
      // describe table
      sql = "describe " + tableName;
      System.out.println("Running: " + sql);
      res = stmt.executeQuery(sql);
      while (res.next()) {
        System.out.println(res.getString(1) + "\t" + res.getString(2));
      }

      // load data into table
      // NOTE: filepath has to be local to the hive server
      // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
      String filepath = "/home/hadoop/unnik/sample_text.csv";
      sql = "load data local inpath '" + filepath + "' into table "
          + tableName;
      System.out.println("Running: " + sql);
      res = stmt.executeQuery(sql);

      // select * query
      sql = "select * from " + tableName;
      System.out.println("Running: " + sql);
      res = stmt.executeQuery(sql);
      while (res.next()) {
        System.out.println(String.valueOf(res.getInt(1)) + "\t"
            + res.getString(2));
      }

      // regular hive query
      sql = "select count(1) from " + tableName;
      System.out.println("Running: " + sql);
      res = stmt.executeQuery(sql);
      while (res.next()) {
        System.out.println(res.getString(1));
      }
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

获得连接

14/03/07 20:47:03 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
14/03/07 20:47:03 INFO service.AbstractService: Service:OperationManager is inited.
14/03/07 20:47:03 INFO service.AbstractService: Service:SessionManager is inited.
14/03/07 20:47:03 INFO service.AbstractService: Service:CLIService is inited.
14/03/07 20:47:03 INFO service.AbstractService: Service:OperationManager is started.
14/03/07 20:47:03 INFO service.AbstractService: Service:SessionManager is started.
14/03/07 20:47:03 INFO service.AbstractService: Service:CLIService is started.
14/03/07 20:47:05 INFO metastore.HiveMetaStore: 0: Opening raw store with implemenation class:org.apache.hadoop.hive.metastore.ObjectStore
14/03/07 20:47:06 INFO metastore.ObjectStore: ObjectStore, initialize called
Exception in thread "main" org.apache.hive.service.ServiceException: Unable to connect to MetaStore!
  at org.apache.hive.service.cli.CLIService.start(CLIService.java:86)
  at org.apache.hive.service.cli.thrift.EmbeddedThriftCLIService.<init>(EmbeddedThriftCLIService.java:35)
  at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:86)
  at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:104)
  at java.sql.DriverManager.getConnection(DriverManager.java:582)
  at java.sql.DriverManager.getConnection(DriverManager.java:185)
  at HiveJdbcClient.main(HiveJdbcClient.java:25)
Caused by: javax.jdo.JDOFatalUserException: Class org.datanucleus.api.jdo.JDOPersistenceManagerFactory was not found.
NestedThrowables:
java.lang.ClassNotFoundException: org.datanucleus.api.jdo.JDOPersistenceManagerFactory
  at javax.jdo.JDOHelper.invokeGetPersistenceManagerFactoryOnImplementation(JDOHelper.java:1168)
  at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:803)
  at javax.jdo.JDOHelper.getPersistenceManagerFactory(JDOHelper.java:698)
  at org.apache.hadoop.hive.metastore.ObjectStore.getPMF(ObjectStore.java:262)
  at org.apache.hadoop.hive.metastore.ObjectStore.getPersistenceManager(ObjectStore.java:291)
  at org.apache.hadoop.hive.metastore.ObjectStore.initialize(ObjectStore.java:224)
  at org.apache.hadoop.hive.metastore.ObjectStore.setConf(ObjectStore.java:199)
  at org.apache.hadoop.util.ReflectionUtils.setConf(ReflectionUtils.java:70)
  at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:130)
  at org.apache.hadoop.hive.metastore.RetryingRawStore.<init>(RetryingRawStore.java:62)
  at org.apache.hadoop.hive.metastore.RetryingRawStore.getProxy(RetryingRawStore.java:71)
  at org.apache.hadoop.hive.metastore.HiveMetaStore$HMSHandler.newRawStore(HiveMetaStore.java:404)
  at org.apache.hadoop.hive.metastore.HiveMetaStore$HMSHandler.getMS(HiveMetaStore.java:392)
  at org.apache.hadoop.hive.metastore.HiveMetaStore$HMSHandler.createDefaultDB(HiveMetaStore.java:430)
  at org.apache.hadoop.hive.metastore.HiveMetaStore$HMSHandler.init(HiveMetaStore.java:324)
  at org.apache.hadoop.hive.metastore.HiveMetaStore$HMSHandler.<init>(HiveMetaStore.java:285)
  at org.apache.hadoop.hive.metastore.RetryingHMSHandler.<init>(RetryingHMSHandler.java:56)
  at org.apache.hadoop.hive.metastore.RetryingHMSHandler.getProxy(RetryingHMSHandler.java:61)
  at org.apache.hadoop.hive.metastore.HiveMetaStore.newHMSHandler(HiveMetaStore.java:4010)
  at org.apache.hadoop.hive.metastore.HiveMetaStoreClient.<init>(HiveMetaStoreClient.java:120)
  at org.apache.hadoop.hive.metastore.HiveMetaStoreClient.<init>(HiveMetaStoreClient.java:103)
  at org.apache.hive.service.cli.CLIService.start(CLIService.java:83)
  ... 6 more

1 个答案:

答案 0 :(得分:1)

因为你提供了错误的URL(两个额外的斜杠):

con = DriverManager.getConnection("jdbc:hive2:////localhost:10000/default", "hive", "");

应该是:

con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "hive", "");

另外,仅供参考:

1。)在端口10000

启动hiveserver2

2.确保您提供的用户名有权在HDFS中创建目录。

希望这有助于...... !!!