1、使用create命令创建一个新表
例如:create table if not exists db_web_data.track_log(字段)
partitioned by (date string,hour string)
row format delimited fields terminated by ‘\t’;
2、把一张表的某些字段抽取出来,创建成一张新表
例如:create table backup_track_log as select * from db_web_data.track_log;
尖叫提示:会复制属性以及属性值到新的表中
3、复制表结构
例如:create table like_track_log like db_web_data.track_log;
尖叫提示:不会复制属性值,只会复制表结构。
Hive表导入数据方式
1、本地导入
load data local inpath ‘local_path/file’ into table 表名称 ;
2、HDFS导入
load data inpath ‘hdfs_path/file’ into table 表名称 ;
3、覆盖导入
load data local inpath ‘path/file’ overwrite into table 表名称 ;
load data inpath ‘path/file’ overwrite into table 表名称 ;
4、查询导入
create table track_log_bak as select * from db_web_data.track_log;
5、insert导入–常用
** 追加-append-默认方式
insert into table 表名 select * from track_log;
** 覆盖-overwrite-显示指定-使用频率高
insert overwrite table 表名 select * from track_log;
Hive表导出数据方式
1、本地导出
例如:insert overwrite local directory “/home/admin/Desktop/1/2” row format delimited fields terminated by ‘\t’ select * from db_hive_demo.emp ;
insert overwrite local directory "/home/admin/Desktop/1/2"指定一个本地的目录, select * from db_hive_demo.emp ;查询,将查询出来的结果全部放到对应的目录下,中间的不用管。
尖叫提示:会递归创建目录,但是注意home目录其他用户是没有可写权限的,只有root用户有。
2、HDFS导出
例如:insert overwrite diretory “path/” select * from staff;
3、Bash shell覆盖追加导出
例如:$ bin/hive -e “select * from staff;” > /home/z/backup.log
4、Sqoop
public cLass LogCleanMapperextendsMapper<Longwritable, Text, Longwritable, Text>{
@OVerride
protectedvoidmap(Longwritable key, Text value, Contextcontext) throws IOException, InterruptedException{//上图存在url为空的情况,这个时候通过split("\t")的时候,可能出现只有两个tab键,这个时候就少了一个字段相当于,选择过滤掉这条数据。//所以判断字段,如果是空的或者是“”的,将不将这条数据写入MapReduce
String line value tostring()//id guid provinceId
String[] splits Line split(\t")if(splits, length 32){
context.getCounter("Web Data ETL","Data Length is too short").increment(lL);:return:}
String url splits[l]:if(StringUtils,isBLank(urL)){
context.getCounter("Web Data ETL",url is blank"). inc rement(IL);return;}
String provinceId spLits[20]:
int provinceIdInt Integer MAXVALUE;try{//这里对省份还进行了一次处理,如果省份是汉字的话经过pareInt函数的时候会报错//通过这种方法将省份字段格式错误的过滤掉//不将这行写入MapReduce
provinceIdInt Integer parseInt(provinceId):}catch(Exception e){
context.getCounter("Web Data ETL","provinceId is UNKNOWN"). inc rement(1L);return:}if(StringUtils.isBLank(provinceId)){
context.getCounter("Web Data ETL","provinck ).increment(1L);return}
context.write(key,value);}}
public cLass LogCleanReduceextendsReducer<LongWritable, Text, Text, NulLwritable>{
oVerride
protectedvoidreduce(Longwritable key, Iterable<Text> values, Context context)throws IOException, Ir...){for(Text value values)t
context.write(value, NulLWritable get());}}
order by
全局排序,就一个Reduce
sort by
相当于对每一个Reduce内部的数据进行排序,不是全局排序。
distribute by
类似于MRpartition, 进行分区,一般要结合sort by使用。分成的区就是Reduce
cluster by
当distribute和sort字段相同时,就是cluster by,意思就是distribute和sort后面跟的是一个字段,可以直接写成cluster by 这个字段
案例见:HQL案例.txt
insert overwrite local directory "/home/admin/result/order" row format delimited fields terminated by "/t"
select *from emp order by empno;
//distribute英文的意思分开、散步、把。。。分类,按照部门编号把信息归到一个类中,有点类似部门相同的人分到一个分区,会产生若干文件,每个文件中都是相同部门的人。
insert overwrite local directory "/home/admin/result/sort" row format delimited fields terminated by "/t"
select *from emp distribute by deptno sort by empno
怎么查在任务执行的过程中涉及到的参数
如上图中的参数,可以通过hive客户端临时设置
如果直接运行命令set mapreduce.job.reduces; 会显示当前的设置,运行set mapreduce.job.reduces = 3;是设置将要改变的值。
设只为三个之前,distribute by 和前面的 order by是没有区别的,如下图所示
设置为mapreduce.job.reduces = 3;之后再次运行上面的distribute语句,变成了三个,为什么是三个?因为上面的语句根据deptno分成了三个。
业务案例梳理
需求:执行周期性任务,每天的晚上6点,执行自动化脚本,
加载昨天的日志文件到HDFS,
同时分析网站的多维数据(PV,UV按照省份和小时数进行分类查询)
最后将查询的结果,存储在一张临时表中(表字段:date,hour,provinceId,pv,uv)存储在HIVE中,并且
将该临时表中的所有数据,存储到MySQL中,以供第二天后台开发人员的调用,展示。
1、定时加载本地数据到HDFS,涉及到:auto.sh,crontab
2、清洗数据,打包jar,定时执行,
/user/hive/warehouse/db_web_data.db/track_log/date=20150828/hour=18
part-000001
/user/hive/warehouse/db_web_data.db/track_log/date=20150828/hour=19
part-000001
3、建表track_log,也不需要建立现成的分区,临时指定清洗好的数据作为仓库源
alter table track_log add partition(date=‘20150828’,hour=‘18’) location
“/user/hive/warehouse/db_web_data.db/track_log/date=20150828/hour=18”;
alter table track_log add partition(date=‘20150828’,hour=‘18’) location
“/user/hive/warehouse/db_web_data.db/track_log/date=20150828/hour=19”;
4、开始分析想要的数据,将结果存储在Hive的临时表中
创建临时表:
create table if not exists temp_track_log(date string, hour string, provinceId string, pv string, uv string)
row format delimited fields terminated by ‘\t’;
向临时表中插入数据:
insert overwrite table temp_track_log select date, hour, provinceId, count(url) pv, count(distinct guid) uv
from track_log where date=‘20150828’ group by date, hour, provinceId;
5、使用自定义的JAR,导入本地导出的文件到MYsql或者使用Sqoop。
#exportHADOOP_COMMON_HOME=
#hadoop的绝对路径
exportHADOOP_COMMON_HOME=/opt/modules/cdh/hadoop-2.5.0-cdh5.3.6/
#Set path to where hadoop-*-core.jar is available
#exportHADOOP_MAPRED_HOME=exportHADOOP_MAPRED_HOME=/opt/modules/cdh/hadoop-2.5.0-cdh5.3.6/
#set the path to where bin/hbase is available
#还没学先不管
#exportHBASE_HOME=
#Set the path to where bin/hive is available
#exportHIVE_HOME=
#HIVE路径
exportHIVE_HOME=/opt/modules/cdh/hive-0.13.1-cdh5.3.6/
#Set the path for where zookeper config dir is
#exportZOOCFGDIR=exportZOOCFGDIR=/opt/modules/cdh/zookeeper-3.4.5-cdh5.3.6/exportZOOKEEPER_HOME=/opt/modules/cdh/zookeeper-3.4.5-cdh5.3.6/
一会可能会有一个警告,因为hbase没有配置,可以先不用管
4、拷贝jdbc驱动到sqoop的lib目录下
cp -a mysql-connector-java-5.1.27-bin.jar /opt/modules/cdh/sqoop-1.4.5-cdh5.3.6/lib/
mysql> create database company;
mysql> create table staff(
id int(4) primary key not null auto_increment,
name varchar(255) not null,
sex varchar(255) not null);
mysql> insert into staff(name, sex) values(‘Thomas’, ‘Male’);
Step3、操作数据