Java 通过 JDBC 连接到远程 Mapr Hive
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20395922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting to remote Mapr Hive via JDBC
提问by user152468
This question is similar, but not the same, as Hive JDBC getConnection does not return. Yet this is about a remote connection. Also the metastore is present in the directory in which the hiveserver2 was started.
这个问题很相似,但不一样,因为Hive JDBC getConnection 不返回。然而,这是关于远程连接。元存储也存在于启动 hiveserver2 的目录中。
We have a running mapr cluster on a remote machine. I would like to connect to Hive on this cluster using Java JDBC.
我们在远程机器上有一个正在运行的 mapr 集群。我想使用 Java JDBC 连接到此集群上的 Hive。
Hence we started the hive server:
因此我们启动了 hive 服务器:
/opt/mapr/hive/hive-0.11/bin/hiveserver2
/opt/mapr/hive/hive-0.11/bin/hiveserver2
The output of the server process does not contain any error messages. It listens on port 10000 as reported by netstat.
服务器进程的输出不包含任何错误消息。它侦听 netstat 报告的端口 10000。
I try to connect to the server as described in https://cwiki.apache.org/confluence/display/Hive/HiveClient, thereby replacing localhost by the server name where the hiveserver2 is running:
我尝试按照https://cwiki.apache.org/confluence/display/Hive/HiveClient 中的描述连接到服务器,从而将 localhost 替换为运行 hiveserver2 的服务器名称:
Connection con =
DriverManager.getConnection("jdbc:hive://myserver.example.com:10000/default", "", "");
Yet the program hangs exactly at this statement. It does not seem to get a connection.
然而程序正好挂在这个语句上。它似乎没有获得连接。
Possibly I need to supply a username and password?
可能我需要提供用户名和密码?
Initially I had used the driver org.apache.hadoop.hive.jdbc.HiveDriver.
最初我使用了驱动程序 org.apache.hadoop.hive.jdbc.HiveDriver。
Yet it seems like I should be using the driver org.apache.hive.jdbc.HiveDriver if the hive2 server is running. Now I am getting the following Exception:
然而,如果 hive2 服务器正在运行,我似乎应该使用驱动程序 org.apache.hive.jdbc.HiveDriver。现在我收到以下异常:
Exception in thread "main" java.sql.SQLException: Could not establish connection to jdbc:hive2://myserver.example.com:10000/default: Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null)
at org.apache.hive.jdbc.HiveConnection.openSession(HiveConnection.java:246)
at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:132)
at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:221)
at HiveJdbcClient.main(HiveJdbcClient.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: org.apache.thrift.TApplicationException: Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null)
at org.apache.thrift.TApplicationException.read(TApplicationException.java:108)
at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:71)
at org.apache.hive.service.cli.thrift.TCLIService$Client.recv_OpenSession(TCLIService.java:144)
at org.apache.hive.service.cli.thrift.TCLIService$Client.OpenSession(TCLIService.java:131)
at org.apache.hive.jdbc.HiveConnection.openSession(HiveConnection.java:237)
... 10 more
采纳答案by Philip O.
I had the same problem and was able to get around it by adding the correct dependency to my pom.xml file. I was getting the latest apache release of hive from maven central and switched to using the cdh4 release from the cloudera repo. So, what you are seeing may be a symptom of having the wrong hive-jdbc dependency. Here's the maven snippet I added to my pom file:
我遇到了同样的问题,并且能够通过向我的 pom.xml 文件添加正确的依赖项来解决它。我正在从 maven central 获取最新的 apache 版本的 hive,并转而使用来自 cloudera repo 的 cdh4 版本。因此,您所看到的可能是 hive-jdbc 依赖项错误的症状。这是我添加到我的 pom 文件中的 maven 片段:
<repository>
<id>cloudera</id>
<url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository>
...
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>0.10.0-cdh4.3.2</version>
</dependency>
Here's a link about the cloudera repo.
这是关于cloudera repo的链接。
Also, adding the ";auth=noSasl" to the URL made my application hang so I removed it.
此外,将“;auth=noSasl”添加到 URL 会使我的应用程序挂起,因此我将其删除。
回答by Mukesh S
I think you need to specify the username. Also itshould be hive2not hivesince you are using hiveserver2. Try modifying your connection url:
我认为您需要指定用户名。由于您使用的是hiveserver2,因此它也应该是hive2而不是hive。尝试修改您的连接网址:
Connection con =
DriverManager.getConnection("jdbc:hive2://myserver.example.com:10000/default", "<user>", "");
Its given in the link Hive2
它在链接Hive2 中给出
Hope this helps...!!!
希望这可以帮助...!!!
回答by user3129399
I also had same problem. Please check if server is reachable on the port 10000 from the client (server and port are enabled no firewall is restricting) also check hiveserver is up and running. if yes then it should work. following code work for me for mapr hive.
我也有同样的问题。请检查是否可以从客户端通过端口 10000 访问服务器(服务器和端口已启用,没有防火墙限制),并检查 hiveserver 是否已启动并正在运行。如果是,那么它应该可以工作。以下代码对我适用于 mapr hive。
if you have any query related mapr, please refer answers.mapr.com, this contain most of the information which you might be requiring.
如果您有任何与 mapr 相关的查询,请参阅 answers.mapr.com,其中包含您可能需要的大部分信息。
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
import org.apache.log4j.Logger;
import java.io.*;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.SequenceFile.*;
import org.apache.hadoop.io.SequenceFile.Writer;
import org.apache.hadoop.io.*;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.*;
public class HiveJdbcClient {
//private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
/**
* @param args
* @throws SQLException
**/
private static Logger mLogger = Logger.getLogger(HiveJdbcClient.class);
private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
HiveJdbcClient myJob = new HiveJdbcClient();
myJob.execute();
}
public void execute() throws SQLException {
//mLogger.info("Start HiveJob");
System.out.println("Start HiveJob");
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive://myserver:10000/default", "", "");
Statement stmt = con.createStatement();
String sql = "SHOW TABLES";
//String tableName = "testHiveDriverTable";
// ResultSet res1 = stmt.executeQuery("create table " + tableName + " (key int, value string)");
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1));
}
//mLogger.info("HiveJob executed!");
System.out.println("HiveJob executed!");
}
}
回答by user4033352
You can use the beeline client to connect to hive using JDBC.
您可以使用 beeline 客户端通过 JDBC 连接到 hive。
It would be some thing like: beeline !connect jdbc:hive2://localhost:10000
它会是这样的: beeline !connect jdbc:hive2://localhost:10000
check the link: http://dwbitechguru.blogspot.ca/2014/11/how-to-connect-to-hadoop-hive-using.html
检查链接:http: //dwbitechguru.blogspot.ca/2014/11/how-to-connect-to-hadoop-hive-using.html
回答by user2395959
In my case adding the: ;auth=noSasl
to the JDBC connect string solved the endless waiting for the connection !
在我的情况下,将:添加;auth=noSasl
到 JDBC 连接字符串解决了无休止的等待连接问题!
jdbc:hive2://server:10000/default;auth=noSasl
回答by iwwenbo
You should get hive-service-X.XX.X-cdhX.X.X.jar here :https://repository.cloudera.com/artifactory/cloudera-repos/org/apache/hive/; It works fine to me.
你应该在这里得到 hive-service-X.XX.X-cdhX.XXjar:https://repository.cloudera.com/artifactory/cloudera-repos/org/apache/hive/ ;它对我来说很好用。