首页 > 代码库 > Hive JAVA

Hive JAVA

 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