分享

hive基本操作

pig2 2014-2-10 22:06:59 发表于 实操演练 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 22 74112
阅读本文章可以带着下面问题:
1.与传统数据库对比,找出他们的区别
2.熟练写出增删改查(面试必备)


创建表:
hive> CREATE TABLE pokes (foo INT, bar STRING);
        Creates a table called pokes with two columns, the first being an integer and the other a string

创建一个新表,结构与其他一样
hive> create table new_table like records;

创建分区表:
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 matching follows Java regular
expressions. Check out this link for documentation

显示表的结构信息
hive> DESCRIBE invites;
        shows the list of columns

更新表的名称:
hive> ALTER TABLE source RENAME TO target;

添加新一列
hive> ALTER TABLE invites ADD COLUMNS (new_col2 INT COMMENT '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 from complex;


内连接:
hive> SELECT sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);

查看hive为某个查询使用多少个MapReduce作业
hive> Explain SELECT sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);

外连接:
hive> SELECT sales.*, things.* FROM sales LEFT OUTER JOIN things ON (sales.id = things.id);
hive> SELECT sales.*, things.* FROM sales RIGHT OUTER JOIN things ON (sales.id = things.id);
hive> SELECT sales.*, things.* FROM sales FULL OUTER JOIN 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 /*+ MAPJOIN(things) */ sales.*, things.* FROM sales JOIN things ON (sales.id = things.id);

INSERT OVERWRITE TABLE ..SELECT:新表预先存在
hive> FROM records2
    > INSERT OVERWRITE TABLE stations_by_year SELECT year, COUNT(DISTINCT station) GROUP BY year
    > INSERT OVERWRITE TABLE records_by_year SELECT year, COUNT(1) GROUP BY year
    > INSERT OVERWRITE TABLE good_records_by_year SELECT year, COUNT(1) WHERE temperature != 9999 AND (quality = 0 OR quality = 1 OR quality = 4 OR quality = 5 OR quality = 9) GROUP BY year;  

CREATE TABLE ... AS SELECT:新表表预先不存在
hive>CREATE TABLE target AS SELECT col1,col2 FROM source;

创建视图:
hive> CREATE VIEW valid_records AS SELECT * FROM records2 WHERE temperature !=9999;

查看视图详细信息:
hive> DESCRIBE EXTENDED valid_records;

-------------------------------------------------------------------------------------------------------------------------------------

传统数据库:
添加:

insert into 表名 values();
修改:

update 表名 set a=b where b=c;
删除:

delete from 表名where a=b;
查询:

select * from 表名 where a=b;

有兴趣的同学,可以看你能否写出相应的hive操作



来自群组: Hadoop技术组

本帖被以下淘专辑推荐:

已有(22)人评论

跳转到指定楼层
adangdangyun 发表于 2014-7-8 21:50:58
回复

使用道具 举报

break-spark 发表于 2014-10-18 22:30:47
不错,学习了
回复

使用道具 举报

admin 发表于 2014-10-18 23:28:25
adangdangyun 发表于 2014-7-8 21:50
我一般在/tmp 这个目录下执行hive命令(已经设好环境变量),就进入了hive的shell中,但如果 ...
derby只用于学习,存在很多问题,最好使用mysql。就不会存在这个问题了。
回复

使用道具 举报

hery 发表于 2014-11-18 18:00:27
很不错的帖子,最近在学习hive。。很好
回复

使用道具 举报

ainubis 发表于 2015-3-29 13:14:17
好东西,多xie楼主分享
回复

使用道具 举报

tang 发表于 2015-5-13 21:01:17
回复

使用道具 举报

cdb521007 发表于 2015-7-26 22:45:46
回复

使用道具 举报

hello_hadoop 发表于 2015-8-28 10:27:23
总结的不错

回复

使用道具 举报

123下一页
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条