创建表:
hive>create table tablename(id int,name string,password string);
创建一个名字为tablename的表,表的属性有int id; string name; string password;
创建表时指定分隔符
hive> create table test1(name string,count int)row format delimited fields terminated by ‘/t’;
加载表
hive> load data inpath ‘/user/hadoop/output7/part-r-00000’ into table test1;
创建一个新表,结构与其他一样
hive> create table table1 like table2;
创建一个表table1,表结构跟table2一样;
创建分区表
hive> create table table1(id int,line string) partitioned by (dt string,country string);
显示表里有多少条记录(count 数大于50的有多少条记录)
hive>select count(*) from tablename where count>50;
排序用法order by (查询count 数大于50并排序)
select * from test2 where count > 50 order by count;
显示表中有多少分区
hive> show partitions table1;
显示所有表
hive> show tables;
显示所有与u开头的表
hive> show tables ‘u*’;
显示表的结构信息
hive> describe test1;
修改表名字
hive> alter table table1 rename to test3;
在原表上新添加一列
hive> alter table test1 add columns(new_col2 int comment ‘a commment’);
hive> alter table test1 add columns(new_col3 int);
删除表
hive> drop table test3;
从本地文件加载数据:
hive> LOAD DATA LOCAL INPATH ‘/home/hadoop/input/ncdc/micro-tab/sample.txt’ OVERWRITE INTO TABLE records;
加载分区表
hive> load data inpath ‘/user/hive/warehouse/clickstream_log/dt=2016-11-29/part-r-00000’ overwrite into table clickstream_log PARTITION(dt = ‘2016-11-30’);
显示所有函数
hive> show functions;
查看函数的用法
hive> describe function substr;
查看数组、map、结构
hive> select col1[0],col2[‘b’],col3.c from complex;
查看数组、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;