加载中...

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


阅读目录

  • 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。

二:语法:

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

三:官网例子1 

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

四:官网例子2

  运行脚本文件

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

Beeline CLI(new CLI)

一:介绍

  为了使得CLI轻量化,后来Hive做出了Beeline和HiveServer2。Beeline是一个基于JDBC的SQLLine CLI。

二:官网例子

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

三:实战

  hiveserver2的默认端口是10000。

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

   查看有哪些表。

  1. show tables;

 

  这里可以看到我们在上面几篇测试的时候的表score,score1,score2,我们查下score的数据。

  1. select * from score;

JDBC

一:介绍

  我们可以在代码中通过jdbc连接hive。这样在实际运用场景中就很方便,很原有的非分布式计算的开发方式基本类似,具有很强的适用性。

二:新建工程

  新建工程com.per.hive

三:引入包

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

  1. commons-logging-1.2.jar
  2. hadoop-common-2.6.0.jar
  3. hive-exec-0.13.1.jar
  4. hive-jdbc-0.13.1.jar
  5. hive-service-0.13.1.jar
  6. httpclient-4.3.4.jar
  7. httpcore-4.3.2.jar
  8. log4j-1.2.16.jar
  9. slf4j-api-1.7.6.jar
  10. slf4j-log4j12-1.7.6.jar

四:添加类HiveDemo

  添加HiveDriver

  1. static {
  2. try {
  3. Class.forName("org.apache.hive.jdbc.HiveDriver");
  4. } catch (ClassNotFoundException ex) {
  5. ex.printStackTrace();
  6. }
  7. }

  添加test()方法

  1. /**
  2. * @Description : 测试
  3. */
  4. private static void test() {
  5. try (Connection con = DriverManager
  6. .getConnection("jdbc:hive2://h188:10000/")) {
  7. Statement stm = con.createStatement();
  8. ResultSet rs = stm.executeQuery("select * from score ");
  9. while (rs.next()) {
  10. String info = rs.getString(1);
  11. info += " " + rs.getString(2);
  12. info += " " + rs.getString(3);
  13. info += " " + rs.getString(4);
  14. System.out.println(info);
  15. }
  16. } catch (Exception ex) {
  17. ex.printStackTrace();
  18. }
  19. }

  调用

  1. public static void main(String[] args) {
  2. test();
  3. }

五:运行

  运行,查看结果,可以看见,程序运行的结果与我们在Beeline命令行查看的结果一致。

  

 这样我们的Hive的CLI和JDBC告一段落。

 

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

  到此,本章节的内容讲述完毕。

Demo下载

https://github.com/sinodzh/HadoopExample/tree/master/2016/com.per.hive

系列索引

  【源】从零自学Hadoop系列索引

 

 

 

 

本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作。

文章是哥(mephisto)写的,SourceLink


还没有评论.