HIVE离线案例理解及测试

xiaoxiao2021-02-28  107

1.      离线案例理解及测试(创建表 加载数据导出输出) shell实现自动执行(选做)

Hive在离线分析平台的应用

 

思路:

a.创建什么样的表(ETL过滤清洗:写MR程序/过滤空值,脏数据/字段解析/补全)

         b.根据业务(7个:用户模块,订单模块,地域维度模块),创建中间表,大表拆小表,统计分析模块可以减少数据量的反复加载.

 

【案例】

1.创建二级分区表

@创建数据库

ß------Hive---------->

 

create databasedb_track;

 

//创建二级分区表

Create  table  testlog(

 

 

id                    string,

url                   string,

referer               string,

keyword               string,

type                  string,

guid                  string,

pageId                string,

moduleId              string,

linkId                string,

attachedInfo          string,

sessionId             string,

trackerU              string,

trackerType           string,

ip                    string,

trackerSrc            string,

......                       .........

provinceId            string,

cityId                string,

fee                   string,

buttonPosition        string

)

partitioned by(datestring,hour string)

row formatdelimited fields terminated by '\t'

location'/test/test3';

@desc formattedtest3查看分区信息

@向分区表中添加数据

load data localinpath '/home/vampire/test3/20150828/2015082818' into table test3partition(date="20150828",hour="18");

 

load data localinpath '/home/vampire/test3/20150828/2015082819' into table test3partition(date="20150828",hour="19");

@再建一个表(临时表/外表)接收需要的字段信息(需求来定)

create tableresoult(

date string,

hour string,

pv int,

uv int,

ip int

);

row format delimitedfields terminated by '\t';

 

 

 

记得desc formatted resoult看下表的基本信息;

 

@查询所需数据并将结果插入到结果表中;

insert overwritetable resoult select date,hour,count(url) pv,count(distinct guid)uv,count(distinct ip) from test3 group by date,hour;

Hive创建表的多种方式: http://blog.csdn.net/qq_39532946/article/details/76595182

Example:

create table test4 row format delimited fields terminated by ','  as select date,hour,count(url)pv,count(distinct guid) uv,count(distinct ip) from test3 group by date,hour;

 

 

 

@将统计分析表导出到mysql,(SQOOP支持mysql/hdfs/hive各平台间表的数据传输和CRUD操作);

ß------mysql---------->

create tabledairy(

datevarchar(20),

hourvarchar(20),

pv int,

uv int,

ip int

);

 

 

 

ß------sqoop导出数据---------->

bin/sqoop export\

--connectjdbc:mysql://vampire04:3306/test \

--username root\

--password123456 \

--table  dairy \

--num-mappers 1\

--export-dir  /user/hive/warehouse/test.db/test4 \

--export-dirhive中使用desc formatted static_PVUV;来确认表的目录所在

--fields-terminated-by ‘,’

--export-dir在hive中使用desc formattedstatic_PVUV;来确认表的目录所在

 

 

@可以使用shell脚本结合定时脚本周期性自动执行获得最新的信息覆盖或者追加到信息跟踪表中\

Create  table  testlog(

id                    string,

productId             string,

curMerchantId         string,

…                   …

resultSum             string,

currentPage           string,

linkPosition          string,

buttonPosition        string

)

partitioned by(datestring,hour string)

row formatdelimited fields terminated by '\t'

 

#!/bin/sh

##执行环境变量,以便于脚本中可以使用。“.”的后面有一个空格,代表执行这个脚本文件

. /etc/profile

 

# 定义Hive目录

HIVE_DIR=/opt/modules/cdh/hive-0.13.1-cdh5.3.6

 

# 定义数据源目录

DATA_LOG=/home/vampire/tracklogs

 

# 定义昨天的日期,按照固定格式,可以使用echo $(date --date="1 day ago" +%Y%m%d)尝试

YESTERDAY=$(date--date="1 day ago" +%Y%m%d)

 

# for循环中do...done中间的循环体执行的次数等于in后面元素的个数

# 在我们这里,hql文件被调用了2次

for LOG_DAY in`ls $DATA_LOG/$YESTERDAY`

do

#分别取出文件名中的日期和小时

DATE=${LOG_DAY:0:8}

HOUR=${LOG_DAY:8:2}

 

#在测试时,可以使用echo打印下面命令

$HIVE_DIR/bin/hive-e "load data local inpath '$DATA_LOG/$YESTERDAY/${DATE}${HOUR}' intotable testdb.testlog partition (date='$DATE',hour='$HOUR')"

done

 

 

===crontable 定时任务

30 0  * * *    /bin/sh /home/vampire/loaddata.sh   

定时任务详解:http://blog.csdn.net/qq_39532946/article/details/75578290

转载请注明原文地址: https://www.6miu.com/read-18688.html

最新回复(0)