hive的使用方式有幾種

這篇文章主要為大家展示了“hive的使用方式有幾種”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“hive的使用方式有幾種”這篇文章吧。

成都創(chuàng)新互聯(lián)是一家專注于做網(wǎng)站、網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),羅平網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10多年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:羅平等地區(qū)。羅平做網(wǎng)站價(jià)格咨詢:028-86922220

Hive有三種使用方式——CLI命令行,HWI(hie web interface)瀏覽器 以及 Thrift客戶端連接方式。

1、hive  命令行模式

直接輸入/hive/bin/hive的執(zhí)行程序,或者輸入 hive –service cli
       用于linux平臺(tái)命令行查詢,查詢語(yǔ)句基本跟MySQL查詢語(yǔ)句類似

2、hive  web界面的啟動(dòng)方式

hive –service hwi  用于通過瀏覽器來訪問hive

如果lib目錄下沒有hive-hwi-{version}.war包,我們要自己打包

官網(wǎng)下載源碼包(比如1.10版本)

解壓

$ tar zxvf apache-hive-1.1.0.src.tar.gz

再進(jìn)入 hwi 目錄,打包 war 文件(注意命令末尾有一個(gè)點(diǎn).)

#cd apache-hive-1.1.0-src/hwi
#jar cvfM0 hive-hwi-1.1.0.war -C web/ .

 打包完成后,有了我們需要的 war 文件,再?gòu)?fù)制到 $HIVE_HOME/lib 目錄下

#cp hive-hwi-1.1.0.war /usr/local/hive-1.1.0/lib

另外我們還需要拷貝一個(gè) Java 的 tools.jar 到 $HIVE_HOME/lib 目錄下

 cp /usr/local/jdk1.7.0_67/lib/tools.jar /usr/local/hive-1.1.0/lib

否則會(huì)出現(xiàn)類似于下面的錯(cuò)誤(因?yàn)?JAVA_HOME 指到$JAVA_HOME/jre 下了,而其 lib下的 tools.jar 跟$JAVA_HOME/lib/tools.jar 不一樣,編譯的時(shí)候需要用到后者) 

最后,我們將 hive-site.xml 文件修改為

<property>
    <name>hive.hwi.listen.host</name>
    <value>0.0.0.0</value>
    <description>監(jiān)聽的地址</description>
  </property>
  <property>
    <name>hive.hwi.listen.port</name>
    <value>9999</value>
    <description>監(jiān)聽的端口號(hào)</description>
  </property>
  <property>
    <name>hive.hwi.war.file</name>
    <value>lib/hive-hwi-1.1.0.war</value>
    <description>war包所在的地址,注意這里不支持絕對(duì)路徑,坑!</description>
  </property>

啟動(dòng) hwi
在 $HIVE_HOME/bin 目錄下,啟動(dòng) hwi(由于我們之前已經(jīng)修改了 Derby 為 MySQL 數(shù)據(jù)庫(kù),所以在啟動(dòng) hwi 之前,請(qǐng)確保 MySQL 和 Hadoop 已經(jīng)成功啟動(dòng)):

nohup bin/hive --service hwi > /dev/null 2> /dev/null &

web訪問

我們可以在瀏覽器中打開網(wǎng)絡(luò)接口的地址:localhost:9999/hwi, 啟動(dòng)成功

3、jdbc遠(yuǎn)程連接hiveserver2

在之前的學(xué)習(xí)和實(shí)踐Hive中,使用的都是CLI或者h(yuǎn)ive –e的方式,該方式僅允許使用HiveQL執(zhí)行查詢、更新等操作,并且該方式比較笨拙單一。幸好Hive提供了輕客戶端的實(shí)現(xiàn),通過HiveServer或者HiveServer2,客戶端可以在不啟動(dòng)CLI的情況下對(duì)Hive中的數(shù)據(jù)進(jìn)行操作,兩者都允許遠(yuǎn)程客戶端使用多種編程語(yǔ)言如Java、Python向Hive提交請(qǐng)求,取回結(jié)果。HiveServer或者HiveServer2都是基于Thrift的,但HiveSever有時(shí)被稱為Thrift
server,而HiveServer2卻不會(huì)。既然已經(jīng)存在HiveServer為什么還需要HiveServer2呢?這是因?yàn)镠iveServer不能處理多于一個(gè)客戶端的并發(fā)請(qǐng)求,這是由于HiveServer使用的Thrift接口所導(dǎo)致的限制,不能通過修改HiveServer的代碼修正。因此在Hive-0.11.0版本中重寫了HiveServer代碼得到了HiveServer2,進(jìn)而解決了該問題。HiveServer2支持多客戶端的并發(fā)和認(rèn)證,為開放API客戶端如JDBC、ODBC提供了更好的支持。

配置

<property>
  <name>hive.metastore.warehouse.dir</name>
  <value>/usr/hive/warehouse</value>               //(hive中的數(shù)據(jù)庫(kù)和表在HDFS中存放的文件夾的位置)
  <description>location of default database for the warehouse</description>
</property>
<property>
  <name>hive.server2.thrift.port</name>
  <value>10000</value>                               //(HiveServer2遠(yuǎn)程連接的端口,默認(rèn)為10000)
  <description>Port number of HiveServer2 Thrift interface.
  Can be overridden by setting $HIVE_SERVER2_THRIFT_PORT</description>
</property>
 
<property>
  <name>hive.server2.thrift.bind.host</name>
  <value>**.**.**.**</value>                          //(hive所在集群的IP地址)
  <description>Bind host on which to run the HiveServer2 Thrift interface.
  Can be overridden by setting $HIVE_SERVER2_THRIFT_BIND_HOST</description>
</property>
<property>
  <name>hive.server2.long.polling.timeout</name>
  <value>5000</value>                                // (默認(rèn)為5000L,此處修改為5000,不然程序會(huì)報(bào)錯(cuò))
  <description>Time in milliseconds that HiveServer2 will wait, before responding to asynchronous calls that use long polling</description>
</property>
<property>
  <name>javax.jdo.option.ConnectionURL</name>
  <value>jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true</value>  //(Hive的元數(shù)據(jù)庫(kù),我采用的是本地Mysql作為元數(shù)據(jù)庫(kù))
  <description>JDBC connect string for a JDBC metastore</description>
</property>
 
<property>                         
  <name>javax.jdo.option.ConnectionDriverName</name>          //(連接元數(shù)據(jù)的驅(qū)動(dòng)名)
  <value>com.mysql.jdbc.Driver</value>
  <description>Driver class name for a JDBC metastore</description>
</property>
<property>
  <name>javax.jdo.option.ConnectionUserName</name>             //(連接元數(shù)據(jù)庫(kù)用戶名)
  <value>hive</value>
  <description>username to use against metastore database</description>
</property>
 
<property>
  <name>javax.jdo.option.ConnectionPassword</name>             // (連接元數(shù)據(jù)庫(kù)密碼)
  <value>hive</value>
  <description>password to use against metastore database</description>
</property>

先啟動(dòng)元數(shù)據(jù)庫(kù),在命令行中鍵入:hive --service metastore & 

接下來開啟hiveserver2服務(wù):
在命令行中鍵入:hive --service hiveserver2 &
注意查看日志是否報(bào)錯(cuò)。

javaapi操作hive實(shí)例

package com.berg.hive.test1.api;

import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;  

import org.apache.log4j.Logger;  

/** 
 * Hive的JavaApi 
 *  
 * 啟動(dòng)hive的遠(yuǎn)程服務(wù)接口命令行執(zhí)行:hive --service hiveserver & 
 *  
 * @author 湯高 
 *  
 */  
public class HiveJdbcCli {  
    //網(wǎng)上寫 org.apache.hadoop.hive.jdbc.HiveDriver ,新版本不能這樣寫
    private static String driverName = "org.apache.hive.jdbc.HiveDriver";  

  //這里是hive2,網(wǎng)上其他人都寫hive,在高版本中會(huì)報(bào)錯(cuò)
    private static String url = "jdbc:hive2://master:10000/default"; 
    private static String user = "hive";  
    private static String password = "hive";  
    private static String sql = "";  
    private static ResultSet res;  
    private static final Logger log = Logger.getLogger(HiveJdbcCli.class);  

    public static void main(String[] args) {  
        Connection conn = null;  
        Statement stmt = null;  
        try {  
            conn = getConn();  
            stmt = conn.createStatement();  

            // 第一步:存在就先刪除  
            String tableName = dropTable(stmt);  

            // 第二步:不存在就創(chuàng)建  
            createTable(stmt, tableName);  

            // 第三步:查看創(chuàng)建的表  
            showTables(stmt, tableName);  

            // 執(zhí)行describe table操作  
            describeTables(stmt, tableName);  

            // 執(zhí)行l(wèi)oad data into table操作  
            loadData(stmt, tableName);  

            // 執(zhí)行 select * query 操作  
            selectData(stmt, tableName);  

            // 執(zhí)行 regular hive query 統(tǒng)計(jì)操作  
            countData(stmt, tableName);  

        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
            log.error(driverName + " not found!", e);  
            System.exit(1);  
        } catch (SQLException e) {  
            e.printStackTrace();  
            log.error("Connection error!", e);  
            System.exit(1);  
        } finally {  
            try {  
                if (conn != null) {  
                    conn.close();  
                    conn = null;  
                }  
                if (stmt != null) {  
                    stmt.close();  
                    stmt = null;  
                }  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  
    }  

    private static void countData(Statement stmt, String tableName)  
            throws SQLException {  
        sql = "select count(1) from " + tableName;  
        System.out.println("Running:" + sql);  
        res = stmt.executeQuery(sql);  
        System.out.println("執(zhí)行“regular hive query”運(yùn)行結(jié)果:");  
        while (res.next()) {  
            System.out.println("count ------>" + res.getString(1));  
        }  
    }  

    private static void selectData(Statement stmt, String tableName)  
            throws SQLException {  
        sql = "select * from " + tableName;  
        System.out.println("Running:" + sql);  
        res = stmt.executeQuery(sql);  
        System.out.println("執(zhí)行 select * query 運(yùn)行結(jié)果:");  
        while (res.next()) {  
            System.out.println(res.getInt(1) + "\t" + res.getString(2));  
        }  
    }  

    private static void loadData(Statement stmt, String tableName)  
            throws SQLException {  
        //目錄 ,我的是hive安裝的機(jī)子的虛擬機(jī)的home目錄下
        String filepath = "user.txt";  
        sql = "load data local inpath '" + filepath + "' into table "  
                + tableName;  
        System.out.println("Running:" + sql);  
         stmt.execute(sql);  
    }  

    private static void describeTables(Statement stmt, String tableName)  
            throws SQLException {  
        sql = "describe " + tableName;  
        System.out.println("Running:" + sql);  
        res = stmt.executeQuery(sql);  
        System.out.println("執(zhí)行 describe table 運(yùn)行結(jié)果:");  
        while (res.next()) {  
            System.out.println(res.getString(1) + "\t" + res.getString(2));  
        }  
    }  

    private static void showTables(Statement stmt, String tableName)  
            throws SQLException {  
        sql = "show tables '" + tableName + "'";  
        System.out.println("Running:" + sql);  
        res = stmt.executeQuery(sql);  
        System.out.println("執(zhí)行 show tables 運(yùn)行結(jié)果:");  
        if (res.next()) {  
            System.out.println(res.getString(1));  
        }  
    }  

    private static void createTable(Statement stmt, String tableName)  
            throws SQLException {  
        sql = "create table "  
                + tableName  
                + " (key int, value string)  row format delimited fields terminated by '\t'";  
        stmt.execute(sql);  
    }  

    private static String dropTable(Statement stmt) throws SQLException {  
        // 創(chuàng)建的表名  
        String tableName = "testHive";  
        sql = "drop table  " + tableName;  
        stmt.execute(sql);  
        return tableName;  
    }  

    private static Connection getConn() throws ClassNotFoundException,  
            SQLException {  
        Class.forName(driverName);  
        Connection conn = DriverManager.getConnection(url, user, password);  
        return conn;  
    }  

}

以上是“hive的使用方式有幾種”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

新聞標(biāo)題:hive的使用方式有幾種
本文來源:http://muchs.cn/article16/ijcodg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、品牌網(wǎng)站建設(shè)、軟件開發(fā)、云服務(wù)器ChatGPT、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)