-
在hive上启动service
hive --service hiveserver
-
在eclipse中进行开发
导入需要的jar包(我这个导入的是udf和jdbc连接hive需要的jar包,基本是最简的了)
我的代码,hive的语法就不说了,大家可以修改例子中的sql来进行自己的业务。我的hive没有设置用户名,密码。所以
Connection con = new HiveJDBC().getConnection( "jdbc:hive://192.168.192.138:10000/default", "", "" );
后两个参数我都是设置的空
package com.hive.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 连接hive * @author liqi * */ public class HiveJDBC { public static final String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver"; /** * 获取连接 */ public Connection getConnection(String url,String userName,String password){ try { Class.forName(driverName); Connection conn = DriverManager.getConnection(url, userName, password); return conn; } catch (ClassNotFoundException e) { e.printStackTrace(); }catch (SQLException e) { e.printStackTrace(); } return null; } public static void main(String args[]){ Connection con = new HiveJDBC().getConnection( "jdbc:hive://192.168.192.138:10000/default", "", "" ); try { Statement stmt = con.createStatement(); String sql = "show tables"; ResultSet res = stmt.executeQuery(sql); while(res.next()) { sql = "select * from " + res.getString(1); System.out.println("tables:" + res.getString(1)); ResultSet resTable = stmt.executeQuery(sql); while(resTable.next()){ System.out.println(resTable.getString(2)); } } } catch (SQLException e) { e.printStackTrace(); } } }
本文出自 “屌丝程序员的逆袭” 博客,请务必保留此出处http://cdelliqi.blog.51cto.com/9028667/1435695