bin/ schematool -dbType mysql -initSchema
systemctl stop firewalld.service
hadoop fs -chmod 777 /tmp/hadoop-yarn
more ~/.hivehistory
SELECT *
FROM t2_bak;
load data local inpath '/data/soft/hivedata/t2.data' into table t2_bak;
select * from t2_bak;
alter table t2_bak add columns (name string);
create table t2(age int comment '年龄')comment '测试';
show create table t2;
desc t2;
mysql -uroot -pAdmin@123
create table t2(
age int comment '年龄'
)comment '测试'
create table t3(
id int comment 'ID',
stu_name string comment 'name',
stu_birthday date comment 'birthday',
online boolean comment 'is online'
);
create table stu(
id int,
name string,
favors array<string>
)row format delimited
fields terminated by '\t'
collection items terminated by ','
lines terminated by '\n';
create table stu2(
id int,
name string,
scores map<string,int>
)row format delimited
fields terminated by '\t'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n';
select id,name,scores['chinese'],scores['math'] from stu2;
select id,name,scores['chinese'] as chinese,scores['math'] as math,scores['english'] as english from stu2;
create table stu3(
id int,
name string,
address struct<home_addr:string,office_addr:string>
)row format delimited
fields terminated by '\t'
collection items terminated by ','
lines terminated by '\n';
create table student(
id int comment 'id',
name string comment 'name',
favors array<string>,
scores map<string,int>,
address struct<home_addr:string,office_addr:string>
)row format delimited
fields terminated by '\t'
collection items terminated by ','
map keys terminated by ':'
lines terminated by '\n';
create external table external_table(
key string
)location '/data/external';
create table partition_1 (
id int,
name string
) partitioned by (dt string)
row format delimited
fields terminated by '\t';
load data local inpath '/data/soft/hivedata/partition_1.data' overwrite into table partition_1 partition(dt='2021-10-27');
alter table partition_1 add partition (dt='2021-89-56')
show partitions partition_1;
alter table partition_1 drop partition(dt='2020-01-02');
alter table partition_1 drop partition(dt='2021-10-27');
create table partition_2(
id int,
name string
)partitioned by (year int,school string)
row format delimited
fields terminated by '\t';
load data local inpath '/data/soft/hivedata/partition_2.data'
into table partition_2 partition (year='2021',school='english');
select * from partition_2;
select * from partition_2 where year=2020;
select * from partition_2 where year=2020 and school='xk';
create external table ex_par(
id int,
name string
)partitioned by (dt string)
row format delimited
fields terminated by '\t'
location '/data/ex_par';
load data local inpath '/data/soft/hivedata/ex_par.data' into table ex_par partition (dt='2020-12-12');
alter table ex_par drop partition(dt='2020-12-12');
alter table ex_par add partition(dt='2020-12-12') location '/data/ex_par/dt=2020-12-12';
create table bucket_tb(
id int
)clustered by (id) into 4 buckets;
insert into table … select … from …;
set hive.enforce.bucketing =true
create table b_source(id int);
insert into table bucket_tb select id from b_source;
insert into table bucket_tb select id from b_source where id =1;
create view v1 as select t3_new.id,t3_new.stu_name from t3_new;
create external table ex_par_more_type(
log string
)partitioned by(dt string,d_type string)
row format delimited
fields terminated by '\t'
location '/moreType';
alter table ex_par_more_type add partition(dt='20211022',d_type='giftRecord') location '/moreType/20211022/giftRecord';
alter table ex_par_more_type add partition(dt='20211022',d_type='userInfo') location '/moreType/20211022/userInfo';
alter table ex_par_more_type add partition(dt='20211022',d_type='videoInfo') location '/moreType/20211022/videoInfo';
select * from ex_par_more_type where dt='20211022' and d_type='giftRecord';
select * from ex_par_more_type where dt='20211022' and d_type='userInfo';
select * from ex_par_more_type where dt='20211022' and d_type='giftRecord';
create view gift_record_view as select get_json_object(log,'$.send_id') send_id,
get_json_object(log,'$video_id') as video_id,get_json_object(log,'$.gold') as gold,
dt from ex_par_more_type where d_type='giftRecord';
create view user_info_view as select get_json_object(log,'$.uid') as uid,
get_json_object(log,'$.nickname') as nickname,get_json_object(log,'$.usign') as usign,
get_json_object(log,'$.sex') as sex,
dt from ex_par_more_type where d_type='userInfo';
create view video_info_view as select get_json_object(log,'$.id') as id,
get_json_object(log,'$.uid') as uid,
get_json_object(log,'$.lat') as lat,
get_json_object(log,'$.lnt') as lnt,
dt from ex_par_more_type where d_type = 'videoInfo';
select * from user_info_view where dt='20211022';
select * from gift_record_view where dt='20211022';
select * from video_info_view where dt='20211022';
if [ "a$1" = "a" ]
then
dt=`date +%Y%m%d`
else
dt=$1
fi
hive -e "
alter table ex_par_more_type add if not exists partition(dt='${dt}',d_type='giftRecord') location '/moreType/${dt}/giftRecord';
alter table ex_par_more_type add if not exists partition(dt='${dt}',d_type='userInfo') location '/moreType/${dt}/userInfo';
alter table ex_par_more_type add if not exists partition(dt='${dt}',d_type='videoInfo') location '/moreType/${dt}/videoInfo';
"
show partitions ex_par_more_type;
alter table ex_par_more_type drop partition(dt='20211022',d_type='giftRecord');
alter table ex_par_more_type drop partition(dt='20211022',d_type='userInfo');
alter table ex_par_more_type drop partition(dt='20211022',d_type='videoInfo');
alter table ex_par_more_type add if not exists partition(dt='20211022',d_type='giftRecord') location '/moreType/${dt}/giftRecord';
00 01 * * * root /bin/bash /data/soft/hivedata/addPartition.sh >> /data/soft/hivedata/addPartition.log
create external table student_score(
id int,
name string,
sub string,
scores int
)row format delimited
fields terminated by '\t'
location '/data/student_score';
row_number,row_number
不能单独使用,在这里需要加上over()
select *,row_number() over() from student_score;
select *,row_number() over (partition by sub order by scores desc) as num from student_score;
select * from (select *,row_number() over (partition by sub order by scores desc) as num from student_score)
s WHERE s.num<=3;
select *,rank() over (partition by sub order by scores desc) as num from student_score;
select *,dense_rank() over (partition by sub order by scores desc) as num from student_score;
create external table student_favors(
name string,
favor string
)row format delimited
fields terminated by '\t'
location '/data/student_favors';
select * FROM student_favors;
SELECT name,collect_list(favor) as favor_list FROM student_favors GROUP BY name;
SELECT name,concat_ws(',',collect_list(favor)) as favor_list FROM student_favors GROUP BY name;
SELECT name,concat_ws(',',collect_set(favor)) as favor_list FROM student_favors GROUP BY name;
create external table student_favors_2(
name string,
favorlist string
)row format delimited
fields terminated by '\t'
location '/data/student_favors_2';
select split(favorlist,',') FROM student_favors_2;
select explode(split(favorlist,',')) from student_favors_2;
select name,favor_new from student_favors_2 lateral view
explode(split(favorlist, ',')) table1 as favor_new ;
select id,from t2_bak sort by id;
set mapreduce.job.reduces = 2;
select id from t2_bak distribute by id sort by id;