您的位置:首页 > 其它

Hive基本命令整理

2013-08-20 10:03 357 查看
创建表:

hive> CREATE TABLE pokes (foo INT, barSTRING);

Creates a table called pokes with two columns, the first being aninteger and the other a string

创建一个新表,结构与其他一样

hive> create table new_table like records;

已有表上新增一列

ALTER TABLE pokes ADD COLUMNS (new_col INT);
创建分区表:

hive> create table logs(ts bigint,line string)partitioned by (dt String,country String);

加载分区表数据:

hive> load data local inpath '/home/Hadoop/input/hive/partitions/file1'into table logs partition (dt='2001-01-01',country='GB');

展示表中有多少分区:

hive> show partitions logs;

展示所有表:

hive> SHOW TABLES;

lists all the tables

hive> SHOW TABLES '.*s';

lists all the table that end with 's'. The pattern matchingfollows Java regular

expressions. Check out this link for documentationhttp://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html

显示表的结构信息

hive> DESCRIBE invites;

shows the list of columns

更新表的名称:

hive> ALTER TABLE source RENAME TO target;

添加新一列

hive> ALTER TABLE invites ADD COLUMNS (new_col2 INTCOMMENT 'a comment');

删除表:

hive> DROP TABLE records;

删除表中数据,但要保持表的结构定义

hive> dfs -rmr /user/hive/warehouse/records;

从本地文件加载数据:

hive> LOAD DATA LOCAL INPATH '/home/Hadoop/input/ncdc/micro-tab/sample.txt'OVERWRITE INTO TABLE records;

显示所有函数:

hive> show functions;

查看函数用法:

hive> describe function substr;

查看数组、map、结构

hive> select col1[0],col2['b'],col3.c fromcomplex;

内连接:

hive> SELECT sales.*, things.* FROM sales JOINthings ON (sales.id = things.id);

查看hive为某个查询使用多少个MapReduce作业

hive> Explain SELECT sales.*, things.* FROM salesJOIN things ON (sales.id = things.id);

外连接:

hive> SELECT sales.*, things.* FROM sales LEFT OUTERJOIN things ON (sales.id = things.id);

hive> SELECT sales.*, things.* FROM sales RIGHTOUTER JOIN things ON (sales.id = things.id);

hive> SELECT sales.*, things.* FROM sales FULL OUTERJOIN things ON (sales.id = things.id);

in查询:Hive不支持,但可以使用LEFT SEMI JOIN

hive> SELECT * FROM things LEFT SEMI JOIN sales ON(sales.id = things.id);

Map连接:Hive可以把较小的表放入每个Mapper的内存来执行连接操作

hive> SELECT sales.*, things.* FROM sales JOINthings ON (sales.id = things.id);

INSERT OVERWRITE TABLE ..SELECT:新表预先存在

hive> FROM records2

> INSERT OVERWRITE TABLE stations_by_year SELECTyear, COUNT(DISTINCT station) GROUP BY year

> INSERT OVERWRITE TABLE records_by_year SELECTyear, COUNT(1) GROUP BY year

> INSERT OVERWRITE TABLE good_records_by_year SELECTyear, COUNT(1) WHERE temperature != 9999 AND (quality = 0 ORquality = 1 OR quality = 4 OR quality = 5 OR quality = 9) GROUP BYyear;

CREATE TABLE ... AS SELECT:新表表预先不存在

hive>CREATE TABLE target AS SELECT col1,col2 FROMsource;

创建视图:

hive> CREATE VIEW valid_records AS SELECT * FROMrecords2 WHERE temperature !=9999;

查看视图详细信息:

hive> DESCRIBE EXTENDED valid_records;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: