码迷,mamicode.com
首页 > 编程语言 > 详细

Hive JAVA

时间:2017-08-06 12:52:02      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:filesyste   code   conf   stack   static   oop   port   before   config   

 1 import org.apache.hadoop.conf.Configuration;
 2 import org.apache.hadoop.fs.FileStatus;
 3 import org.apache.hadoop.fs.FileSystem;
 4 import org.apache.hadoop.fs.Path;
 5 import org.apache.hadoop.ipc.Server;
 6 import org.junit.After;
 7 import org.junit.Before;
 8 import org.junit.Test;
 9 
10 import java.io.IOException;
11 import java.sql.*;
12 
13 /**
14  * Created by Administrator on 2017/8/5.
15  */
16 public class TestHive {
17 
18     private static String driverName = "org.apache.hive.jdbc.HiveDriver";
19     private Connection conn = null;
20     private Statement stmt = null;
21 
22     @Before
23     public void setUp() throws IOException {
24         try {
25             Class.forName(driverName);
26             conn = DriverManager.getConnection("jdbc:hive2://hadoop-senior.jason.com:10000/db_hive", "jason", "abc123");
27             stmt = conn.createStatement();
28         }catch (ClassNotFoundException e) {
29             e.printStackTrace();
30             System.exit(1);
31         }catch (SQLException e){
32             e.printStackTrace();
33             System.exit(1);
34         }
35         System.out.println("Connection is set up");
36     }
37 
38     @Test
39     public void testSelectHive() throws IOException, SQLException {
40         ResultSet resultSet = stmt.executeQuery("select * from bf_log_20170712");
41         while(resultSet.next()){
42             String ip = resultSet.getString(1);
43             String user = resultSet.getString(2);
44             String req_url = resultSet.getString(3);
45             System.out.println("ip: "+ip+" user: "+user+" req_url: "+req_url);
46         }
47     }
48 
49     @Test
50     public void testDescTable() throws SQLException {
51         ResultSet resultSet = stmt.executeQuery("desc bf_log_20170712");
52         while (resultSet.next()){
53             System.out.println(resultSet.getString(1));
54         }
55     }
56 
57     @Test
58     public void testSelectCount() throws SQLException {
59         ResultSet resultSet = stmt.executeQuery("select count(1) from bf_log_20170712");
60         while (resultSet.next()){
61             System.out.println(resultSet.getString(1));
62         }
63     }
64 
65     @After
66     public void closeConn() throws SQLException {
67         stmt.close();
68         conn.close();
69         System.out.println("Connection is closed");
70     }
71     
72 }

 

Hive JAVA

标签:filesyste   code   conf   stack   static   oop   port   before   config   

原文地址:http://www.cnblogs.com/xdlaoliu/p/7293891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!