hive建表以及测试

内部表建表(manaaged_table)

内部表定义

1、由create table命令直接创建,没有加入关键字的表
2、内部表默认存储/user/hive/warehouse下,也可以由location指定、
3、drop表时会将表数据与元数据同时删除
根据以上两种建表方式,引出,hive常用的三种建表的方法

内部表常规建表

根据需求,分别列出表中的列,创建表

应用场景

场景:在库中没有相关的表,根据要处理的数据信息,创建原始表

建表HQL命令
create table if not exists db_hive_test.testTable01(
ip string comment 'remote ip address',
user string comment 'customer name',
request_url string,
city string 
)
comment 'create table test ',
row format delimited fields terminated by '\t' lines terminated by '\n';
效果截图

这里写图片描述
如图,加载数据到表中,

load data local inpath '/../../.' overwrite into table testTable01; 

如图,创建一个常规表
这里写图片描述

复制库中表结构创建新表

适应场景

需要创建一个跟已存在数据库中相同,结构相同的表,及,复制已存在表的结构、

HQL 命令
create table if  not exists Newtable_name like db_name.Oldtable_name;  
效果截图

这里写图片描述

如图,复制已存在的testTable01表结构,创建表testTable02
这里写图片描述
通过查询命令查看表信息

describe  formatted table_name;

这里写图片描述
可以看到,新建的表testTable02的表结构与表testTable01相同

根据查询信息建表

查询数据中的信息,将查询结果信息,保存成一个表

应用场景

为了保存处理数据的结果信息,需要将处理的结果保存,因此需要创建表来存储结果信息

HQL 命令
create table if not exists Newtable_name as select col_name1,col_name2  from Oldtable_name
效果截图

这里写图片描述
如图所示,查询testTable01表的信息后创建一个新的表testTable03
这里写图片描述
如图所示,testTable03表中的数据就是查询testTable01表中的信息

外部表(extenal_table)

外部表介绍

1、创建表是,建表命令增加 ext ernal 关键字
2、drop命令只会删除数据表的元数据,不会删除表的数据文件

建表sql

外部表与内部表的建表hql语句都相同,唯一的区别就是外部表有关键字

create  external table if not exists db_hive_test.testTable01(
ip string comment 'remote ip address',
user string comment 'customer name',
request_url string,
city string 
)
comment 'create table test ',
row format delimited fields terminated by '\t' lines terminated by '\n';
内部表外部表的区别

内部表和外部表的区别
1、建表语句的差异,外部表需要增加关键字,内部表不需要
2、内部表drop表时,会将表数据,与元数据同时删除,而外部表则不会删除元数据,只会删除表数据

效果截图

创建外部表
这里写图片描述
加载外部表数据
这里写图片描述
查询元数据,以及表信息
这里写图片描述
testTable02表数据
这里写图片描述
externalTable表数据
这里写图片描述
mysql数据库mata store库中的信息
这里写图片描述
drop table

drop table testTable02;
drop table externalTable04;

这里写图片描述
这里写图片描述
drop后通过ui查看,可以发现,内部表testTable02被删除了,外部表externalTable的元数据还在
这里写图片描述
drop后mysql查询
这里写图片描述
明显可以看到数据库中的表数据全部被删除

分区表建表

分区表:实际就是对应一个HDFS文件系统上的独立文件夹,该文件下是该分区所有的数据文件。
hive中分区就是分目录,把一个打的数据集根据业务需求分割成更小de数据集。

建表HQ命令

     create table if not exists db_hive_test.dept_partition05(
     loc loc_type,loc loc_type 
  )
  partitioned by(month string)
  row format delimited fields terminated by '\t';

这里写图片描述
加载数据

load data local inpath ’/opt/modules/datafiles/testData.txt‘ into table db_hive_test.dept_partition partitioner (month ='201603');

这里写图片描述
查看分区通过UI界面我们可以看到,数据表已经完成分区
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值