从零自学Hadoop(18):Hive的CLI和JDBC

ylwp0804 8年前

来自: 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

</div>