從零自學Hadoop(18):Hive的CLI和JDBC

ylwp0804 8年前發布 | 23K 次閱讀 Hive JDBC Hadoop 分布式/云計算/大數據

來自: http://www.cnblogs.com/mephisto/p/5179786.html

閱讀目錄

  • Hive CLI(old CLI)
  • Beeline CLI(new CLI)
  • JDBC
  • Demo下載
  • 系列索引

本文版權歸mephisto和博客園共有,歡迎轉載,但須保留此段聲明,并給出原文鏈接,謝謝合作。

文章是哥(mephisto)寫的,SourceLink

上一篇,我們對hive的數據導出,以及集群Hive數據的遷移進行描述。了解到了基本的hive導出操作。這里,我們將對hive的CLI及JDBC這些實用性很強的兩個方便進行簡要的介紹。

下面我們開始介紹hive的CLI和JDBC。

Hive CLI(old CLI)

一:說明

在0.11之前只有Hive CLI,他需要安裝Hive Client才能使用。是一個重量級的命令行工具。連接的服務器是HiveServer1。

二:語法:

usage: hive
 -d,--define <key=value>          Variable subsitution to apply to hive
                                  commands. e.g. -d A=B or --define A=B
 -e <quoted-query-string>         SQL from command line
 -f <filename>                    SQL from files
 -H,--help                        Print help information
 -h <hostname>                    Connecting to Hive Server on remote host
    --hiveconf <property=value>   Use value for given property
    --hivevar <key=value>         Variable subsitution to apply to hive
                                  commands. e.g. --hivevar A=B
 -i <filename>                    Initialization SQL file
 -p <port>                        Connecting to Hive Server on port number
 -S,--silent                      Silent mode in interactive shell
 -v,--verbose                     Verbose mode (echo executed SQL to the
                                  console)

三:官網例子1

$HIVE_HOME/bin/hive -e 'select a.col from tab1 a'

四:官網例子2

運行腳本文件

$HIVE_HOME/bin/hive -f /home/my/hive-script.sql

Beeline CLI(new CLI)

一:介紹

為了使得CLI輕量化,后來Hive做出了Beeline和HiveServer2。Beeline是一個基于JDBC的SQLLine CLI。

二:官網例子

bin/beeline
!connect jdbc:hive2://localhost:10000 scott tiger org.apache.hive.jdbc.HiveDriver

三:實戰

hiveserver2的默認端口是10000。

beeline -u jdbc:hive2://h188:10000

查看有哪些表。

show tables;

這里可以看到我們在上面幾篇測試的時候的表score,score1,score2,我們查下score的數據。

select * from score;

一:介紹

我們可以在代碼中通過jdbc連接hive。這樣在實際運用場景中就很方便,很原有的非分布式計算的開發方式基本類似,具有很強的適用性。

二:新建工程

新建工程com.per.hive

三:引入包

版本根據自己使用hadoop集群而定

commons-logging-1.2.jar
hadoop-common-2.6.0.jar
hive-exec-0.13.1.jar
hive-jdbc-0.13.1.jar
hive-service-0.13.1.jar
httpclient-4.3.4.jar
httpcore-4.3.2.jar
log4j-1.2.16.jar
slf4j-api-1.7.6.jar
slf4j-log4j12-1.7.6.jar

四:添加類HiveDemo

添加HiveDriver

    static {
        try {
            Class.forName("org.apache.hive.jdbc.HiveDriver");
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

添加test()方法

   /**
     * @Description : 測試
     */
    private static void test() {
        try (Connection con = DriverManager
                .getConnection("jdbc:hive2://h188:10000/")) {
            Statement stm = con.createStatement();
            ResultSet rs = stm.executeQuery("select * from score ");

            while (rs.next()) {
                String info = rs.getString(1);
                info += " " + rs.getString(2);
                info += " " + rs.getString(3);
                info += " " + rs.getString(4);

                System.out.println(info);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

調用

    public static void main(String[] args) {
        test();
    }

五:運行

運行,查看結果,可以看見,程序運行的結果與我們在Beeline命令行查看的結果一致。

這樣我們的Hive的CLI和JDBC告一段落。

--------------------------------------------------------------------

到此,本章節的內容講述完畢。

Demo下載

系列索引

本文版權歸mephisto和博客園共有,歡迎轉載,但須保留此段聲明,并給出原文鏈接,謝謝合作。

文章是哥(mephisto)寫的,SourceLink

 本文由用戶 ylwp0804 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!