两种情况下不走map-reduce:

1. where ds >' ' //ds 是partition

2. select * from table //后面没有查询条件,什么都没有

1.建表

CREATE TABLE sal(
id INT,
name STRING,
salary INT
)
partitioned by (city string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE;

1.1 修改表及属性

#把id,name以外的列删除
alter table sal replace columns (id int, name string);
#增加列
alter table sal add columns (remark string);
#修改column
ALTER TABLE table_name
CHANGE col_old_name col_new_name
column_type; ALTER TABLE sal CHANGE remark city string;

2.导入数据

load data local inpath '/home/hadoop/in/mytable' overwrite into table sal;
1 zuansun 3000 none
2 zuansu2 4000 none
3 zuansu3 3000 none
4 zuansu4 4000 none
5 zuansu5 3000 none
6 zuansu6 4000 none
7 zuansu7 3000 none
8 zuansu8 4000 none
9 zuansu9 10000 none
10 zuansu10 20000 none
11 zuansu11 15000 none
12 zuansu12 25000 none

3.嵌套查询

from (select * from sal) e select e.id,e.name,e.salary  where e.salary>3000;
#case when
select id,name,
case
when salary<10000 then '屌丝'
when salary>=10000 and salary<20000 then '中下等'
when salary>=20000 and salary<50000 then '高帅富'
else '外星人'
end as salarylevel
from sal;

4.group by

select remark,sum(salary) from sal group by remark;

5.动态分区

5.1 创建临时表

CREATE TABLE sal_tmp(
id INT,
name STRING,
salary INT,
city string
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE;

5.2将数据导入到临时表中

load data local inpath '/home/hadoop/in/mytable' overwrite into table sal_tmp;

5.3 操作的配置

set hive.exec.dynamic.partition=true; // 允许动态分区
set hive.exec.dynamic.partition.mode=nonstrict;
set hive.exec.dynamic.partitions.pernode=50000;
set hive.exec.dynamic.partitions.partitions=50000;
set hive.exec.max.created.files=500000;
set mapred.reduce.tasks =20000; //每个任务默认的reduce数目
set hive.merge.mapfiles=true; //在Map-only的任务结束时合并小文件

5.4附partition相关参数:

hive.exec.dynamic.partition(缺省false): 设置为true允许使用dynamic partition

hive.exec.dynamic.partition.mode(缺省strick):设置dynamic partition模式(nostrict允许所有partition列都为dynamic partition,strict不允许)

hive.exec.max.dynamic.partitions.pernode (缺省100):每一个mapreduce job允许创建的分区的最大数量,如果超过了这个数量就会报错

hive.exec.max.dynamic.partitions (缺省1000):一个dml语句允许创建的所有分区的最大数量

hive.exec.max.created.files (缺省100000):所有的mapreduce job允许创建的文件的最大数量









5.5

insert into table sal partition (city) select * from sal_tmp;

6. join操作

#建表
create table a(id int,gender string)
row format delimited fields terminated by '\t' stored as textfile;
#加载数据
load data local inpath '/home/hadoop/in/a' overwrite into table a;
#内连接查询
select sal.id,sal.name,sal.salary,sal.city,a.gender from sal join a on(sal.id=a.id);
#左外连接查询
select sal.id,sal.name,sal.salary,sal.city,a.gender from sal left outer join a on(sal.id=a.id);

7.创建索引

create index a_index on table a(id) AS  'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' WITH DEFERRED REBUILD ;     

8.桶

#临时表
create table tb_tmp(id int,age int, name string ,timeflag bigint) row format delimited fields terminated by ',';
#带桶的表,4个桶
create table tb_stu(id int,age int, name string,timeflag bigint) clustered by (id) sorted by (age) into 4 buckets row format delimited fields terminated by ',';
#加载数据到临时表
load data local inpath '/home/hadoop/in/tb_tmp' overwrite into table tb_tmp; 1,20,zxm,20140330
2,21,ljz,20140330
3,19,cds,20140330
4,18,mac,20140330
5,22,android,20140330
6,23,symbian,20140330
7,25,wp,20140330
8,20,cxd,20140330
9,21,fvd,20140330
10,19,cvb,20140330
11,18,erd,20140330
12,22,nid,20140330
13,23,fvd,20140330
14,19,cvb,20140330
15,18,e33,20140330
16,22,nid,20140330
#设置执行桶的属性
set hive.enforce.bucketing = true;
#插入到tb_stu表
insert into table tb_stu select * from tb_tmp;
#抽样
select * from tb_stu tablesample(bucket 1 out of 4 on id);
注:tablesample是抽样语句,语法:TABLESAMPLE(BUCKET x OUT OF y),相当于以下语句:
SELECT * FROM numbersflat WHERE number % y = x-1;

9.RCfile

#rcfile 格式表
create table tb_rc(id int,age int, name string ,timeflag bigint) row format delimited fields terminated by ',' stored as rcfile;
#插入数据,上表中已经有tb_tmp表,所以直接插入数据即可
insert into table tb_rc select * from tb_tmp;

10.分隔符的多样化(配合正则表达式使用)

#cat /tmp/liuxiaowen/1.txt

000377201207221125^^APPLE IPHONE 4S^^2
132288201210331629^^THINKING IN JAVA^^1
132288201210331629^^THIN ssss^^1111
132288201210331629^^THdd dd ddJAVA^^10 add jar /opt/app/hive-0.7.0-rc1/lib/hive-contrib-0.7.0.jar ; create external table tt(times string,
product_name string,
sale_num string
) ROW FORMAT
SERDE 'org.apache.hadoop.hive.contrib.serde2.RegexSerDe'
WITH SERDEPROPERTIES
( 'input.regex' = '([^^]*)\\^\\^([^^]*)\\^\\^([^^]*)',
'output.format.string' = '%1$s %2$s %3$s')
STORED AS TEXTFILE; load data local inpath '/home/hadoop/in/tt' overwrite into table tt; hive> select product_name from tt; APPLE IPHONE 4S
THINKING IN JAVA
THIN ssss
THdd dd ddJAVA

11.更加复杂的数据类型

11.1 array

cat login_array.txt
192.168.1.1,3105007010|3105007011|3105007012
192.168.1.2,3105007020|3105007021|3105007022 CREATE TABLE login_array (
ip STRING,
uid array<BIGINT>
)
PARTITIONED BY (dt STRING)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY '|'
STORED AS TEXTFILE;
加载数据到hive表
LOAD DATA LOCAL INPATH '/home/hadoop/in/login_array' OVERWRITE INTO TABLE login_array PARTITION (dt='20130101');
#查看数据
select * from login_array;
192.168.1.1 [3105007010,3105007011,3105007012] 20130101
192.168.1.2 [3105007020,3105007021,3105007022] 20130101
select ip,uid[0] from login_array where dt='20130101'; --使用下标访问数组
192.168.1.1 3105007010
192.168.1.2 3105007020
select ip,size(uid) from login_array where dt='20130101'; #查看数组长度
192.168.1.1 3
192.168.1.2 3
select * from login_array where array_contains(uid,3105007010);#数组查找
192.168.1.1 [3105007010,3105007011,3105007012] 20130101

11.2 使用Map

cat map_test_raw:
2014-03-03 12:22:34#127.0.0.1#get#amap#src=123&code=456&cookie=789#status=success&time=2s
2014-03-03 11:22:34#127.0.0.1#get#autonavi#src=123&code=456#status=success&time=2s&cookie=789
#创建表
create external table map_test_raw(ts String,ip String,type String,logtype String,request Map<String,String>,response Map<String,String>)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '#'
COLLECTION ITEMS TERMINATED BY '&'
MAP KEYS TERMINATED BY '='
stored as textfile;
LOAD DATA LOCAL INPATH '/home/hadoop/in/map_test_raw' OVERWRITE INTO TABLE map_test_raw;
#查看数据
select * from map_test_raw;
2014-03-03 12:22:34 127.0.0.1 get amap {"src":"123","code":"456","cookie":"789"} {"status":"success","time":"2s"}
2014-03-03 11:22:34 127.0.0.1 get autonavi {"src":"123","code":"456"} {"status":"success","time":"2s","cookie":"789"}

11.3 使用struct

# cat login_struct.txt
192.168.1.1,zhangsan:40
192.168.1.1,lisi:41
192.168.1.1,gavin:42
192.168.1.1,wangwu:43
192.168.1.1,xiaoming:44
192.168.1.1,xiaojun:45
# 建表
CREATE TABLE login_struct (
ip STRING,
user struct<name:string,age:int>
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
COLLECTION ITEMS TERMINATED BY ':'
STORED AS TEXTFILE;
#导入数据
LOAD DATA LOCAL INPATH '/home/hadoop/in/login_struct' OVERWRITE INTO TABLE login_struct;
#查看数据
select ip,user from login_struct; 192.168.1.1 {"name":"zhangsan","age":40}
192.168.1.1 {"name":"lisi","age":41}
192.168.1.1 {"name":"gavin","age":42}
192.168.1.1 {"name":"wangwu","age":43}
192.168.1.1 {"name":"xiaoming","age":44}
192.168.1.1 {"name":"xiaojun","age":45}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Hive 进阶的更多相关文章

  1. Hive进阶(下)

    Hive进阶(下) Hive进阶(下) Hive的表连接 等值连接 查询员工信息:员工号.姓名.月薪.部门名称 1.select e.empno,e.ename,e.sal,d.dname2.from ...

  2. Hive进阶(上)

    Hive进阶(上) Hive进阶(上) 执行数据导入 使用Load语句 语法: 1.LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE ...

  3. Hive学习之六 《Hive进阶— —hive jdbc》 详解

    接Hive学习五 http://www.cnblogs.com/invban/p/5331159.html 一.配置环境变量 hive jdbc的开发,在开发环境中,配置Java环境变量 修改/etc ...

  4. Hive学习之五 《Hive进阶—UDF操作案例》 详解

    hive—UDF操作 udf的操作过程: 在HIVE会话中add 自定义函数的jar文件,然后创建function,继而使用函数. 下面就以下面课题为例: 课题:统计每个活动的PV和UV 一.Java ...

  5. hive 进阶笔记

    -- mysql方式 create table account_channel(account_ String,channel_ String) as select a.account,b.chann ...

  6. Hive进阶_汇总

    =========================================================================== 第2章 Hive数据的导入 使用Load语句执行 ...

  7. Hive进阶_开发Hive的自定义函数

    Hive中的自定义函数简介 (1) 在类中创建自定义函数.自定义UDF需要继承'org.apache.hadoop.hive.ql.exec.UDF',实现evaluate函数,evaluate函数支 ...

  8. Hive进阶_Hive的客户端操作

    启动远程客户端 # hive --service hiveserver2获取连接-〉创建运行环境-〉执行HQL-〉处理结果-〉释放资源 工具类 package demo.utils; import j ...

  9. Hive进阶_Hive的子查询

    - 集合中如果含null数据,不可使用not in, 可以使用in- hive只支持where和from子句中的子查询- 主查询和自查询可以不是同一张表 select e.ename from emp ...

随机推荐

  1. linux下扩展root分区

    1 查看当前磁盘情况 fdisk -l /dev/sda1 2048 6143 2048 83 Linux /dev/sda2 * 6144 1054719 524288 83 Linux /dev/ ...

  2. JDK8新特性:接口的静态方法和默认方法

    在jdk8之前,interface之中可以定义变量和方法,变量必须是public.static.final的,方法必须是public.abstract的.由于这些修饰符都是默认的,所以在JDK8之前, ...

  3. LINQ 学习路程 -- 查询操作 Average Count Max Sum

    IList<, , }; var avg = intList.Average(); Console.WriteLine("Average: {0}", avg); IList ...

  4. Base64Util工具类

    package com.qianmi.weidian.common.util; import java.io.*; /** * This class provides encode/decode fo ...

  5. 图像处理检测方法 — ORB(Oriented FAST and Rotated BRIEF)

    1.FAST  FAST算子的基本原理是:若某像素点与其周围领域内足够多的连续的像素点存在某一属性差异,并且该差异大于指定阈值,则可以断定该像素点与其邻域像素有可被识别的不同之处,可以作为一个特征点( ...

  6. php 二维数组验证一个值是否存在

    php 判断数字在二维数组里 $arr = array( array('a', 'b'), array('c', 'd') ); in_array('a', $arr); // 此时返回的永远都是 f ...

  7. codeforces 653C C. Bear and Up-Down(乱搞题)

    题目链接: C. Bear and Up-Down time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  8. Java 处理批量数据提交

    在Java web开发过程中,处理表单数据是很重要一部分,常见的是form post处理单条数据的,但也会遇到一次提交多条数据到服务器的,如下:

  9. Solaris/Linux 命令手册

    无意翻到之前收藏的一个文档,共享一下. Solaris/Linux 命令手册 1. 系统 # passwd:修改口令 # exit:退出系统 2. 文件 # cp:复制文件或目录,参数:-a递归目录, ...

  10. OSS阿里云文件上传 demo。

    所需jar包: aliyun-openservices-1.2.3.jar jdom-1.1.jar commons-codec-1.4.jar commons-logging-1.1.1.jar g ...