Hive 中内部表与外部表的区别与创建方法

先来说下Hive中内部表与外部表的区别: 
Hive 创建内部表时,会将数据移动到数据仓库指向的路径;若创建外部表,仅记录数据所在的路径, 
不对数据的位置做任何改变。在删除表的时候,内部表的元数据和数据会被一起删除, 
而外部表只删除元数据,不删除数据。这样外部表相对来说更加安全些,数据组织也更加灵活,方便共享源数据。 

需要注意的是传统数据库对表数据验证是 schema on write(写时模式),而 Hive 在load时是不检查数据是否 
符合schema的,hive 遵循的是 schema on read(读时模式),只有在读的时候hive才检查、解析具体的 
数据字段、schema。 
读时模式的优势是load data 非常迅速,因为它不需要读取数据进行解析,仅仅进行文件的复制或者移动。 
写时模式的优势是提升了查询性能,因为预先解析之后可以对列建立索引,并压缩,但这样也会花费要多的加载时间。 

下面来看下 Hive 如何创建内部表: 

1 create table test(userid string);
2 LOAD DATA INPATH '/tmp/result/20121213' INTO TABLE test partition(ptDate='20121213');

这个很简单,不多说了,下面看下外部表: 

01hadoop fs -ls /tmp/result/20121214
02Found 2 items
03 -rw-r--r--   3 june supergroup       1240 2012-12-26 17:15 /tmp/result/20121214/part-00000
04 -rw-r--r--   1 june supergroup       1240 2012-12-26 17:58 /tmp/result/20121214/part-00001
05-- 建表
06 create EXTERNAL table IF NOT EXISTS test (userid string) partitioned by (ptDate string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
07-- 建立分区表,利用分区表的特性加载多个目录下的文件,并且分区字段可以作为where条件,更为重要的是
08-- 这种加载数据的方式是不会移动数据文件的,这点和 load data 不同,后者会移动数据文件至数据仓库目录。
09 alter table test add partition (ptDate='20121214') location '/tmp/result/20121214';
10-- 注意目录20121214最后不要画蛇添足加 /*,我就是linux shell用多了,加了这玩意,调试了一下午。。。

注意:location后面跟的是目录,不是文件,hive会把整个目录下的文件都加载到表中:

1 create EXTERNAL table IF NOT EXISTS userInfo (id int,sex string, age int, name string, email string,sd string, ed string)  ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' location '/hive/dw';

否则,会报错误:

FAILED: Error in metadata: MetaException(message:Got exception: org.apache.hadoop.ipc.RemoteException java.io.FileNotFoundException: Parent path is not a directory: /hive/dw/record_2013-04-04.txt

最后提下还有一种方式是建表的时候就指定外部表的数据源路径,

但这样的坏处是只能加载一个数据源了:

CREATE EXTERNAL TABLE sunwg_test09(id INT, name string) 
ROW FORMAT DELIMITED 
FIELDS TERMINATED BY ‘\t’ 
LOCATION ‘/sunwg/test08′; 
上面的语句创建了一张名字为sunwg_test09的外表,该表有id和name两个字段, 
字段的分割符为tab,文件的数据文件夹为/sunwg/test08 
select * from sunwg_test09; 
可以查询到sunwg_test09中的数据。 
在当前用户hive的根目录下找不到sunwg_test09文件夹。 
此时hive将该表的数据文件信息保存到metadata数据库中。 
mysql> select * from TBLS where TBL_NAME=’sunwg_test09′; 
可以看到该表的类型为EXTERNAL_TABLE。 
mysql> select * from SDS where SD_ID=TBL_ID; 
在表SDS中记录了表sunwg_test09的数据文件路径为hdfs://hadoop00:9000/hjl/test08。 
# hjl为hive的数据库名 

实际上外表不光可以指定hdfs的目录,本地的目录也是可以的。 
比如: 
CREATE EXTERNAL TABLE test10(id INT, name string) 
ROW FORMAT DELIMITED 
FIELDS TERMINATED BY ‘\t’ 

LOCATION ‘file:////home/hjl/sunwg/’;

推荐阅读:

hive中partition如何使用
http://p-x1984.iteye.com/blog/1156408
用正则表达式匹配w3c日志,导入hive
http://essen.iteye.com/blog/1720491
hive中的外表EXTERNAL TABLE

http://www.oratea.net/?p=489

HIVE 数据定义 DDL

http://www.kaixinwenda.com/article-iquicksandi-8522691.html

大数据学习笔记35:Hive - 内部表与外部表 文章目录一、内部表1、什么是内部表2、创建与操作内部表任务1、创建park数据库 一、内部表 1、什么是内部表 在hive里用“create table命令”创建一张表,然后向这个表插入数据(通过insert命令插入数据,也可以通过加载外部文件的方式来插入数据),这样的表叫做内部表。 2、创建与操作内部表 任务1、创建park数据库 hive> create database park; ... 阅读详情

相关推荐

WD西部数据2TB,2.5寸移动硬盘,因为磁头坏了,长时间通电导致划片划伤,维修过程通过反复更换磁头

WD西部数据2TB,2.5寸移动硬盘,因为磁头坏了,长时间通电导致划片划伤,维修过程通过反复更换磁头,才能利用专业设备,里面找到,镜像,把内容数据恢复出来。 1.说一下经历,借用的别人移动硬盘,去帮别人维修,拷数据出来,别人的文件,数据比较多,且复杂,而且是在PE系统,接口什么的是2.0,没有很好的思考就全选复制拷贝了; 2,拷贝的过程,极其慢,文件多,且文件有损坏,移动硬盘一直处于通电的状态,大概从下午,两点多开始100多G的数据,一直卡住,到,晚上7 。8点拷完还是没拷完。打开的时候就已经,就一直

weixin_43766920的博客 4303

Hive内部表和外部表的区别

主要体现在load与drop(是否同时删除元数据与数据)的操作上: 创建表: Hive创建内部表时,会将数据移动到数据仓库指向的路径,hive管理数据的生命周期; Hive创建外部表时,仅记录数据所在的路径,不对数据的位置做任何改变。 删除表: Hive删除内部表时,内部表的元数据和数据会一起被删除,同时对于一些hive操作不适应于外部表,比如单个查询语句创建表并向表中插入数据。 Hive删除外部表时,不删除数据。这样外部表相对来说更加安全些,数据组织也更加灵活,方便共享源数据。创建外部表时,甚至不

airyv的博客 1万+

gephi生成网络关系图_路网拓扑重建专题 (3) 网络图生成

城市道路网建模在获得基础路网之后,需要考虑道路交通网络的描述与表达问题。网络是将现实世界中的事物及其关系进行适当的抽象,构造成由节点和边按照一定规则组成的形式。一个实体网络可以由有限个“节点”集合与“边”集合构成的图表示,即网络表示为。对于一个由个节点组成的无权复杂网络,该图有邻接矩阵,这里。根据研究对象不同,建模方法主要包括原始法[1]和对偶法[2]两种,见图1,字母a-j为交叉口编号...

weixin_39761195的博客 3504

hive创建表(内部表和外部表)

一、理论 1)建表语法 CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment], ...)] [COMMENT table_comment] [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)] [CLUSTERED BY (col_name, col_nam

now 1万+

抖音直播行为规范考试

抖音直播行为规范考试

Q1642968641的博客 1万+

ENVI5.6 Seamless Mosaic多景影像镶嵌色差问题

多景影像无缝镶嵌色差问题拟解决方法

Matchaya的博客 7705

Hive创建内部表

启动Hive hive 内部表 建表 create table test1(id int,name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE; 创建完成后使用 show tables; 查看是否创建成功. show tables; hive> show tables; OK s...

weixin_43865893的博客 6112

深度学习优化算法解析(Momentum, RMSProp, Adam)

之前看了很多人介绍网络的优化器optimizer,无奈很多都把简单的东西讲的复杂化了,不容易让人懂,恰好之前看过吴恩达老师的最新的课程,这里结合老师讲的和个人想法写一下,方便别人同时也为自己以后回顾吧。 深度学习的优化算法主要有GD,SGD,Momentum,RMSProp和Adam算法吧,还有诸如Adagrad算法,不过大同小异,理解了前面几个,后面的也就引刃而解了。GD算法,SGD算法以及m

willduan的博客 12万+

HIVE内表与外表的区别

hive内外表的区别,主要总结六点区别

m0_70949976的博客 2572

《拉依达的嵌入式\驱动面试宝典》—Linux篇(八)_Linux驱动编程

《拉依达的嵌入式\驱动面试宝典》 整合了所有我认为可能遇到的技术面试问题。所有问题及其答案均是我个人学习后查阅资料总结,每个回答都做了仔细的分析,在嵌入式及其驱动面试相关问题解析做到全网独一无二。学习或者转行到嵌入式、驱动开发领域学习的不二之选

拉依达不拉跨的博客 320

从泛光成像到可寻址扫描:Flash激光雷达的车规级优势与技术演进

Flash激光雷达凭借固态架构优势成为自动驾驶感知技术的重要发展方向。该方案无机械部件,具备抗振动、长寿命等特点,同时CMOS工艺带来显著成本优势。但传统泛光式存在能量弥散问题,难以兼顾大视场和远距离探测。可寻址扫描技术通过分区点亮VCSEL阵列,有效提升局部能量密度,在保持固态可靠性的同时突破性能瓶颈。这种"电子扫描+分区点亮"的混合架构,使Flash激光雷达有望实现大视场、远距离、高可靠和低成本的多重突破,推动自动驾驶技术发展。

致力于lidar应用开发 235

Hive表---内部表、外部表、数据导入和导出

内部表、外部表、数据导入和导出的学习

Yan_bigdata的博客 2694

01-诊断开发流程概述

传送门 ==>> AutoSAR入门和实战系列总目录。

1786

hive外部表改为内部表_Hive中内部表与外部表的区别与创建方法

先来说下Hive中内部表与外部表的区别:Hive 创建内部表时,会将数据移动到数据仓库指向的路径;若创建外部表,仅记录数据所在的路径,不对数据的位置做任何改变。在删除表的时候,内部表的元数据和数据会被一起删除,而外部表只删除元数据,不删除数据。这样外部表相对来说更加安全些,数据组织也更加灵活,方便共享源数据。需要注意的是传统数据库对表数据验证是 schema on write(写时模式),而 Hi...

weixin_39997400的博客 514

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Hive内部表和外部表的区别及如何创建

目前所创建的表都是所谓的管理表,有时也被称为内部表,因为这种表,Hive会(或多或少地)控制着数据的生命周期。 当删除一个管理表时,Hive也会删除这个表中的数据,管理表不方便和其他工作共享数据。 内部表(internal table/managed table): 没有external修饰,表数据保存在Hive默认的路径下,数据完全由Hive管理,删除表时元数据和表数据都会一起删除。 外部表...

qq_39783601的博客 1745

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

上一篇: Hive基本命令整理
下一篇: hbase二级索引Observer
weixir123
博客等级 码龄16年 1粉丝 207原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值