Hive内部表,外部表,分区表的创建

--------创建内部表------------
默认存储在/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

转载于:https://www.cnblogs.com/Thomas-blog/p/9728176.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值