首页 > 其他分享 >Hive实战

Hive实战

时间:2022-10-27 22:11:33浏览次数:83  
标签:实战 p1 word string dif Hive state user

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表内容

OK
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;

wordcount.words 库名称.表名称,group by word这个word是create table words(word string) 命令创建的word string

结果:

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进行行列转换

1、问题

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

 

2、数据

test.txt

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

3、答案

(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;

 

二、列转行

1、问题

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

2、答案

(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;

 

标签:实战,p1,word,string,dif,Hive,state,user
From: https://www.cnblogs.com/yeyuzhuanjia/p/16834197.html

相关文章

  • Hive调优
    1.Fetch抓取Fetch抓取是指Hive中对某些情况的查询可以不必使用MapReduce计算。例如:SELECT*FROMemployees;在这种情况下,Hive可以简单地读取employee对应的存储目录下的文......
  • hashlib模块、subprocess模块、loggin日志模块及实战
    hashlib加密模块1.何为加密 将明文数据处理成密文数据让人无法看懂2.为什么加密 保证数据的安全3.如何判断数据是否是加密的 一串没有规律的字符串(数字、字母、符......
  • docker如何实现隔离(chrono《kubernetes入门实战课》笔记整理)
    linux操作内核中,为资源隔离提供了三种技术:namespace、cgroup、chroot。容器就是操作系统里一个特殊的“沙盒”环境,与外部系统隔离。隔离是为了系统安全考虑。namespace:可......
  • docker engine内部角色和工作流程(转载极客时间chrono《kubernetes入门实战课》)
    docker有两种产品形态:dockerdesktop和dockerengine。dockerdesktop是针对个人使用设计的,有直观图形界面,支持mac和Windows快速安装,方便易用,但由于为商业产品,有一些docke......
  • Arctic 基于 Hive 的流批一体实践
    背景随着大数据业务的发展,基于Hive的数仓体系逐渐难以满足日益增长的业务需求,一方面已有很大体量的用户,但是在实时性,功能性上严重缺失;另一方面Hudi,Iceberg这类系统在......
  • 斑马打印机ZPL语言编程实战
    什么是ZPL语言?ZPLZPL是斑马条码打印机工业型号用的编程语言。利用这些编程语言,编辑好一个打印的指令集,发送给条码打印机,条码打印机就会把ZPL所绘制的标签打印出来。EP......
  • Go 云原生实战:如何增加 Web 应用配置模块
    1介绍当我们为自己编写程序时,通常会将一些重要的配置项直接写在源代码里,比如:服务器监听的端口、数据库使用的名称和端口号、HTTP请求超时的持续时间...但是,如果我们尝试将......
  • 100_实战:京东搜索
    目录效果图项目搭建创建模块自定义ES依赖版本添加依赖com.alibaba.fastjson删除多余的文件配置application.properties拷贝基本静态文件cssjsimages拷贝页面index.ht......
  • koa实战
    项目初始化//初始化项目,生成package.jsonnpminit指定入口文件main.js项目的基础搭建创建src工作目录创建main.js主入口文件在main.js中引入koacon......
  • Go配置文件绑定到结构体实战演示
    说明在实际的开发过程中,我们必然会用到MySQL、Redis等这样的服务。为了实现系统的配置化,我们会把一些配置信息单独放在一些文件中,使用到的地方直接读取配置文件即可。常见......