第2节 hive基本操作:纯命令行

vim /etc/profile
export HIVE_HOME=/export/servers/hive...
export PATH=:$HIVE_HOME/bin:$PATH

前台启动hive:
hive --service hiveserver2
客户端连接:
beeline
!connect jdbc:hive2://node03:10000

create database [if not exists] myhive location '/myhive2';
use myhive;
desc database myhive2;
desc database extended myhive2;
drop database myhive2 [cascade];

create table [if not exists] stu (sid string,sname string);

create table stu2 as select * from stu;
create table stu3 like stu;
desc [formatted] stu2;

create external table [if not exists] student (sid string,sname string,ssex string,sbirth string) partitioned by (year string,month string,day string) row format delimited fields terminated by '\t' stored as textfile location '/user/stu';

load data local inpath '/export/servers/hivedatas/score.csv' [overwrite] into table score;

load data inpath '/hivedatas/score.csv' into table score partition(year='2018',month='06',day='18');

insert overwrite table score5 partition(month='201801') select sid,sname,sscore from score; (注意不能使用select * from score,否则报错:Error: Error while compiling statement: FAILED: SemanticException [Error 10044]: Line 1:23 Cannot insert into target table because column number/types are different ''201902'': Table insclause-0 has 3 columns, but query has 4 columns. (state=42000,code=10044))

select * from score where month='201806'
union all
select * from score where month='201807';

show partitions score;

alter table score add partition(month='201808') partition(month='201809') ;

alter table score drop partition(month='201809');

msck repair table score4;
==============================================
truncate table score6;(只能清空管理表)

from score
insert overwrite table score_fir partition(month='201806') select sid,cid
insert overwrite table score_sec partition(month='201806') select cid,sscore;

小结:create as select , insert select。

SELECT [ALL | DISTINCT] select_expr, select_expr, ...

FROM table_reference

[WHERE where_condition]

[GROUP BY col_list [HAVING condition]]

[CLUSTER BY col_list

  | [DISTRIBUTE BY col_list] [SORT BY| ORDER BY col_list]

]

[LIMIT number]

select sid as myid, cid myid2 from score;

select count(distinct sid) from score;

select max(sscore),min(sscore),sum(sscore),avg(sscore) from score;

select * from score where sscore like '_9%';

select * from score where sscore rlike '9';

select sid,avg(sscore) from score group by sid;

select s_id ,avg(s_score) avgscore from score group by s_id having avgscore > 85;

select s.sid,s.sscore,stu.sname,stu.sbirth from score s left join student stu on s.sid = stu.sid;

select * from student s left | right | full [outer] join score b on s.sid = b.sid; 左右全 外连接

select sid, avg(sscore) avgScore from score group by sid order by avgScore desc; 按照别名排序

SELECT s.s_id,s.s_score,stu.s_name,stu.s_birth  FROM score s LEFT JOIN student stu ON s.s_id = stu.s_id

猜你喜欢

转载自www.cnblogs.com/mediocreWorld/p/11061858.html