1. 程式人生 > >常用HSQL

常用HSQL

建立內部表通過,分割 create table t13(id int,name string,subject string) row format delimited fields terminated by ‘,’;

LOAD DATA LOCAL INPATH ‘/root/t13.txt’ into table t13; – collect_set 和 炸裂explode演示 select explode(a_subject) as sub from (select id,name,collect_set(subject) as a_subject from t13 group by id,name) o1;

select id,if(age>25,‘青年’,‘少年’) from t_user; select moive_name,if(array_contains(actors,‘吳剛’),‘好電影’,‘爛片兒’) from t_movie;

select from_unixtime(unix_timestamp(),“yyyy/MM/dd HH:mm:ss”);

select greatest(1,99) ;

select concat(“A”,“B”); select concat_ws("*", “A”,“B”);

號需要轉義 select split(concat_ws("", “A”,“B”), “\*”);

create table t_ip(dt string,ip string,url string) row format delimited fields terminated by ‘,’; –注意into和overwrite的區別 load data local inpath ‘/root/t_ip.txt’ into table t_ip; load data local inpath ‘/root/t_ip.txt’ overwrite into table t_ip; load data local inpath ‘/root/t10.txt’ overwrite into table t10; load data local inpath ‘/root/t_s.txt’ overwrite into table t_s;

create table t_person_struct(id int,name string,info structage:int,sex:string,addr:string) row format delimited fields terminated by ‘,’ collection items terminated by ‘:’;

select id,name,brother from (select id,name,family_members[‘brother’] as brother from t_person) tmp where brother is not null;

select * from t_person where array_contains(map_keys(family_members),‘brother’);

–arryays建立陣列

create table t_movie(moive_name string,actors array,first_show date) row format delimited fields terminated by ‘,’ collection items terminated by ‘:’;

load data local inpath ‘/root/t_moive.txt’ into table t_movie; load data local inpath ‘/root/t_person.txt’ into table t_person; load data local inpath ‘/root/t_strut.txt’ into table t_person_struct; load data local inpath ‘/root/t_ip.txt’ into table t_ip; load data local inpath ‘/root/sale.txt’ into table t_sale;

select moive_name,actors from t_movie where array_contains(actors,‘吳剛’);

select * from a order by id desc limit 2; select * from t_p where is_married;

create table t_customer(id int,name string,birthday date) row format delimited fields terminated by ‘,’;

load data local inpath ‘/root/customer.dat’ into table t_customer; load data local inpath ‘/root/t_user.txt’ into table t_user; load data local inpath ‘/root/boolen.txt’ into table t_p;

create table t_p(id int,name string,age int,is_married string) row format delimited fields terminated by ‘,’;

–建立資料庫 create database mytestdb; –展示資料庫 show databases; –切換資料庫 use default; –建立表,每個欄位按照 , 間隔 create table t_1(id string, name string, sex string , age string) row format delimited fields terminated by ‘,’; create table t_2(id string, name string, sex string , age string) row format delimited fields terminated by ‘,’;

–建立外部表 create external table t_3(id string, name string, sex string , age string) row format delimited fields terminated by ‘,’ LOCATION ‘/aa/bb’;

– 更改建立表的預設目錄 create table t_3(id string, name string, sex string , age string) row format delimited fields terminated by ‘,’ LOCATION ‘/xx/oo’;

–檢視建立表的資訊 show create table t_2; desc t_2;

–匯入資料 –方式一: LOAD DATA LOCAL INPATH ‘/root/t_1.txt’ into TABLE t_1;

–方式二:在hdfs上匯入資料表,就是剪下資料 LOAD DATA INPATH ‘/t_1.txt’ into TABLE t_1;

select * from t_1;

–表的欄位型別的修改 alter table t_x change string(欄位名) id int first;

alter table t_p change string is_married boolean;

–新增一個欄位 alter table t_1 add columns (clo1 String);

–表名的修改 alter table t_1 rename to t_x;

–建立分割槽表 CREATE TABLE t_2(id string, name string, sex string , age string) partitioned by (day string,city string) row format delimited fields terminated by ‘,’;

–匯入分割槽資料 LOAD DATA LOCAL INPATH ‘/root/t_1.txt’ into TABLE t_2 PARTITION(day=‘2018-04-15’,city=‘beijing’); select * from t_2 where day=‘2018-04-15’ and city=‘shanghai’;

select upper(“abc”); --做測試 set hive.exec.mode.local.auto=true; --設定本機執行模式

–清空表的資料 truncate table a;

select * from a order by id desc limit 2;