zoukankan      html  css  js  c++  java
  • Java 下Hive编程

    首先假定你的Hive已经部署完毕。

    导入hive 下所有包

    linux 下启动您的Hive:

    [root@xxx bin]# hive --service hiveserver 50031
    Starting Hive Thrift Server

    Hive 连接
     1 package hadoop.demo.hive;
     2 
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 import java.sql.SQLException;
     6 
     7 public class getConnect {
     8     private static Connection conn = null;
     9     private static Connection connToMysql = null;
    10 
    11     private getConnect() {
    12     }
    13 
    14     // 获得hive连接
    15     public static Connection GetHiveConn() throws SQLException {
    16         if (conn == null) {
    17             try {
    18                 Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
    19             } catch (ClassNotFoundException e) {
    20 
    21                 e.printStackTrace();
    22                 System.exit(1);
    23             }
    24             conn = DriverManager.getConnection(
    25                     "jdbc:hive://Ip:50031/default", "", "");
    26         }
    27         return conn;
    28     }
    29 
    30     // 获得sql连接
    31     public static Connection getMySqlConn() throws SQLException {
    32         if (connToMysql == null) {
    33             try {
    34                 Class.forName("com.mysql.jdbc.Driver");
    35             } catch (ClassNotFoundException e) {
    36 
    37                 e.printStackTrace();
    38                 System.exit(1);
    39             }
    40             connToMysql = DriverManager.getConnection(
    41                     "jdbc:mysql://ip:3306/hive", "junjun", "123456");
    42         }
    43         return connToMysql;
    44     }
    45 
    46     public static void closeHive() throws SQLException {
    47         if (conn != null) {
    48             conn.close();
    49         }
    50     }
    51 
    52     public static void closeMysql() throws SQLException {
    53         if (connToMysql != null) {
    54             connToMysql.close();
    55         }
    56     }
    57 }
    Hive工具类
     1 package hadoop.demo.hive;
     2 
     3 import java.sql.Connection;
     4 import java.sql.ResultSet;
     5 import java.sql.SQLException;
     6 import java.sql.Statement;
     7 
     8 /**
     9  * Hive工具类
    10  * 
    11  * @author xiaoyun.zhao
    12  * 
    13  */
    14 public class HiveUtil {
    15     public static void createTable() {
    16     }
    17 
    18     public static ResultSet queryHive(String hql) throws SQLException {
    19         Connection conn = getConnect.GetHiveConn();
    20         Statement stmt = conn.createStatement();
    21         ResultSet rs = stmt.executeQuery(hql);
    22         return rs;
    23     }
    24 }
    测试函数
     1 package hadoop.demo.hive;
     2 
     3 import java.sql.ResultSet;
     4 
     5 public class HiveMain {
     6 
     7     public static void main(String[] args) throws Exception {
     8 
     9         ResultSet res = HiveUtil.queryHive("select * from pokes");
    10 
    11         while (res.next()) {
    12             System.out.println("Result: key:" + res.getString(1)
    13                     + "  –>  value:" + res.getString(1));
    14         }
    15     }
    16 }
  • 相关阅读:
    linux:centOs7换源阿里云
    nginx:负载均衡实战(一)
    linux-安装jdk以及tomcat
    nginx:在linux上进行nginx的安装
    开源 免费 java CMS
    使用PHP获取汉字的拼音(全部与首字母)
    php版获取重定向后地址的代码分享
    php获取数组中重复数据的两种方法
    php删除html标签的三种解决方法
    php curl 伪造IP来源的代码分享
  • 原文地址:https://www.cnblogs.com/yhql/p/yhql_916.html
Copyright © 2011-2022 走看看