2017-08-28 435 views
0

使用Beeline我能够成功连接。使用Java JDBC访问Hive问题 - 无法从ZooKeeper读取HiveServer2 uri

!connect jdbc:hive2://xxxxxxx/;serviceDiscoveryMode=zookeeper;zookeeperNameSpace=hiveserver2 

当我开始使用Java JDBC代码有相同的URL得到下面的错误。

错误日志:

Exception in thread "main" java.sql.SQLException: org.apache.hive.jdbc.ZooKeeperHiveClientException: Unable to read HiveServer2 uri from ZooKeeper 
        at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:131) 
        at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105) 
        at java.sql.DriverManager.getConnection(DriverManager.java:664) 
        at java.sql.DriverManager.getConnection(DriverManager.java:270) 
        at io.saagie.example.hive.HiveJdbc.main(HiveJdbc.java:23) 
    Caused by: org.apache.hive.jdbc.ZooKeeperHiveClientException: Unable to read HiveServer2 uri from ZooKeeper 
        at org.apache.hive.jdbc.ZooKeeperHiveClientHelper.getNextServerUriFromZooKeeper(ZooKeeperHiveClientHelper.java:86) 
        at org.apache.hive.jdbc.Utils.resolveAuthorityUsingZooKeeper(Utils.java:517) 
        at org.apache.hive.jdbc.Utils.resolveAuthority(Utils.java:489) 
        at org.apache.hive.jdbc.Utils.parseURL(Utils.java:396) 
        at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:129) 
        ... 4 more 
    Caused by: org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /hiveserver2 
        at org.apache.zookeeper.KeeperException.create(KeeperException.java:99) 
        at org.apache.zookeeper.KeeperException.create(KeeperException.java:51) 
        at org.apache.zookeeper.ZooKeeper.getChildren(ZooKeeper.java:1590) 
        at org.apache.curator.framework.imps.GetChildrenBuilderImpl$3.call(GetChildrenBuilderImpl.java:214) 
        at org.apache.curator.framework.imps.GetChildrenBuilderImpl$3.call(GetChildrenBuilderImpl.java:203) 
        at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:107) 
        at org.apache.curator.framework.imps.GetChildrenBuilderImpl.pathInForeground(GetChildrenBuilderImpl.java:199) 
        at org.apache.curator.framework.imps.GetChildrenBuilderImpl.forPath(GetChildrenBuilderImpl.java:191) 
        at org.apache.curator.framework.imps.GetChildrenBuilderImpl.forPath(GetChildrenBuilderImpl.java:38) 
        at org.apache.hive.jdbc.ZooKeeperHiveClientHelper.getNextServerUriFromZooKeeper(ZooKeeperHiveClientHelper.java:69) 
        ... 8 more 

Java代码

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

public class HiveJdbc { // class name hive jdbc 

    private static String driverName = "org.apache.hive.jdbc.HiveDriver"; 

    public static void main(String[] args) throws SQLException { 
     try { 
      Class.forName(driverName); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
      System.exit(1); 
     } 
     // replace "hive” here with the name of the user the queries should run 
     // as 
    Connection con = DriverManager.getConnection("jdbc:hive2://xxxxxxx/;serviceDiscoveryMode=zookeeper;zookeeperNameSpace=hiveserver2"); 
     Statement stmt = con.createStatement(); 
     String Database = "airanalytics"; 
     String tableName = "airlines"; 
     stmt.execute("use " + Database); 
     // stmt.execute("drop table if exists ” + tableName); 
     // stmt.execute("create table ” + tableName + ” (key int, value 
     // string)”); 
     // show tables 
     //String sql = "select origin from airlines where year=1988"; 
     //System.out.println("Running: " + sql); 
     //ResultSet res = stmt.executeQuery(sql); 
     //while (res.next()) { 
     // System.out.println(res.getString("origin")); 
     //} 
    } 
} 

POM

<?xml version="1.0" encoding="UTF-8"?> 
<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/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>io.vk</groupId> 
    <artifactId>java-read-and-write-from-hive</artifactId> 
    <version>1.0-SNAPSHOT</version> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <hadoop.version>2.6.0</hadoop.version> 
     <log4j.version>1.2.17</log4j.version> 
     <hive-jdbc.version>1.2.0</hive-jdbc.version> 
    </properties> 

    <dependencies> 
     <!-- Hadoop main client artifact --> 
     <dependency> 
      <groupId>org.apache.hadoop</groupId> 
      <artifactId>hadoop-client</artifactId> 
      <version>${hadoop.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.hive</groupId> 
      <artifactId>hive-jdbc</artifactId> 
      <version>${hive-jdbc.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>${log4j.version}</version> 
     </dependency> 
     <!-- 
     <dependency> 
      <groupId>org.apache.hive</groupId> 
      <artifactId>hive-service</artifactId> 
      <version>1.2.1000.2.4.2.10-1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.hive</groupId> 
      <artifactId>hive-jdbc</artifactId> 
      <version>1.2.1000.2.4.2.10-1</version> 
     </dependency> 

     --> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>io.saagie</groupId> 
       <artifactId>saagie-maven-plugin</artifactId> 
       <version>1.0.2</version> 
       <configuration> 
        <platformId>1</platformId> 
        <jobName>example-java-read-and-write-from-hive</jobName> 
        <jobCategory>extract</jobCategory> 
       </configuration> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
      <plugin> 
       <artifactId>maven-assembly-plugin</artifactId> 
       <configuration> 
        <archive> 
         <manifest> 
          <mainClass>io.vk.example.hive.Main</mainClass> 
         </manifest> 
        </archive> 
        <descriptorRefs> 
         <descriptorRef>jar-with-dependencies</descriptorRef> 
        </descriptorRefs> 
       </configuration> 
       <executions> 
        <execution> 
         <id>make-assembly</id> <!-- this is used for inheritance merges --> 
         <phase>package</phase> <!-- bind to the packaging phase --> 
         <goals> 
          <goal>single</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 

</project> 
+0

Connection con = DriverManager.getConnection(“ive2:// xxxxxxx /; serviceDiscoveryMode = zookeeper; zookeeperNameSpace = hiveserver2”);这里使用了ive2而不是hive2作为uri字符串/协议的开始 – Rizwan

回答

0

我已经解决了问题,分享ns帮助他人。

我们可以将我的问题分为两部分。

1)我试图连接窗口机器时,在直线上工作相同的URL时,Java Jdbc中出现以下异常。

Exception in thread "main" java.sql.SQLException: org.apache.hive.jdbc.ZooKeeperHiveClientException: Unable to read HiveServer2 uri from ZooKeeper 
        at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:131) 
        at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105) 
        at java.sql.DriverManager.getConnection(DriverManager.java:664) 
        at java.sql.DriverManager.getConnection(DriverManager.java:270) 
        at io.saagie.example.hive.HiveJdbc.main(HiveJdbc.java:23) 
    Caused by: org.apache.hive.jdbc.ZooKeeperHiveClientException: Unable to read HiveServer2 uri from ZooKeeper 
        at org.apache.hive.jdbc.ZooKeeperHiveClientHelper.getNextServerUriFromZooKeeper(ZooKeeperHiveClientHelper.java:86) 
        at org.apache.hive.jdbc.Utils.resolveAuthorityUsingZooKeeper(Utils.java:517) 
        at org.apache.hive.jdbc.Utils.resolveAuthority(Utils.java:489) 
        at org.apache.hive.jdbc.Utils.parseURL(Utils.java:396) 
        at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:129) 
        ... 4 more 
    Caused by: org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /hiveserver2 
        at org.apache.zookeeper.KeeperException.create(KeeperException.java:99) 
        at org.apache.zookeeper.KeeperException.create(KeeperException.java:51) 
        at org.apache.zookeeper.ZooKeeper.getChildren(ZooKeeper.java:1590) 
        at org.apache.curator.framework.imps.GetChildrenBuilderImpl$3.call(GetChildrenBuilderImpl.java:214) 
        at org.apache.curator.framework.imps.GetChildrenBuilderImpl$3.call(GetChildrenBuilderImpl.java:203) 
        at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:107) 
        at org.apache.curator.framework.imps.GetChildrenBuilderImpl.pathInForeground(GetChildrenBuilderImpl.java:199) 
        at org.apache.curator.framework.imps.GetChildrenBuilderImpl.forPath(GetChildrenBuilderImpl.java:191) 
        at org.apache.curator.framework.imps.GetChildrenBuilderImpl.forPath(GetChildrenBuilderImpl.java:38) 
        at org.apache.hive.jdbc.ZooKeeperHiveClientHelper.getNextServerUriFromZooKeeper(ZooKeeperHiveClientHelper.java:69) 
        ... 8 more 

调试:

远程登录服务器来检查防火墙是打开或阻塞端口

Go to cmd and type telnet 

open 11.12.13.14:10000 

在我的情况端口被防火墙阻止,这就是原因,我变得异常以上。

2)现在一旦端口打开,我再次尝试运行我的程序,并开始得到不同的错误。

Exception in thread "main" java.lang.NullPointerException at 
    org.apache.thrift.transport.TSocket.open(TSocket.java:170) at 
    org.apache.thrift.transport.TSaslTransport.open(TSaslTransport.java:266) at 

org.apache.thrift.transport.TSaslClientTransport.open(TSaslClientTransport.java:37) at 
    org.apache.hive.jdbc.HiveConnection.openTransport(HiveConnection.java:204) at 
    org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:176) at 
    org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105) at 
    java.sql.DriverManager.getConnection(DriverManager.java:571) at 
    java.sql.DriverManager.getConnection(DriverManager.java:215) at 
    com.hugedata.hive.HiveJdbcConnect.getConnection(HiveJdbcConnect.java:19) at 
    com.hugedata.hive.HiveTest.main(HiveTest.java:12) 

分析后发现,需要驱动程序jar版本的蜂巢版本匹配

我用的是1.2.0版本

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

与2.0.0更新,它的开始工作。

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