Hive JAVA

Posted 代号菜鸟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

Hive数据仓库--Java代码JDBC方式访问Hive中表的数据

java代码在片段活动中不起作用

java 代码片段【JAVA】

Hive – Emerica 滑手在曼彻斯特

# Java 常用代码片段

# Java 常用代码片段