使用Beeline我能够成功连接。
!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 :(得分:0)
我已经解决了问题,分享了帮助他人的问题。
我们可以将我的问题分成两部分。
1)我在Java Jdbc中遇到异常,试图在Beeline上运行相同的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
调试:
Telnet您的服务器以检查防火墙是否已打开或阻止端口
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版与hive版匹配
我使用的是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>