使用直线我能够成功连接。
!connect jdbc:hive2://xxxxxxx/;serviceDiscoveryMode=zookeeper;zookeeperNameSpace=hiveserver2
当我开始使用具有相同url的javajdbc代码时,出现以下错误。
错误日志:
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"));
//}
}
}
聚甲醛
<?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>
1条答案
按热度按时间ifmq2ha21#
我已经解决了这个问题,分享答案来帮助别人。
我们可以把我的问题分成两部分。
1) 我在javajdbc中遇到以下异常:当我在beeline上使用相同的url时,试图连接windowmachine。
调试:
telnet服务器以检查防火墙是否打开或阻止端口
在我的情况下,端口被防火墙阻止,这就是为什么我得到上述例外。
2) 现在,一旦端口打开,我再次尝试运行我的程序,并开始得到不同的错误。
经过分析发现,需要将驱动程序jar版本与hive版本匹配
我用的是1.2.0版本
更新为2.0.0并开始工作。