文章目錄
基本操作
建表
show databases;
show tables;
create database mydb;
create table if not exists
mydb.employee(
eid int,
name String,
salary String,
destination String)
COMMENT 'Employee details'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n' STORED AS TEXTFILE;
描述表,查看schema
DESCRIBE mydb.employee;
從文件導入數據
load data local inpath '/root/text.csv' into table employee;
hive 中執行 shell 命令
!clear; //hive中執行shell命令, 清空屏幕
hive 中執行 hdfs 命令
dfs -ls -R /; //hive中執行hdfs命令
拷貝表
create table mydb.employee2 as select * from myhive.employee; // 複製表
內部表和外部表
內部表
託管表(內部表),hive默認創建的表都是託管表,hive控制其數據的生命週期。刪除託管表時,元數據和數據均被刪除。
create table if not exists
mydb.employee_inner(
eid int,
name String,
salary String,
destination String)
COMMENT 'Employee details'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
drop table mydb.employee_inner;
外部表
hive 控制元數據,刪除外部表時,數據不被刪除
create external table if not exists
myhive.employee_outer(
eid int,
name String,
salary String,
destination String)
COMMENT 'Employee details'
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
分區表
建表
partitioned by
create table if not exists
test2(
eid int,
name String,
salary String,
destination String)
partitioned by(country string,state string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
描述分區表
載入數據到指定分區
load data local inpath '/root/text.csv'
into table mydb.test2
partition(country='china',state='shanxi');
查詢數據
select * from mydb.test2 where country='china' and state='shanxi';
查看 hdfs
分區表的查詢模式: strict/nostrict
默認是非嚴格,嚴格模式要求查詢的時候必須帶分區
set hive.mapred.mode=strict
查看有哪些分區
show partitions mydb.test2;
show partitions mydb.test2 partition(state='shanxi');
增加分區
注意不能增加不存在的分區列
alter table mydb.test2 add partition(country='china',state='henan');
alter table mydb.test2 add
partition(country='china',state='hubei')
partition(country='china',state='hebei')
partition(country='china',state='shandong');
修改表名
alter table mydb.test2 rename to mydb.test;
移動分區的存儲位置
alter table mydb.test partition(country='china',state='hubei')
set location '/user/hive/warehouse/myhive.db/test/country=china/state=hubei1';
驗證一下:
load data local inpath '/root/text.csv'
into table mydb.test
partition(country='china',state='shanxi');
dfs -ls /user/hive/warehouse/mydb.db/test/country=china/state=hubei;
dfs -ls /user/hive/warehouse/myhive.db/test/country=china;
複製表
create table if not exists
mydb.test3(
eid int,
name String,
salary String,
destination String)
partitioned by(country string,state string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
STORED AS TEXTFILE;
insert into mydb.test3
partition(country='china',state='hubei')
select eid,name,salary,destination
from mydb.test
where country='china' and state='henan';