--------創建內部表------------
默認存儲在/user/hive/warehouse下 也可以通過location指定
刪除表時,會刪除表數據及元數據
create table if not exists db_study.student(
id String ,
name String
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\t’ #文件內容格式與表的格式相同,否則導入后表的數據是null
location ‘/user/wei/hive-table/student’;
#加載本地數據到表中
load data local inpath ‘/opt/data/student.table’ into table db_study.student ;
#刪除表,會刪除創建表時指定的目錄,以及目錄下的數據文件
drop table if exists student ;
---------創建外部表-------------
在創建表時必須指定目錄位置(LOCATION)
刪除表時,只刪除元數據不會刪除表數據
create external table if not exists db_study.teacher(
id String,
name String
)
row format delimited fields terminated by ‘\t’
location ‘/user/wei/hive-table/teacher’
#上傳數據文件到localtion目錄下,hive會把所有的文件映射到表中
dfs -put /opt/data/teacher.table /user/wei/hive-table/teacher
dfs -put /opt/data/teacher2.table /user/wei/hive-table/teacher
----------創建分區表-------------
實際上就是對應一個HDFS文件系統上的獨立文件夾,該文件夾下是該分區的所有數據文件
Hive中的分區就是分目錄,把一個大的數據集根據業務需要分割成更多的數據集
查詢時通過where子句中的表達式來選擇查詢所需要的指定分區,這樣的查詢效率會高很多
create table db_study.class_partition(
id int,
name string
)
partitioned by(month string)
row format delimited fields terminated by ‘\t’
location ‘/user/wei/hive-table/class_partition’
加載表數據
方法一 load 加載
load data local inpath ‘/opt/data/class1.table’ into table db_study.class_partition partition(month=‘201809’)
select * from class_partition where month=201809 ;
方法二 insert 加載
insert overwrite table NAME partition(month=‘201707’) select id, name from NAME;
方法三 可通過手動上傳文件到分區目錄,進行加載
hdfs dfs -mkdir /user/hive/warehouse/tb_partition/month=201710
hdfs dfs -put nameinfo.txt /user/hive/warehouse/tb_partition/month=201710
雖然方法三手動上傳文件到分區目錄,但是查詢表的時候是查詢不到數據的,需要更新元數據信息。
更新源數據的兩種方法
方法一:msck repair table 表名
方法二:alter table tb_partition add partition(month=‘201708’);
查詢表數據
select * from default.emp_partition where month = ‘201807’ ;
show partitions tb_partition;
-------從已經存在的表選出字段內容組成新的表,分表抽取-------
create table IF NOT EXISTS test_db.user_tmp
ROW FORMAT DELIMITED FIELDS TERMINATED BY ‘\t’
location ‘/user/wei/oozie/datas/table/user_tmp’
as select username from test_db.hive_user
-------復制表結構-------
create table IF NOT EXITSTS default.teacher03
like default.teacher
-------導出Hive 表的數據---------------
1)導出查詢數據到本地目錄下
insert overwrite local directory ‘/opt/datas/user2.txt’
row format delimited fields terminated by ‘/t’ collection items terminated by ‘\n’
select * from db_wei.user_pt ;
2)導出查詢數據到本地目錄下
bin/hive -e “select * from db_wei.user;” > /opt/datas/user
3)導出查詢數據到HDFS
insert overwrite directory ‘/user/root’
row format delimited fields terminated by ‘/t’ collection items terminated by ‘\n’
select * from db_wei.user_pt ;
-------數據庫的操作-------
create database if not exists
show databases;
show databases like ‘db_hive’;
use db_hive;
desc database db_hive ;
desc database extended db_hive ;
drop database db_hive;
drop database db_hive cascade ; #級聯刪除,刪除數據庫,刪除下面的表
drop database if exists db_hive;
-------表的操作-------
#清除一個表的數據
truncate table tablename ;
#對表進行改名
alter table tablename rename to tablename ;
#刪除表
drop table if exists tablename;
#表詳情
desc FORMATTED tablename