天天看點

Hive建立表文法

内部表

-- 建立内部表
create table user1(
id int comment '使用者id',
username varchar(20) comment '賬戶名稱',
password varchar(255) comment '賬戶密碼',
realname varchar(20) comment '真實姓名',
age int comment '年齡',
address varchar(255) comment '住址',
create_time string comment '建立日期'
)
row format delimited 
fields terminated by '\t'
lines terminated by '\n'
-- collection items terminated by '\001'
-- map keys terminated by '\002'
stored as avro
location '/hive/warehouse/testing.db/user1';           

外部表

-- 建立外部表
-- 删除外部表對應hdfs源資料: hadoop fs -rm -r /hive/warehouse/testing.db/user1
create external  table user1(
id int comment '使用者id',
username varchar(20) comment '賬戶名稱',
password varchar(255) comment '賬戶密碼',
realname varchar(20) comment '真實姓名',
age int comment '年齡',
address varchar(255) comment '住址',
create_time string comment '建立日期'
)
row format delimited 
fields terminated by '\t'
lines terminated by '\n'
-- collection items terminated by '\001'
-- map keys terminated by '\002'
stored as avro
location '/hive/warehouse/testing.db/user1';           

臨時表

-- 建立臨時表
create temporary table user1(
id int comment '使用者id',
username varchar(20) comment '賬戶名稱',
password varchar(255) comment '賬戶密碼',
realname varchar(20) comment '真實姓名',
age int comment '年齡',
address varchar(255) comment '住址',
create_time string comment '建立日期'
)
row format delimited 
fields terminated by '\t'
lines terminated by '\n'
-- collection items terminated by '\001'
-- map keys terminated by '\002'
stored as avro
location '/hive/warehouse/testing.db/user1';           

指令行顯示目前資料庫(目前有效)

set hive.cli.print.current.db=true;           

使用場景

外部表:重要資料,高容錯,資料共享(删除表不删除實體資料)

内部表:資料清洗轉換的中間結果(删除表時中繼資料和實體資料都删除)

臨時表:測試使用,目前session有效,退出自動删除(不支援分區和索引)

注意事項

繼續閱讀