--创建内表
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完成,那么你应该创建表,否则使用外部表!

有一个命令:

时光如水,悄然而逝。

hadoop-hive的内表和外表的更多相关文章

  1. Hive 7、Hive 的内表、外表、分区(22)

    Hive 7.Hive 的内表.外表.分区   1.Hive的内表 Hive 的内表,就是正常创建的表,在 http://www.cnblogs.com/raphael5200/p/5208437.h ...

  2. Hive的内表和外表以及分区表

    1. 内表和外表的区别 内表和外表之间是通过关键字EXTERNAL来区分.删除表时: 内表:在删除时,既删除内表的元数据,也删除内表的数据 外表:删除时,仅仅删除外表的元数据. CREATE [EXT ...

  3. Hive 7、Hive 的内表、外表、分区

    1.Hive的内表 Hive 的内表,就是正常创建的表,在 http://www.cnblogs.com/raphael5200/p/5208437.html 中已经提到: 2.Hive的外表 创建H ...

  4. Hive内表和外表的区别

    本文以例子的形式介绍一下Hive内表和外表的区别.例子共有4个:不带分区的内表.带分区的内表.不带分区的外表.带分区的外表. 1 不带分区的内表 #创建表 create table innerTabl ...

  5. hive内表和外表的创建、载入数据、区别

    创建表 创建内表 create table customer( customerId int, firstName string, lastName STRING, birstDay timestam ...

  6. hadoop Hive 的建表 和导入导出及索引视图

       1.hive 的导入导出 1.1 hive的常见数据导入方法 1.1.1 从本地系统中导入数据到hive表 1.创建student表 [ROW FORMAT DELIMITED]关键字,是用来设 ...

  7. hadoop系列 第二坑: hive hbase关联表问题

    关键词: hive创建表卡住了 创建hive和hbase关联表卡住了 其实针对这一问题在info级别的日志下是看出哪里有问题的(为什么只能在debug下才能看见呢,不太理解开发者的想法). 以调试模式 ...

  8. kylin加载hive表错误:ERROR [http-bio-7070-exec-10] controller.TableController:189 : org/apache/hadoop/hive/conf/HiveConf java.lang.NoClassDefFoundError: org/apache/hadoop/hive/conf/HiveConf 解决办法

    一.问题背景 在kylin中加载hive表时,弹出提示框,内容是“oops!org/apache/hadoop/hive/conf/HiveConf”,无法加载hive表,查找kylin的日志时发现, ...

  9. hive中删除表的错误Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException

    hive使用drop table 表名删除表时报错,return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. MetaException 刚 ...

随机推荐

  1. Python - 默认参数传参陷阱

    def extend_list(v, li=[]): li.append(v) return li list1 = extend_list(10) print(list1) # [10] list2 ...

  2. Ubuntu16.04中VirtualBox中安装FreeBSD

    获取镜像 FreeBSD官网:https://www.freebsd.org,打开后即可看到“Download Freebsd”的按钮,点击进去 进入版本选择的页面.可以看到当前RELEASR版本,旧 ...

  3. python matlab 带包实现全排列

    >> A=[2,5,7];perms(A) ans = 7 5 2 7 2 5 5 7 2 5 2 7 2 7 5 2 5 7 >> perms(1:4) ans = 4 3 ...

  4. 第一个React Native项目

    1>下图操作创建第一个React Native项目: 用Xcode运行界面如下: 记住: 在使用项目文件期间,终端记住不要关闭的哟!!! 改变了程序代码,需要刷新运行,使用快捷键: Comman ...

  5. Spring boot Gradle项目搭建

    Spring boot Gradle项目搭建 使用IDEA创建Gradle工程     操作大致为:File->new->Project->Gradle(在左侧选项栏中)     创 ...

  6. 《精通并发与Netty》学习笔记(02 - 服务端程序编写)

    上节我们介绍了开发netty项目所必需的开发环境及工具的使用,这节我们来写第一个netty项目 开发步骤 第一步:打开https://search.maven.org 找到netty依赖库 第二步:打 ...

  7. 【并行计算-CUDA开发】CUDA存储器模型

    CUDA存储器模型 除了执行模型以外,CUDA也规定了存储器模型(如图2所示)和一系列用于主控CPU与GPU间通信的不同地址空间.图中红色的区域表示GPU片内的高速存储器,橙色区域表示DRAM中的的地 ...

  8. nginx 代理 registry docker certificate is valid for k8s, not

    1.配置SSL证书及nginx反向代理docker registry 搭建私有CA,初始化CA环境,在/etc/pki/CA/下建立证书索引数据库文件index.txt和序列号文件serial,并为证 ...

  9. [转帖]PostgreSQL的用户、角色和权限管理

    PostgreSQL的用户.角色和权限管理 2018年05月18日 14:02:29 jerry-89 阅读数 6143 https://blog.csdn.net/eagle89/article/d ...

  10. # Clion中编译多个cpp(实现单文件编译)

    Clion中编译多个cpp(实现单文件编译) 在不做任何配置情况下,Clion工程下只能有一个main()函数,新建多个cpp会导致报main()函数重复定义的错误,所以默认情况下无法在一个工程下编译 ...