使用JDBC访问HIVE:
首先启动hive的JDBC服务.
进入hive的bin目录:
这样启动是启动到前台.如果 要想启动到后台需要用到Linux的相关命令.
我们先把其放到前台看下效果,之后再把它放到后台.
查看这个启动的服务是否已经能够监听到了.
这之后就可以编写JDBC代码了.
以word count为例子.
示例代码如下:
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.PreparedStatement; 4 import java.sql.ResultSet; 5 6 public class HiveJdbc { 7 private static String driverName = "org.apache.hive.jdbc.HiveDriver"; 8 private static String url = "jdbc:hive2://192.168.1.99:10002/default"; 9 private static String user = "root"; 10 private static String password = ""; 11 private static String sql = ""; 12 private static ResultSet res; 13 14 public static void main(String[] args) throws Exception { 15 Class.forName(driverName); 16 Connection conn = DriverManager.getConnection(url, user, password); 17 sql = "select word,count(word) as count from (select explode(split(line,' ')) word from t_wordcount) w group by word"; 18 PreparedStatement prepStmt = conn.prepareStatement(sql); 19 res = prepStmt.executeQuery(); 20 while(res.next()){ 21 String word = res.getString(1); 22 int count = res.getInt(2); 23 System.out.println(word + ", " +count); 24 } 25 res.close(); 26 prepStmt.close(); 27 conn.close(); 28 } 29 }
运行...
没有报任何错误.
然后前面设置端口,在前台的运行的标签页中运行MR....
要把这个放在服务任务放在后台执行:
要用到Linux中的nohup