1. 程式人生 > >內表 外表區別 以及靜態分割槽和動態分割槽

內表 外表區別 以及靜態分割槽和動態分割槽

//內表 和外表的區分 刪外表 表下的東西還在 內表相反
CREATE EXTERNAL TABLE CITY_ex(
province_code int,
province_name string,
city_code int,
city_name string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ‘,’
LINES TERMINATED BY ‘\n’
LOCATION ‘/user/ljl/city’;

CREATE EXTERNAL TABLE CITY_in(
province_code int,
province_name string,
city_code int,
city_name string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ‘,’
LINES TERMINATED BY ‘\n’
LOCATION ‘/user/ljl/city’;

//建立一個user表,把’/home/bigdata/tanqi/hive/user.txt’倒到user表裡
CREATE TABLE user(
uid INT,
city_code INT,
model string,
access string
)
row FORMAT delimited
fields terminated by ‘,’
lines terminated by ‘\n’;

load data local inpath ‘/home/bigdata/tanqi/hive/user.txt’ into table user;

//靜態分割槽
CREATE TABLE user_daily(
uid int,
city_code int,
model string,
access string
)
partitioned by (p_date string);

INSERT OVERWRITE TABLE user_daily PARTITION (p_date=’2017-09-13’)
SELECT * FROM user;

//檢視 插入分割槽的情況
show partitions user_daily;

//動態分割槽
set hive.exec.dynamic.partition.mode=nonstrict;
INSERT OVERWRITE TABLE user_daily PARTITION (p_date)
SELECT *,’2017-09-02’ FROM user
UNION ALL
SELECT *,’2017-09-03’ FROM user
;

//分割槽里名字的更改
ALTER TABLE user_daily PARTITION (p_date=’2017-01-01’) RENAME TO PARTITION (p_date=’20170101’);

//查 某個分割槽的內容
select * from user_daily where p_date=’2017-09-02’;