共计 2053 个字符,预计需要花费 6 分钟才能阅读完成。
Java 如何通过 jdbc 连接 HIVESERVER,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面丸趣 TV 小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
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
/**
* @param args
* @throws SQLException
*/
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);
}
Connection con = DriverManager.getConnection( jdbc:hive://localhost:10000/default , ,
Statement stmt = con.createStatement();
String tableName = testHiveDriverTable
stmt.executeQuery(drop table + tableName);
ResultSet res = stmt.executeQuery(create table + tableName + (key int, value string)
// show tables
String sql = show tables + tableName +
System.out.println(Running: + sql);
res = stmt.executeQuery(sql);
if (res.next()) { System.out.println(res.getString(1));
}
// describe table
sql = describe + tableName;
System.out.println(Running: + sql);
res = stmt.executeQuery(sql);
while (res.next()) { System.out.println(res.getString(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 filepath = /tmp/a.txt
sql = load data local inpath + filepath + into table + tableName;
System.out.println(Running: + sql);
res = stmt.executeQuery(sql);
// select * query
sql = select * from + tableName;
System.out.println(Running: + sql);
res = stmt.executeQuery(sql);
while (res.next()) { System.out.println(String.valueOf(res.getInt(1)) + \t + res.getString(2));
}
// regular hive query
sql = select count(1) from + tableName;
System.out.println(Running: + sql);
res = stmt.executeQuery(sql);
while (res.next()) { System.out.println(res.getString(1));
}
}
}
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注丸趣 TV 行业资讯频道,感谢您对丸趣 TV 的支持。
正文完