1.使用hive实现WordCount

(1) 创建数据库

create database wordcount;

(2) 创建外部表

create external table word_data
(
line string
)
row format delimited fields
terminated by ','
location '/home/hadoop/worddata';

(3) 映射数据表

load data inpath '/home/hadoop/worddata' into table word_data;

(4) 这里假设我们的数据存放在hadoop下,路径为:/home/hadoop/worddata,里面主要是一些单词文件,内容大概为:

hello man
what are you doing now
my running
hello
kevin
hi man

执行了上述hql就会创建一张表src_wordcount,内容是这些文件的每行数据,每行数据存在字段line中,select * from word_data;就可以看到这些数据。

(5) 根据MapReduce的规则,我们需要进行拆分,把每行数据拆分成单词,这里需要用到一个hive的内置表生成函数(UDTF):explode(array),参数是array,其实就是行变多列:

create table words(word string);
insert into table words select explode(split(line, " ")) as word from word_data;

(6) 查看words表内容

hello
man
what
are
you
doing
now
my
running
hello
kevin
hi
man

split是拆分函数,跟java的split功能一样,这里是按照空格拆分,所以执行完hql语句,words表里面就全部保存的单个单词

(7) group by统计单词

 select 
  word,
count(*)
from wordcount.words
group by word;

结果:

are     1
doing 1
hello 2
hi 1
kevin 1
man 2
my 1
now 1
running 1
what 1
you 1

2.使用hive求TOP N

    rank() over()
dense_rank() over()
row_number() over()

3.使用Hive进行行列转换

问题

hive如何将
a b 1
a b 2
a b 3
c d 4
c d 5
c d 6
变为:
a b 1,2,3
c d 4,5,6

数据

test.txt

a       b       1
a b 2
a b 3
c d 4
c d 5
c d 6

答案

(1).建表

drop table tmp_jiangzl_test;
create table tmp_jiangzl_test
(
  col1 string,
  col2 string,
  col3 string
)
row format delimited fields terminated by '\t'
stored as textfile; -- 加载数据
load data local inpath '/home/jiangzl/shell/test.txt' into table tmp_jiangzl_test;

(2).处理

select col1,col2,concat_ws(',',collect_set(col3))
from tmp_jiangzl_test
group by col1,col2;

二、列转行

问题

hive如何将
a b 1,2,3
c d 4,5,6
变为:
a b 1
a b 2
a b 3
c d 4
c d 5
c d 6

答案

(1). 建表

drop table tmp_jiangzl_test;
create table tmp_jiangzl_test
(
col1 string,
col2 string,
col3 string
)
row format delimited fields terminated by '\t'
stored as textfile;

(2). 处理:

select col1, col2, col5
from tmp_jiangzl_test a
lateral view explode(split(col3,',')) b AS col5;

4.使用Hive进留存率统计

游戏公司等会很关注用户留存率问题,这里给出一个模板

SET mapreduce.job.queuename=xxx;
SET mapreduce.job.name=xxx;
SET mapreduce.job.reduces=19;
select '日期', '注册用户数', '次日留存率', '2日留存率', '3日留存率',  dim_date

                ,total_cnt

                ,concat_ws('% | ', cast(round(dif_1cnt*100/total_cnt, 2) as string), cast(dif_1cnt as string))

                ,concat_ws('% | ', cast(round(dif_2cnt*100/total_cnt, 2) as string), cast(dif_2cnt as string))

                ,concat_ws('% | ', cast(round(dif_3cnt*100/total_cnt, 2) as string), cast(dif_3cnt as string))

                ,concat_ws('% | ', cast(round(dif_4cnt*100/total_cnt, 2) as string), cast(dif_4cnt as string))

            from

            (

                select p1.state dim_date

                    ,p1.device_os

                    ,count(distinct p1.user_id) total_cnt

                    ,count(distinct if(datediff(p3.state,p1.state) = 1, p1.user_id, null)) dif_1cnt

                    ,count(distinct if(datediff(p3.state,p1.state) = 2, p1.user_id, null)) dif_2cnt

                    ,count(distinct if(datediff(p3.state,p1.state) = 3, p1.user_id, null)) dif_3cnt

                    ,count(distinct if(datediff(p3.state,p1.state) = 4, p1.user_id, null)) dif_4cnt

                from

                    (

                        select

                            from_unixtime(unix_timestamp(cast(partition_date as string), 'yyyyMMdd'), 'yyyy-MM-dd') state,

                            user_id

                        from user_active_day

                        where partition_date between date1 and date2

                        and user_is_new = 1

                        group by 1,2

                    )p1 --日新增用户名单(register_date,user_id)

                    left outer join

                    (

                        select

                            from_unixtime(unix_timestamp(cast(partition_date as string), 'yyyyMMdd'), 'yyyy-MM-dd') state,

                            user_id

                        from active_users

                        where partition_date between date1 and date2

                        group by 1,2

                    )p3 --期间活跃用户(active_date,user_id)

                    on (p3.user_id = p1.user_id)

                group by 1,2

            ) p4;

Hive实战的更多相关文章

  1. Spark入门实战系列--5.Hive(下)--Hive实战

    [注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 1.Hive操作演示 1.1 内部表 1.1.1 创建表并加载数据 第一步   启动HDFS ...

  2. 60分钟内从零起步驾驭Hive实战学习笔记

    本博文的主要内容是: 1. Hive本质解析 2. Hive安装实战 3. 使用Hive操作搜索引擎数据实战 SparkSQL前身是Shark,Shark强烈依赖于Hive.Spark原来没有做SQL ...

  3. Hive实战之Youtube数据集

    1 数据来源 本次实战的数据来自于"YouTube视频统计与社交网络"的数据集,是西蒙弗雷泽大学计算机学院在2008年所爬取的数据 数据集地址 1. 1 Youtube视频表格式如 ...

  4. 60分钟内从零起步驾驭Hive实战学习笔记(Ubuntu里安装mysql)

    本博文的主要内容是: 1. Hive本质解析 2. Hive安装实战 3. 使用Hive操作搜索引擎数据实战 SparkSQL前身是Shark,Shark强烈依赖于Hive.Spark原来没有做SQL ...

  5. Hive实战UDF 外部依赖文件找不到的问题

    目录 关于外部依赖文件找不到的问题 为什么要使用外部依赖 为什么idea 里面可以运行上线之后不行 依赖文件直接打包在jar 包里面不香吗 学会独立思考并且解决问题 继承DbSearcher 读取文件 ...

  6. Hive实战—时间滑动窗口计算

    关注公众号:大数据技术派,回复: 资料,领取1024G资料. 目录 时间滑动计算 外部调用实现时间循环 自关联实现滑动时间窗口 扩展基于自然周的的滚动时间窗口计算 总结 时间滑动计算 今天遇到一个需求 ...

  7. Hive 实战(1)--hive数据导入/导出基础

    前沿: Hive也采用类SQL的语法, 但其作为数据仓库, 与面向OLTP的传统关系型数据库(Mysql/Oracle)有着天然的差别. 它用于离线的数据计算分析, 而不追求高并发/低延时的应用场景. ...

  8. Hive 实战(2)--hive分区分桶实战

    前言: 互联网应用, 当Mysql单机遇到性能瓶颈时, 往往采用的优化策略是分库分表. 由于互联网应用普遍的弱事务性, 这种优化效果非常的显著.而Hive作为数据仓库, 当数据量达到一定数量时, 查询 ...

  9. Python之大数据库hive实战

    今天和大家分享的是Python如何连接hive数据库来进行hivesql的查询操作.   step1:环境准备 Python版本:3.6.2 Windows版本:Windows10版本的64位 ste ...

  10. HIve实战分析Hadoop的日志

    1.日志格式分析首先分析 Hadoop 的日志格式, 日志是一行一条, 日志格式可以依次描述为:日期.时间.级别.相关类和提示信息.如下所示: -03-06 15:23:48,132 INFO org ...

随机推荐

  1. 为R Markdown配置TinyTex编译环境

    技术背景 在前面一篇博客中,我们介绍了一些关于在Windows系统上安装R Studio来编写R Markdown,最后编译成Beamer的演示文档的过程.而在Windows系统的使用过程中发现,编译 ...

  2. Makeflie脚本使用

    1.目标 2.Makefile的作用 自动化编译仿真 文件有引用层级关系,Tb会引用RTL顶层,RTL顶层也会引用一些其他的小的模块,编译的时候被引用的文件需要先进行编译. 脚本有两种模式,debug ...

  3. 例2.6 设计一个高效的算法,从顺序表L中删除所有值为x的元素,要求时间复杂度为0(n)空间复杂度为0(1)。

    1.题目 例2.6 设计一个高效的算法,从顺序表L中删除所有值为x的元素,要求时间复杂度为0(n)空间复杂度为0(1). 2.算法思想 3.代码 void DeleteX(SeqList LA, Se ...

  4. 探讨Java死锁的现象和解决方法

    死锁是多线程编程中常见的问题,它会导致线程相互等待,无法继续执行.在Java中,死锁是一个需要注意和解决的重要问题.让我们通过一系列详细的例子来深入了解Java死锁的现象和解决方法. 1. 什么是死锁 ...

  5. [转帖]AMD处理器ZEN一代之国产化海光

    https://huataihuang.gitbook.io/cloud-atlas-draft/os/linux/kernel/cpu/amd_hygon   2020年国产化处理器受到了广泛的关注 ...

  6. [转帖]tidb 搭建私有镜像库

    https://docs.pingcap.com/zh/tidb/stable/tiup-mirror 在构建私有云时,通常会使用隔离的网络环境,此时无法访问 TiUP 的官方镜像.因此,TiUP 提 ...

  7. [转帖]rclone将本地文件或文件夹导入minio中

    1.背景:公司数据迁移涉及到文件迁移,原有文件服务器没有使用minio,但是现在的新系统使用了minio.所以这就需要我们将文件上传到minio文件服务器中:由于历史文件数据量大,甲方要求可以通过服务 ...

  8. [转帖]关于Linux操作系统中LUN的队列深度(queue_depth)

    Linux中的queue_depth(队列深度),可以用lsscsi查看. 不过今天在我的vm 虚拟机环境中(无外界存储),是没有lsscsi命令. 不过,从网上,搜到了如下的信息: $ lsscsi ...

  9. [转帖]Linux 内核 | 网络流量限速方案大 PK

    https://maimai.cn/article/detail?fid=1674483493&efid=UXVPILU_JTlqLrYhTkDStA 网络流量限速是一个经久不衰的话题,Lin ...

  10. [转帖]linux 系统级性能分析工具 perf 的介绍与使用

    目录 1. 背景知识 1.1 tracepoints 1.2 硬件特性之cache 2. 主要关注点 3. perf的使用 3.0 perf引入的overhead 3.1 perf list 3.2 ...