星空网 > 软件开发 > 数据库

从零自学Hadoop: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。

二:语法:

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

从零自学Hadoop:Hive的CLI和JDBC

   查看有哪些表。

show tables;

从零自学Hadoop:Hive的CLI和JDBC

 

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

select * from score;

从零自学Hadoop:Hive的CLI和JDBC

JDBC

一:介绍

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

二:新建工程

  新建工程com.per.hive

三:引入包

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

commons-logging-1.2.jarhadoop-common-2.6.0.jarhive-exec-0.13.1.jarhive-jdbc-0.13.1.jarhive-service-0.13.1.jarhttpclient-4.3.4.jarhttpcore-4.3.2.jarlog4j-1.2.16.jarslf4j-api-1.7.6.jarslf4j-log4j12-1.7.6.jar

从零自学Hadoop:Hive的CLI和JDBC

四:添加类HiveDemo

从零自学Hadoop:Hive的CLI和JDBC

  添加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();  }

从零自学Hadoop:Hive的CLI和JDBC

五:运行

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

从零自学Hadoop:Hive的CLI和JDBC

  

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

 

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

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

Demo下载

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

系列索引

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

 

 

 

 

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


文章是哥(mephisto)写的,SourceLink





原标题:从零自学Hadoop:Hive的CLI和JDBC

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

fba海派价格:https://www.goluckyvip.com/tag/19541.html
fba海派美国:https://www.goluckyvip.com/tag/19542.html
fba海派日本:https://www.goluckyvip.com/tag/19543.html
fba海外仓储:https://www.goluckyvip.com/tag/19544.html
fba海外仓服务:https://www.goluckyvip.com/tag/19545.html
fba海外仓有什么好处:https://www.goluckyvip.com/tag/19546.html
亚马逊以图搜图功能再更新,99%运营不知道!:https://www.kjdsnews.com/a/1842135.html
短视频广告剪辑技巧 :https://www.kjdsnews.com/a/1842136.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流