惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Hacker News: Ask HN
Hacker News: Ask HN
N
News and Events Feed by Topic
Forbes - Security
Forbes - Security
The Last Watchdog
The Last Watchdog
TaoSecurity Blog
TaoSecurity Blog
Schneier on Security
Schneier on Security
SecWiki News
SecWiki News
V
Vulnerabilities – Threatpost
Project Zero
Project Zero
O
OpenAI News
W
WeLiveSecurity
Security Archives - TechRepublic
Security Archives - TechRepublic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
H
Hacker News: Front Page
Cisco Talos Blog
Cisco Talos Blog
Spread Privacy
Spread Privacy
Help Net Security
Help Net Security
P
Privacy & Cybersecurity Law Blog
K
Kaspersky official blog
S
Security @ Cisco Blogs
Latest news
Latest news
AWS News Blog
AWS News Blog
U
Unit 42
Martin Fowler
Martin Fowler
阮一峰的网络日志
阮一峰的网络日志
S
Secure Thoughts
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Know Your Adversary
Know Your Adversary
Scott Helme
Scott Helme
博客园 - 司徒正美
B
Blog RSS Feed
C
Check Point Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
Docker
Google Online Security Blog
Google Online Security Blog
Jina AI
Jina AI
aimingoo的专栏
aimingoo的专栏
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Last Week in AI
Last Week in AI
月光博客
月光博客
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
SegmentFault 最新的问题
NISL@THU
NISL@THU
T
The Blog of Author Tim Ferriss
C
Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
小众软件
小众软件

博客园 - sinodzh

从零自学Hadoop(25):Impala相关操作下 从零自学Hadoop(24):Impala相关操作上 从零自学Hadoop(23):Impala介绍及安装 Hadoop技巧(04):简易处理solr date 时区问题 从零自学Hadoop(22):HBase协处理器 从零自学Hadoop(21):HBase数据模型相关操作下 从零自学Hadoop(20):HBase数据模型相关操作上 从零自学Hadoop(19):HBase介绍及安装 Hadoop技巧(03):HostName命名带来的问题 Hadoop技巧(02):时间同步 使用Nexus搭建Maven本地仓库 Hadoop技巧(01):插件,终端权限 Hadoop技巧系列索引 从零自学Hadoop(17):Hive数据导入导出,集群数据迁移下 从零自学Hadoop(16):Hive数据导入导出,集群数据迁移上 从零自学Hadoop(15):Hive表操作 从零自学Hadoop(14):Hive介绍及安装 从零自学Hadoop(13):Hadoop命令下 从零自学Hadoop(12):Hadoop命令中
从零自学Hadoop(18):Hive的CLI和JDBC
sinodzh · 2016-02-04 · via 博客园 - sinodzh

阅读目录

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

     上一篇,我们对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

   查看有哪些表。

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

JDBC

一:介绍

  我们可以在代码中通过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下载

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

系列索引

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

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