Hive:用Java代码经过JDBC链接Hiveserver

参考https://www.iteblog.com/archives/846.htmlhtml

一、hive依赖hadoop,将hdfs看成文件存储介质,那是否意味着hive须要知道namenode的地址?java

     实际上在hive的hive-env.sh 中配置了 HADOOP_HOME=/home/install/hadoop-2.5.1node

二、hive的本地模式和远程模式有什么区别?mysql

     hive本质上是将sql语法解析为mapreduce的过程,既然如此它就必须提交mapreduce任务到resoucemanager,那么它如何提交?就是经过hadoop提供的命令hadoop jar命令来提交。linux

     本地模式:简单的理解,hive客户端仅供本地使用,直接使用hive命令,不须要指定IP 端口
web

     远程模式:简单的理解,将hive发布成一个服务进程,经过hive --service hiveserver命令,那么其余hive客户端就能够链接hive的服务进程sql

                其余客户端能够是jdbc方式、hive提供的beeline命令等,既然要链接远端的hive服务进程,那天然须要指定 IP 端口,这里的IP指的是hive服务进程所在的IP,端口天然也是,也天然与hadoop无关。因此不要混淆
数据库

HiveServer2提供了一个新的命令行工具Beeline,它是基于SQLLine CLI的JDBC客户端。apache

Beeline工做模式有两种,即本地嵌入模式和远程模式。嵌入模式状况下,它返回一个嵌入式Hive(相似于Hive CLI)。而远程模式则是经过Thrift协议与某个单独的HiveServer2进程进行链接通讯。浏览器

hive的三种链接方式

一、hive 命令行模式,直接输入/hive/bin/hive的执行程序,或者输入 hive --service cli
用于linux平台命令行查询,查询语句基本跟mysql查询语句相似
二、 hive web界面的 (端口号9999) 启动方式
hive –service hwi &
       用于经过浏览器来访问hive,感受没多大用途
三、 hive 远程服务 (端口号10000) 启动方式
hive --service hiveserver &
或者
hive --service hiveserver 10000>/dev/null 2>/dev/null &
    beeline方式链接:beeline -u jdbc:hive2//localhost:10000/default -n root -p 123456
    或者
    java client方式链接
备注:
链接Hive JDBC URL:jdbc:hive://192.168.6.116:10000/default (Hive默认端口:10000 默认数据库名:default)

第一步:开启hive 远程服务

bin/hive --service hiveserver -p 10002
   Starting Hive Thrift Server
第二步:添加依赖
 <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.7.1</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.7.1</version>
    </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-jdbc</artifactId>
        <version>1.2.1</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore -->
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-metastore</artifactId>
        <version>1.2.1</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore -->
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-exec</artifactId>
        <version>1.2.1</version>
    </dependency>
   
 
第三步:
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
 
public class HiveJdbcTest {
     
    private static String driverName =
                   "org.apache.hadoop.hive.jdbc.HiveDriver";
   
    public static void main(String[] args)
                            throws SQLException {
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.exit(1);
        }
 
        Connection con = DriverManager.getConnection(
                           "jdbc:hive2://localhost:10002/default", "wyp", "");
        Statement stmt = con.createStatement();
        String tableName = "wyphao";
        stmt.execute("drop table if exists " + tableName);
        stmt.execute("create table " + tableName +
                                     " (key int, value string)");
        System.out.println("Create table success!");
        // show tables
        String sql = "show tables '" + tableName + "'";
        System.out.println("Running: " + sql);
        ResultSet res = stmt.executeQuery(sql);
        if (res.next()) {
            System.out.println(res.getString(1));
        }
 
        // describe table
        sql = "describe " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println(res.getString(1) + "\t" + res.getString(2));
        }
 
 
        sql = "select * from " + tableName;
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println(String.valueOf(res.getInt(1)) + "\t"
                                               + res.getString(2));
        }
 
        sql = "select count(1) from " + tableName;
        System.out.println("Running: " + sql);
        res = stmt.executeQuery(sql);
        while (res.next()) {
            System.out.println(res.getString(1));
        }
    }
}
相关文章
相关标签/搜索