LOAD DATA LOCAL INPUT 'input/hive/partitions/file1'
INTO TABLE logs
PARTITION (dt='2001-1-1',country='GB');
在文件系统中,分区只是表目录下嵌套的子目录。如按分区检索数据其实就是按文件系统目录检索数据了。
SHOW PARTITIONS logs; 命令会返回当前表中所有分区信息。PARTITIONED BY 子句中的列是表中正式的列,称为分区列。但是数据文件并不包含这些列,因为在目录中包含了。可以像普通列一样使用分区列(partition column)。
select ts,dt,line
from logs
where country='GB'
桶可以获得更高的查询处理效率;桶可以使取样操作更高效。使用CLUSTERED BY 子句来制定划分桶所用的列和要划分的桶的个数。
create table bucketed_records(year int,temperature STRING,quality STRING,station STRING)
clustered by (year) sorted by (year ASC) into 4 buckets;
select year,temperature from records
distribute by year
sort by temperature desc;
内连接:
Hive 不支持 where sales.id=things.id 这样的连接操作!
select * from sales.*,things.*
from sales join things on (sales.id = things.id);
三种外连接:(左外,右外,全外)
select * from sales.*.things.*
from sales left outer jion things on (sales.id=things.id);
select * from sales.*.things.*
from sales right outer jion things on (sales.id=things.id);
select * from sales.*.things.*
from sales full outer jion things on (sales.id=things.id);
半连接:
Hive不支持IN操作,但是可以使用left semi join 达到相同的效果:(sales表不能再select 后面出现,感觉类似exist了)
select * from things left semi join sales on (sales.id=things.id);
指定map连接:
如果有一个连接表小到足以放入内存,Hive就可以把较小的表放入每个mapper的内存来执行连接操作。在Hive中需要使用注释来指定map连接:
select /*+ MAPJOIN(things) */ sales.*,things.*
from sales join things on (sales.id=things.id);
子查询:
Hive只支持子查询出现在From子句中
select col1,col2
from (select * from records where ... ) a
ADD FILE /test.py;
SELECT TRANSFORM(COL1,COL2,COL3)
FROM TABLE
USING 'test.py'
AS COL1,COL2;
--TRANSFORM(COL1,COL2,COL3) 表示查询本身将把col1,col2,col3作为以制表符分割的行的形式流式传递给脚本test.py
FROM (
FROM table
MAP col1,col2,col3
USING 'testmap.py'
AS col1,col2) map_output
REDUCE col1,col2
USING 'testreduce.py' AS col1,col2;