启动远程客户端 # hive --service hiveserver2
获取连接-〉创建运行环境-〉执行HQL-〉处理结果-〉释放资源

工具类

 package demo.utils;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class JDBCUtils {
private static String driverString = "org.apache.hive.jdbc.HiveDriver";
private static String urlString = "jdbc:hive2://sd-9c1f-2eac:10000/default";
static {
try {
Class.forName(driverString);
} catch (ClassNotFoundException e) {
throw new ExceptionInInitializerError(e);
}
} public static Connection getConnection() {
try {
return DriverManager.getConnection(urlString);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
} public static void release (Connection conn, Statement st, ResultSet rs){
if (rs!=null){
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}finally{
rs=null;
}
}
if (st!=null){
try{
st.close();
}catch(SQLException e){
e.printStackTrace();
}finally{
st=null;
}
}
if (conn!=null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}finally{
conn=null;
}
}
} }

JDBCUtil.java

测试类

 package demo.hive;

 import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import demo.utils.JDBCUtils; public class HiveJDBCDemo {
public static void main(String[] args) {
Connection conn=null;
Statement st=null;
ResultSet rs=null;
String sql="select * from sampledata";
conn=JDBCUtils.getConnection();
try {
st=conn.createStatement();
rs = st.executeQuery(sql);
while(rs.next()){
String sid = rs.getString(1);
String sname = rs.getString(2);
String gender = rs.getString(3);
System.out.println(sid+" "+sname+" "+gender);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
JDBCUtils.release(conn, st, rs);
} } }

HiveJDBCDemo.java

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/PL62716/workspace/HiveDemo/lib/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/PL62716/workspace/HiveDemo/lib/slf4j-log4j12-1.7.22.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
1 Tom M
2 Mary F
3 Jerry M
4 Rose M
5 Mike F

add hive/lib->all jar and hadoop->share->hadoop->common->hadoop-common.jar to project build path.

最新文章

  1. DeprecatedAttribute vs. ObsoleteAttribute
  2. linux命令分享(四):iostat
  3. Web Audio API_基本概念
  4. U3D临时文件GICache巨大
  5. php phpeclipse + xampp 配置安装过程
  6. PS4 的下载速度问题
  7. java懒汉式单例遇到多线程
  8. Spring的配置文件
  9. C#读取Excel五种方式的体会
  10. 运维小东西:每天备份sql到远程服务器上
  11. 限制TextBox输入,只能输入整数
  12. MVC 创建Word文档
  13. xampp——apache服务启动问题(端口占用)
  14. 004_Python高级特性(1):Iterators、Generators和itertools(参考)
  15. 2019.01.22 uoj#14. 【UER #1】DZY Loves Graph(并查集)
  16. 生成学习算法(Generative Learning algorithms)
  17. 12月8日 周五 image_tag.
  18. Elasticsearch 索引的全量/增量更新
  19. UVaLive 2531 The K-League (网络流)
  20. IOS第三方之SVProgressHUD

热门文章

  1. POJ 2151 Check the difficulty of problems:概率dp【至少】
  2. Java_图片处理_02_图片处理工具类库
  3. kettle 设置变量
  4. ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)
  5. Python手记
  6. vmware ubuntu14.04 忘记密码
  7. Python 模拟post请求
  8. 用expressjs写RESTful API
  9. servlet的九大内置对象和四大域对象
  10. js用"."和"[]"获取属性的区别