环境
  虚拟机:VMware 10
  Linux版本:CentOS-6.5-x86_64
  客户端:Xshell4
  FTP:Xftp4
  jdk8
  hadoop-3.1.1
  apache-hive-3.1.1

一、需求:统计出掉线率最高的前10基站
  数据:
    record_time:通话时间
    imei:基站编号
    cell:手机编号
    drop_num:掉话的秒数
    duration:通话持续总秒数

1、建表

--数据表
create table cell_monitor(
record_time string,
imei string,
cell string,
ph_num string,
call_num string,
drop_num int,
duration int,
drop_rate DOUBLE,
net_type string,
erl string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
STORED AS TEXTFILE; --结果表
create table cell_drop_monitor(
imei string,
total_call_num int,
total_drop_num int,
d_rate DOUBLE
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE;

2、导入数据

LOAD DATA LOCAL INPATH '/root/cdr_summ_imei_cell_info.csv' OVERWRITE INTO TABLE cell_monitor;
#展示前10条
hive> select * from cell_monitor limit 10;
OK
record_time imei cell ph_num call_num NULL NULL NULL net_type erl
-- ::+ - 0.0 G
-- ::+ - 0.0 G
-- ::+ - 0.0 G
-- ::+ - 0.0 G
-- ::+ - 0.0 G
-- ::+ - 0.0 G
-- ::+ - 0.0 G
-- ::+ - 0.0 G
-- ::+ - 0.0 G
Time taken: 0.132 seconds, Fetched: row(s)
hive>

出现NULL 是因为字段类型是非字符串类型,匹配不上 所以显示NULL

3、查询掉线率 倒序排列

from cell_monitor cm
insert overwrite table cell_drop_monitor
select cm.imei,sum(cm.drop_num),sum(cm.duration),sum(cm.drop_num)/sum(cm.duration) d_rate
group by cm.imei
sort by d_rate desc;

二、使用hive实现wordcount

1、建表

--数据表
create table docs(line string);
--结果表
create table wc(word string, totalword int);
hive> create table docs(line string);
OK
Time taken: 0.722 seconds
hive> create table wc(word string, totalword int);
OK
Time taken: 0.045 seconds

2、导入数据

/root/wc:

hadoop hello world
hello hadoop
hbase zookeeper
name name name

导入数据:

hive> load data local inpath '/root/wc' into table docs;
Loading data to table default.docs
OK
Time taken: 0.392 seconds
hive> select * from docs;
OK
hadoop hello world
hello hadoop
hbase zookeeper
name name name
Time taken: 1.728 seconds, Fetched: 4 row(s)

3、统计

hive> select explode(split(line, ' ')) as word from docs;
OK
hadoop
hello
world
hello
hadoop
hbase
zookeeper
name
name
name
Time taken: 0.377 seconds, Fetched: row(s)
hive>

下面统计语句会产生MR任务:

from (select explode(split(line, ' ')) as word from docs) w
insert into table wc
select word, count() as totalword
group by word
order by word;

4、查询结果

hive> select * from wc;
OK
hadoop 2
hbase 1
hello 2
name 3
world 1
zookeeper 1
Time taken: 0.121 seconds, Fetched: 6 row(s)
hive>

【Hive学习之四】Hive 案例的更多相关文章

  1. hive学习(五) 应用案例

    1.实现struct数据结构例子 1.1创建student表 create table student( id int, info struct<name:string,age:int> ...

  2. hive学习(二) hive操作

    hive   ddl 操作官方手册https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL hive  dml 操作官方手 ...

  3. Hive学习之四 《Hive分区表场景案例应用案例,企业日志加载》 详解

    文件的加载,只需要三步就够了,废话不多说,来直接的吧. 一.建表 话不多说,直接开始. 建表,对于日志文件来说,最后有分区,在此案例中,对年月日和小时进行了分区. 建表tracktest_log,分隔 ...

  4. hive学习(四) hive的函数

    1.内置运算符 1.1关系运算符 运算符 类型 说明 A = B 所有原始类型 如果A与B相等,返回TRUE,否则返回FALSE A == B 无 失败,因为无效的语法. SQL使用”=”,不使用”= ...

  5. hive学习(三) hive的分区

    1.Hive 分区partition 必须在表定义时指定对应的partition字段 a.单分区建表语句: create table day_table (id int, content string ...

  6. Hive学习笔记——Hive中的分桶

    对于每一个表(table)或者分区, Hive可以进一步组织成桶,也就是说桶是更为细粒度的数据范围划分.Hive也是针对某一列进行桶的组织.Hive采用对列值哈希,然后除以桶的个数求余的方式决定该条记 ...

  7. Hive学习:Hive连接JOIN用例详解

    1 准备数据: 1.1 t_1 01 张三 02 李四 03 王五 04 马六 05 小七 06 二狗 1.2 t_2 01 11 03 33 04 44 06 66 07 77 08 88 1.3 ...

  8. Hive学习路线图(转)

    Hadoophivehqlroadmap学习路线图   1 Comment Hive学习路线图 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig ...

  9. 【转】Hive学习路线图

    原文博客出自于:http://blog.fens.me/hadoop-hive-roadmap/ 感谢! Hive学习路线图 Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Ha ...

随机推荐

  1. 封装一个axios请求后台的通用方法

    import axios from 'axios'; import constant from '@/js/const'; import alert from '@/js/alertView'; le ...

  2. 【pyqtgraph绘图】Qt速成课程

    解读官方API-Qt速成课程 参考:http://www.pyqtgraph.org/documentation/qtcrashcourse.html Qt速成课程 PyQtGraph广泛使用Qt来生 ...

  3. es组合多个条件进行查询

    GET /test_index/_search{ "query": { "bool": { "must": { "match&qu ...

  4. aws小结

    IAM:亚马逊访问权限控制(AWS Identity and Access Management ) https://www.cnblogs.com/andy9468/p/10635019.html ...

  5. percona顶级项目(针对数据库)

    percona顶级项目(针对数据库) 地址:https://github.com/Percona-Lab 1.mongodb_consistent_backupTool for getting con ...

  6. Java基础知识(重载和覆盖)

    重载(overload): 在一个类中,如果出现了两个或者两个以上的同名函数,只要它们的参数的个数,或者参数的类型不同,即可称之为该函数重载了. 即当函数同名时,只看参数列表.和返回值类型没关系. 重 ...

  7. what's the python之面向对象

    编程分为面向过程和面向对象,首先我们要了解什么是面向对象. 面向对象 面向过程就是我们之前学的内容,主要是函数式,其核心是过程,过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全 ...

  8. Python3学习之路~2.7 文件操作

    对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下 Somehow, it seems the love I knew was always the ...

  9. 基于nodejs的 本地文件夹http服务器:http-server

    请记住,是文件夹服务器 $ npm install http-server -g $ cd /tmp && http-server 或: $ http-server /tmp

  10. 116A

    #include <stdio.h> int main() { int n; int a1,a2; int min=0; int cap=0; scanf("%d",& ...