--创建内表
create table if not exists employee(
id int comment 'empoyeeid',
dateincompany string comment 'data come in company',
money float comment 'work money',
mapdatamap array<string>,
arraydata  array<int>,
structordata struct<col1:string,col2:string>)
partitioned by (century string comment 'centruycome in company',
year string comment 'come in comany year')
row format delimited fields terminated by ','
collection items terminated by '@'
map keys terminated by '$'
stored as textfile;

数据文件:

[hadoop@master hivetest]$ more employee.txt
1,huawei,1000.2,m1$a,1@2@3@4,c1@c2

装载数据:

hive>LOAD DATA LOCAL INPATH '/home/hadoop/tmp/hivetest/employee.txt'  INTO TABLE employee PARTITION(century='zhengzhou', year='20180910');

查询数据:

hive> select * from employee;
OK
1       huawei  1000.2  ["m1$a"]        [1,2,3,4]       {"col1":"c1","col2":"c2"}       zhengzhou       20180910
Time taken: 0.638 seconds, Fetched: 1 row(s)

给出的现象是:

在HDFS上,默认的路径下/user/hive/warehouse/employee生成一个目录,在前台界面,也是一个目录。

删除内表,目录都不存在了。

还可以指定目录(HDFS上)

create  table if not exists test1(id int, name string)
row format delimited fields terminated by ',' stored as textfile  location '/tmp/data';

> load data inpath '/tmp/test1.txt' into table test1 ;

> select * from test1;
OK
1       zhangwei
Time taken: 0.593 seconds, Fetched: 1 row(s)

/tmp/test1.txt在加载的时候,删除掉了,数据加载到表里,其实就是落成文件到/tmp/data/test1.txt

删除表后,数据都删除了,data目录都删除了。

--创建外表

create external table if not exists test2 (id int,name string)
row format delimited fields terminated by ',' stored as textfile;

会在/user/hive/warehouse/新建一个表目录test2
hive> load data inpath '/tmp/test1.txt' into table test2 ;
Loading data to table default.test2
OK
Time taken: 1.053 seconds
hive> select * from test2;
OK
1       zhangwei
Time taken: 0.281 seconds, Fetched: 1 row(s)

现象: 在load的那一步,会把/tmp/test1.txt文件移动到/user/hive/warehouse/test2这个目录下。

数据的位置发生了变化!

本质是load一个hdfs上的数据时会转移数据!删除表后,数据文件在留在HDFS上。

 
最后归纳一下Hive中表与外部表的区别:
     1、在导入数据到外部表,数据并没有移动到自己的数据仓库目录下(如果指定了location的话),也就是说外部表中的数据并不是由它自己来管理的!而内部表则不一样;
     2、在删除内部表的时候,Hive将会把属于表的元数据和数据全部删掉;而删除外部表的时候,Hive仅仅删除外部表的元数据,数据是不会删除的!

3. 在创建内部表或外部表时加上location 的效果是一样的,只不过表目录的位置不同而已,加上partition用法也一样,只不过表目录下会有分区目录而已,load data local inpath直接把本地文件系统的数据上传到hdfs上,有location上传到location指定的位置上,没有的话上传到hive默认配置的数据仓库中。

外部表相对来说更加安全些,数据组织也更加灵活,方便共享源数据。

那么,应该如何选择使用哪种表呢?在大多数情况没有太多的区别,因此选择只是个人喜好的问题。但是作为一个经验,如果所有处理都需要由Hive完成,那么你应该创建表,否则使用外部表!

有一个命令:

时光如水,悄然而逝。

最新文章

  1. react native 环境配置
  2. tcpdump捕捉样例
  3. Java实时读取日志文件
  4. js数组去重方法
  5. ZOJ 2182 Cable TV Network(无向图点割-最大流)
  6. 机器学习总结之逻辑回归Logistic Regression
  7. 几个MVC属性
  8. java实现——005从尾到头打印链表
  9. Java继承时的初始化顺序
  10. Python面向对象——多态
  11. CentOS5.x、CentOS6.x 使用NFS及mount实现两台服务器间目录共享
  12. 第 7 章 多主机管理 - 046 - 创建 Machine
  13. Dubbo(6)Dubbo服务集群实现负载均衡
  14. c#public、private、protected、internal、protected internal
  15. CronExpression
  16. [leetcode]721. Accounts Merge账户合并
  17. 微信小游戏 小程序与小游戏获取用户信息接口调整 wx.createUserInfoButton
  18. Nginx+SpringBoot搭建负载均衡
  19. inviteflood 洪泛滥工具
  20. 一款基于jQuery外观优雅带遮罩弹出层对话框

热门文章

  1. [ZT]Enhancement-01
  2. iOS开发嵌套ReactNative页面
  3. linux编译gpu_flow
  4. 【HANA系列】SAP HANA SQL IFNULL和NULLIF用法与区别
  5. DEVOPS ROADMAP
  6. SecureCRT 8.3注册码下载
  7. 【机器学习】Matlab中实现QQ-plot的一个好工具gqqplot
  8. SSDsim
  9. OpenCV_复制一个或多个ROI图像区域
  10. 2019icpc西安邀请赛 J And And And (树形dp)