我是蜂巢新手。我正在尝试使用此代码enter link description here
这是我的代码
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.hadoop.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
System.out.println("Drive loaded...");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
System.out.println("Before Connecting to hive...");
try {
Connection con = DriverManager.getConnection("jdbc:hive://servername:10001/test","hive","hive");
System.out.println("Connected to hive..");
Statement stmt = con.createStatement();
String tableName = "testHiveDriverTable";
ResultSet res = stmt.executeQuery("create table " + tableName + " (key int, value string)");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.exit(1);
}
}
}
当我运行程序时它只执行驱动程序加载步骤,但是当DriverManager.getConnection()执行时它会永远运行。所以它不会给出任何异常或错误。所以请帮我解决这个问题。
答案 0 :(得分:0)
在命令行启动hiveserver服务,然后尝试, 运行配置单元服务器 hive --service hiveserver&
答案 1 :(得分:0)
最后我得到了答案。 我使用hiveserver2。
这是我的代码。
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class HiveJdbcClient {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
// org.apache.hadoop.hive.jdbc.HiveDriver org.apache.hive.jdbc.HiveDriver
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
System.out.println("Before Connecting to hive...");
try {
Connection con = DriverManager.getConnection("jdbc:hive2://<Server_name>:10001/test","hive_username","hive_password");
System.out.println("Connected to hive..");
// Select query
Statement stmt = con.createStatement();
String sql = "select * from test.emp";
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(String.valueOf(res.getInt(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 tableName = "test.emp";
String filepath = "D:\\datatoload\\emp1.txt";
sql = "load data local inpath '" + filepath + "' into table " + tableName;
System.out.println("Running: " + sql);
boolean result = stmt.execute(sql);
if(!result)
System.out.println("Data load successfully!!!");
else
System.out.println("Failed to load data!!!");
// regular hive query
sql = "select count(*) from " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
System.exit(1);
}
}
}
以下是必需的罐子。