1. 程式人生 > >hive的java客戶端

hive的java客戶端

1.啟動hive

  • (1)啟動hive
./hive --service hiveserver2 &
  • (2)啟動beeline
#進入beeline命令列
./beeline
#連線hive的hiveserver2,按照提示輸入使用者名稱和密碼
beeline> !connect jdbc:hive2://192.168.1.222:10000
#檢視資料庫
0: jdbc:hive2://192.168.1.222:10000> show databases;

2.java客戶端

  • (1)依賴
<?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>hadoop</groupId>
    <artifactId>1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>

        <!--hadoop-->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-assemblies</artifactId>
            <version>3.1.1</version>
        </dependency>

        <!--hbase-->
        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!--hive-->
        <dependency>
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-jdbc</artifactId>
            <version>2.3.3</version>
        </dependency>

    </dependencies>
    
</project>
  • (2)測試類

由hiveSql提交的查詢語句將會解析成一個或多個mr程式,不包含聚合/連線的簡單select語句會使用一個只有m階段的job,使用group by的聚合查詢語句會使用一個獨立的mr程式,包含大量多表連線的複雜查詢會依靠多個mr查詢的順序執行來實現。

//DDL操作測試類
public class HiveClient {

    private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    private static String url = "jdbc:hive2://192.168.1.222:10000";
    private static String user = "root";
    private static String password = "123456";

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

    //初始化連線
    static {
       try{
           Class.forName(driverName);
           conn = DriverManager.getConnection(url,user,password);
           stmt = conn.createStatement();
       }
       catch (Exception e){
           e.printStackTrace();
       }
    }

    //檢視資料庫
    public void showDatabases() throws Exception{
        String sql = "show databases";
        System.out.println("查詢語句:" + sql);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    }

    //建立資料庫
    public void createDatabase(String dataBaseName) throws Exception{
        String sql = "create database "+dataBaseName+"";
        System.out.println("查詢語句:" + sql);
        stmt.execute(sql);
    }


    //刪除資料庫
    public void deleteDatabase(String dataBaseName) throws Exception{
        String sql = "drop database if exists "+dataBaseName+"";
        System.out.println("查詢語句:" + sql);
        stmt.execute(sql);
    }


    //建立表
    public void createTable(String tableName) throws Exception{
        String sql = "create table "+tableName+"(\n" +
                "id int,\n" +
                "name string,\n" +
                "phone string,\n" +
                "age int\n" +
                ")\n" +
                "row format delimited "+
                "fields terminated by '\\t'";//行記錄用tab分割屬性
        System.out.println("查詢語句:");
        System.out.println(sql);
        //stmt.execute(sql);
    }

    //查看錶結構
    public void showTable(String tableName) throws Exception{
        String sql = "desc "+tableName+"";
        System.out.println("查詢語句:" + sql);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1) + "\t" + rs.getString(2));
        }
    }


    //刪除表
    public void deleteTable(String tableName) throws Exception{
        String sql = "drop table if exists "+tableName+"";
        System.out.println("查詢語句:" + sql);
        stmt.execute(sql);
    }

    //查看錶
    public void showTables() throws Exception{
        String sql = "show tables";
        System.out.println("查詢語句:" + sql);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    }


    //匯入資料(從本地檔案匯入)
    //檔案userData.txt放到/root下,檔案內容如下
    //1	tyf	13388236294	5
    //2	zd	12345434565	6
    //3	sd	12345434532	5
    //4	lsm	12232323232	70
    public void loadData(String filePath,String tableName) throws Exception{
        String sql = "load data local inpath '" + filePath + "' overwrite into table "+tableName+"";
        System.out.println("查詢語句:" + sql);
        stmt.execute(sql);
    }

    //查詢資料
    public void query(String tableName) throws Exception{
        String sql = "select * from "+tableName+"";
        System.out.println("查詢語句:" + sql);
        rs = stmt.executeQuery(sql);
        System.out.println("id" + "\t" + "name" + "\t" + "phone"+"\t" + "age");
        while (rs.next()) {
            System.out.println(rs.getInt("id") + "\t\t" + rs.getString("name") + "\t\t" + rs.getString("phone")+"\t\t" + rs.getInt("age"));
        }
    }


    //清空資料表
    public void truncateTable(String tableName)throws Exception{
        String sql = "truncate table "+tableName+"";
        System.out.println("查詢語句:" + sql);
        stmt.execute(sql);
    }


    //count(*)
    //count(1)
    //count(column)
    public void countTable(String tableName,String word) throws Exception{
        String sql = "select count('"+word+"') from "+tableName+"";
        System.out.println("查詢語句:" + sql);
        rs = stmt.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getInt(1) );
        }

    }

    //多列單次計數
    public void wordCount(String tableName,String column)throws Exception{
        String sql="select word,count(*) as count from (select explode(split("+column+",'\\t')) as word from tb_user) word group by word order by count";
        System.out.println("查詢語句:" + sql);
        rs = stmt.executeQuery(sql);
        System.out.println("word" + "\t" + "count" );
        while (rs.next()) {
            System.out.println(rs.getInt("word") + "\t\t" + rs.getString("word") + "\t\t" + rs.getInt("count"));
        }
    }


    public static void main(String [] args) throws Exception{

        HiveClient client = new HiveClient();
        //檢視資料庫
        //client.showDatabases();
        //建立資料庫
        //client.createDatabase("db_test3");
        //刪除資料庫
        //client.deleteDatabase("db_test3");
        //建立表
        //client.createTable("tb_user");
        //檢視所有表
        //client.showTables();
        //查看錶結構
        //client.showTable("tb_user");
        //刪除表
        //client.deleteTable("tb_user");
        //匯入資料
        //client.loadData("/root/userData.txt","tb_user");
        //查詢資料
        //client.query("tb_user");
        //清空資料表
        //client.truncateTable("tb_user");
        //記錄計數
        //client.countTable("tb_user","*");
        //單詞計數
        client.wordCount("tb_user","name");//統計一列

    }

}

單詞統計,結果如圖:

結果如圖