Hive wordcount

一 使用Hive CLI (old)命令行工具操作HiveQL

进入hive cli

[root@master /]# hive

创建统计表

hive> create table wctest(line,content string);

导入数据

可以将本地文本文件内容批量加载到Hive 表中,要求文本文件中的格式和Hive 表的定义一致,包括:字段个数、字段顺序、列分隔符都要一致。
这里的dealer_info 表的表定义是以\t 作为列分隔符,所以准备好数据后,将文本文件拷贝到hive 客户端机器上后,执行加载命令。load data local inpath '/home/hadoop/dealerinfodata.txt' overwrite into table dealer_info;

  • local 关键字表示源数据文件在本地,源文件可以在HDFS 上,如果在HDFS 上,则去掉local,inpath 后面的路径是类似”hdfs://namenode:9000/user/datapath”这样的HDFS 上文件的路径。
  • overwrite 关键字表示如果hive 表中存在数据,就会覆盖掉原有的数据。如果省略overwrite,则默认是追加数据。加载完成数据后,在HDFS 上就会看到加载的数据文件。
hive>load data inpath '/user/root/wordcount_in/wordcount' overwrite  into table wctest;

进行统计

hive>select a.word,count(*) from (select explode(split(wordcontent,' ')) word from wctest)a group by word;

退出Hive CLI

 hive> quit;
[root@master /]# 

mysql 中的hive数据库存储的是hive操作管理下的字段定义,以及hive数据文件的存储位置

mysql> show tables;

表名标识
COLUMNS_V2hive 表中的数据类型
DBShive 上的数据库列表
SDShive 表物理路径

二 使用Beeline CLI (new)命令行工具操作HiveQL

Beeline version 2.1.1 by Apache Hive

进入beenline

[root@master /]# beeline
beeline> 

链接

beeline> !connect jdbc:hive2://192.168.137.121:10000
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/usr/tools/apache-hive-2.1.1-bin/lib/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/usr/hadoop-2.6.4/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
Connecting to jdbc:hive2://192.168.137.121:10000
Enter username for jdbc:hive2://192.168.137.121:10000: root
Enter password for jdbc:hive2://192.168.137.121:10000: 
Connected to: Apache Hive (version 2.1.1)
Driver: Hive JDBC (version 2.1.1)
17/06/08 11:36:34 [main]: WARN jdbc.HiveConnection: Request to set autoCommit to false; Hive does not support autoCommit=false.
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://192.168.137.121:10000> 

操作同Hive CLI

退出beenline

 0: jdbc:hive2://192.168.137.121:10000> !quit
Closing: 0: jdbc:hive2://192.168.137.121:10000
[root@master /]# 
HiveQL 客户端SQuirrelSQLClient使用配置官网

《Hive wordcount》 enter description here

相关资源

Hive GettingStarted

    原文作者:天堂宝宝_V
    原文地址: https://www.jianshu.com/p/2fe833328b14
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞