1. 程式人生 > >大資料入門教程系列之Hive的Java API 操作

大資料入門教程系列之Hive的Java API 操作

Java 想要訪問Hive,需要通過beeline的方式連線Hive,hiveserver2提供了一個新的命令列工具beeline,hiveserver2 對 之前的hive做了升級,功能更加強大,它增加了許可權控制,要使用beeline需要先啟動hiverserver2,再使用beeline連線

操作步驟:

①、修改hadoop的core-site.xml配置檔案

②、啟動hadoop

③、啟動hiverserver2

④、開啟一個新視窗使用beeline連線(注意這裡的javaapidb需要提前建立好)

⑤、新建java專案(maven)

 

 

詳細步驟:

一、修改hadoop的core-site.xml配置檔案

<property>
    <name>hadoop.proxyuser.hadoop.hosts</name>
    <value>*</value>
</property>
<property>
    <name>hadoop.proxyuser.hadoop.groups</name>
    <value>*</value>
</property>

否則使用beeline連線時會報如下錯

hadoop is not allowed to impersonate hadoop (state=08S01,code=0)

原因:hiveserver2增加了許可權控制,需要在hadoop的配置檔案中配置

解決方法:在hadoop的core-site.xml中新增如下內容,然後重啟hadoop,再使用beeline連線即可

參考官網:

https://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-common/Superusers.html

 

二、啟動hadoop

start-sll.sh

 

三、啟動hiverserver2

hiveserver2

jps看到這個程序說明啟動成功了

 

四、開啟一個新視窗使用beeline連線(注意這裡的javaapidb需要提前建立好)

beeline -u jdbc:hive2://node1:10000/javaapidb -n hadoop -p

引數解釋: 

-u:連線url,可以使用IP,也可以使用主機名,埠預設為10000 

-n:連線的使用者名稱(注:不是登入hive的使用者名稱,是hive所在伺服器登入使用者名稱) 

-p:密碼,可以不用輸入

啟動中密碼處可以直接回車,啟動後我們查詢一下資料庫即可看到是否啟動成功

 

五、新建java專案(maven)

1、修改pom


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.demo</groupId>
    <artifactId>java-api-hive</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.9</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2、建立測試類HiveJDBC,程式碼如下 

官網參考:https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients

演示了一個查詢

完整程式碼: 

package hiveapi;

/**
 * Created by zhoujh on 2018/8/15.
 */


import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.sql.*;

/**
 * JDBC 操作 Hive(注:JDBC 訪問 Hive 前需要先啟動HiveServer2)
 */
public class HiveJDBC {

    private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    private static String url = "jdbc:hive2://node1:10000/javaapidb";
    private static String user = "hadoop";
    private static String password = "";

    private static Connection conn = null;
    private static Statement stmt = null;
    private static ResultSet rs = null;

    // 載入驅動、建立連線
    @Before
    public void init() throws Exception {
        Class.forName(driverName);
        conn = DriverManager.getConnection(url, user, password);
        stmt = conn.createStatement();
    }

    // 建立資料庫
    @Test
    public void createDatabase() throws Exception {
        String sql = "create testdb";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    // 查詢所有資料庫
    @Test
    public void showDatabases() throws Exception {
        String sql = "show databases";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    }

    // 建立表
    @Test
    public void createTable() throws Exception {
        String sql = "create table emp(\n" +
                "empno int,\n" +
                "ename string,\n" +
                "job string,\n" +
                "mgr int,\n" +
                "hiredate string,\n" +
                "sal double,\n" +
                "comm double,\n" +
                "deptno int\n" +
                ")\n" +
                "row format delimited fields terminated by '\\t'";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    // 查詢所有表
    @Test
    public void showTables() throws Exception {
        String sql = "show tables";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    }

    // 查看錶結構
    @Test
    public void descTable() throws Exception {
        String sql = "desc emp";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1) + "\t" + rs.getString(2));
        }
    }

    // 載入資料
    @Test
    public void loadData() throws Exception {
        String filePath = "/home/hadoop/data/emp.txt";
        String sql = "load data local inpath '" + filePath + "' overwrite into table emp";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    // 查詢資料
    @Test
    public void selectData() throws Exception {
        String sql = "select * from emp";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        System.out.println("員工編號" + "\t" + "員工姓名" + "\t" + "工作崗位");
        while (rs.next()) {
            System.out.println(rs.getString("empno") + "\t\t" + rs.getString("ename") + "\t\t" + rs.getString("job"));
        }
    }

    // 統計查詢(會執行mapreduce作業)
    @Test
    public void countData() throws Exception {
        String sql = "select count(1) from emp";
        System.out.println("Running: " + sql);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getInt(1));
        }
    }

    // 刪除資料庫
    @Test
    public void dropDatabase() throws Exception {
        String sql = "drop database if exists hive_jdbc_test";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    // 刪除資料庫表
    @Test
    public void deopTable() throws Exception {
        String sql = "drop table if exists emp";
        System.out.println("Running: " + sql);
        stmt.execute(sql);
    }

    // 釋放資源
    @After
    public void destory() throws Exception {
        if (rs != null) {
            rs.close();
        }
        if (stmt != null) {
            stmt.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}