Hive中快速复制一张分区表的结构和数据_hive复制分区表-程序员宅基地

技术标签: # hive  hive  大数据相关  快速复制分区表的结构和数据  

首先,来回顾下在hive中创建表的三种方式:
直接创建表:create table table_name (column1 type1, column2 type2, …)
创建新表只复制旧表的结构:create table new_table_name like old_table_name
创建新表复制旧表的结构和数据:create table new_table_name as select * from old_table_name

看到上面的需求和hive中创建表的三种方式,如果是非分区表,我们可以选择上面第三种创建表的方式复制表
但在实际开发中,hive中的表大部分为分区表,针对分区表,我们可以使用上面的第二种方式先复制旧分区表的表结构,再使用动态分区,将旧表的分区数据insert到新表中,虽然能够实现功能,但并非是效率最高的

如果想提高效率,可以使用以下步骤:

1.create table new_partition like old_partition;

2.使用hdfs dfs -cp 命令,把old_partition对应的HDFS目录的文件夹全部拷贝到new_partition对应的目录下

3.使用msck repair table new_table;修复新表的分区元数据

测试下:

在我们数据库中有张表

> select * from rt_data;
OK
rt_data.shop_id	rt_data.stat_date	rt_data.ordamt
10026	201501120030	5170.0
10026	201501120100	5669.0
10026	201501120130	2396.0
10026	201501120200	1498.0
10026	201501120230	1997.0
10026	201501120300	1188.0
10026	201501120330	598.0
10026	201501120400	479.0
10026	201501120430	1587.0
10026	201501120530	799.0
10027	201501120030	2170.0
10027	201501120100	1623.0
10027	201501120130	3397.0
10027	201501120200	1434.0
10027	201501120230	1001.0
10028	201501120300	1687.0
10028	201501120330	1298.0
10028	201501120400	149.0
10029	201501120430	2587.0
10029	201501120530	589.0
Time taken: 12.712 seconds, Fetched: 20 row(s)

创建一张分区表

> create table old_partition (shop_id int, stat_date string, ordamt double) partitioned by (daystr string); 
OK
Time taken: 0.17 seconds

设置动态分区

set hive.exec.dynamic.partition.mode=nonstrict;

将rt_data的数据动态插入old_partition表中

insert into table old_partition partition (daystr)
                > select shop_id, stat_date, ordamt, stat_date from rt_data;
Query ID = rdedu_20180729105529_0d16042f-5d27-4616-826f-bc5e02067767
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks not specified. Estimated from input data size: 1
In order to change the average load for a reducer (in bytes):
  set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
  set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
  set mapreduce.job.reduces=<number>
Starting Job = job_1532570467965_0057, Tracking URL = http://bigdata-training01.erongda.com:8088/proxy/application_1532570467965_0057/
Kill Command = /opt/cdh-5.7.6/hadoop-2.6.0-cdh5.7.6/bin/mapred job  -kill job_1532570467965_0057
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 1
2018-07-29 10:59:14,083 Stage-1 map = 0%,  reduce = 0%
2018-07-29 11:00:04,874 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 20.9 sec
2018-07-29 11:00:17,029 Stage-1 map = 100%,  reduce = 100%, Cumulative CPU 23.07 sec
MapReduce Total cumulative CPU time: 23 seconds 70 msec
Ended Job = job_1532570467965_0057
Stage-4 is selected by condition resolver.
Stage-3 is filtered out by condition resolver.
Stage-5 is filtered out by condition resolver.
Moving data to directory hdfs://bigdata-training01.erongda.com:8020/user/hive/warehouse-3.1.1/db_window.db/old_partition/.hive-staging_hive_2018-07-29_10-55-
29_054_1770048812106415069-1/-ext-10000Loading data to table db_window.old_partition partition (daystr=null)


	 Time taken to load dynamic partitions: 1.588 seconds
	 Time taken for adding to write entity : 0.014 seconds
MapReduce Jobs Launched: 
Stage-Stage-1: Map: 1  Reduce: 1   Cumulative CPU: 23.07 sec   HDFS Read: 18383 HDFS Write: 3075 SUCCESS
Total MapReduce CPU Time Spent: 23 seconds 70 msec
OK
shop_id	stat_date	ordamt	stat_date
Time taken: 292.625 seconds

注意:这样动态插入花费的时间是292.625s,才二十几条数据,花了这么长的时间,如果是上千万条数据,可以想象所需要的处理时间会有多长。

查看分区

> show partitions old_partition;
OK
partition
daystr=201501120030
daystr=201501120100
daystr=201501120130
daystr=201501120200
daystr=201501120230
daystr=201501120300
daystr=201501120330
daystr=201501120400
daystr=201501120430
daystr=201501120530
Time taken: 0.112 seconds, Fetched: 10 row(s)

先设置hive.mapred.mode=nonstrict再查看old_partition中的数据
如果未设置此参数,select分区表时必须指定分区,不然会报错

> set hive.mapred.mode=nonstrict
> select * from old_partition;
OK
old_partition.shop_id	old_partition.stat_date	old_partition.ordamt	old_partition.daystr
10026	201501120030	5170.0	201501120030
10027	201501120030	2170.0	201501120030
10026	201501120100	5669.0	201501120100
10027	201501120100	1623.0	201501120100
10026	201501120130	2396.0	201501120130
10027	201501120130	3397.0	201501120130
10026	201501120200	1498.0	201501120200
10027	201501120200	1434.0	201501120200
10026	201501120230	1997.0	201501120230
10027	201501120230	1001.0	201501120230
10026	201501120300	1188.0	201501120300
10028	201501120300	1687.0	201501120300
10026	201501120330	598.0	201501120330
10028	201501120330	1298.0	201501120330
10026	201501120400	479.0	201501120400
10028	201501120400	149.0	201501120400
10026	201501120430	1587.0	201501120430
10029	201501120430	2587.0	201501120430
10026	201501120530	799.0	201501120530
10029	201501120530	589.0	201501120530
Time taken: 0.425 seconds, Fetched: 20 row(s)

使用第二种方法复制旧分区表结构和数据

> create table new_partition like old_partition;
OK
Time taken: 0.224 seconds

> show create table new_partition;
OK
createtab_stmt
CREATE TABLE `new_partition`(
  `shop_id` int, 
  `stat_date` string, 
  `ordamt` double)
PARTITIONED BY ( 
  `daystr` string)
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  'hdfs://bigdata-training01.fuyun.com:8020/user/hive/warehouse-3.1.1/db_window.db/new_partition'
TBLPROPERTIES (
  'transient_lastDdlTime'='1532834516')
Time taken: 0.17 seconds, Fetched: 16 row(s)

使用hdfs dfs -cp 命令,把old_partition对应的HDFS目录的文件夹全部拷贝到new_partition对应的目录下

cd /opt/cdh-5.7.6/hadoop-2.6.0-cdh5.7.6

bin/hdfs dfs -cp /user/hive/warehouse-3.1.1/db_window.db/old_partition/* /user/hive/warehouse-3.1.1/db_window.db/new_partition

bin/hdfs dfs -ls /user/hive/warehouse-3.1.1/db_window.db/new_partition
Found 10 items
drwxr-xr-x   - rdedu supergroup          0 2018-07-29 11:25 /user/hive/warehouse-3.1.1/db_window.db/new_partition/daystr=201501120030
drwxr-xr-x   - rdedu supergroup          0 2018-07-29 11:25 /user/hive/warehouse-3.1.1/db_window.db/new_partition/daystr=201501120100
drwxr-xr-x   - rdedu supergroup          0 2018-07-29 11:25 /user/hive/warehouse-3.1.1/db_window.db/new_partition/daystr=201501120130
drwxr-xr-x   - rdedu supergroup          0 2018-07-29 11:25 /user/hive/warehouse-3.1.1/db_window.db/new_partition/daystr=201501120200
drwxr-xr-x   - rdedu supergroup          0 2018-07-29 11:25 /user/hive/warehouse-3.1.1/db_window.db/new_partition/daystr=201501120230
drwxr-xr-x   - rdedu supergroup          0 2018-07-29 11:25 /user/hive/warehouse-3.1.1/db_window.db/new_partition/daystr=201501120300
drwxr-xr-x   - rdedu supergroup          0 2018-07-29 11:25 /user/hive/warehouse-3.1.1/db_window.db/new_partition/daystr=201501120330
drwxr-xr-x   - rdedu supergroup          0 2018-07-29 11:25 /user/hive/warehouse-3.1.1/db_window.db/new_partition/daystr=201501120400
drwxr-xr-x   - rdedu supergroup          0 2018-07-29 11:25 /user/hive/warehouse-3.1.1/db_window.db/new_partition/daystr=201501120430
drwxr-xr-x   - rdedu supergroup          0 2018-07-29 11:25 /user/hive/warehouse-3.1.1/db_window.db/new_partition/daystr=201501120530

到hive中查看new_partition表的分区情况

> show partitions new_partition;
OK
partition
Time taken: 0.124 seconds

发现并没有分区,使用msck repair table new_table;修复源数据再查看分区情况

> msck repair table new_partition;
OK
Partitions not in metastore:	new_partition:daystr=201501120030	new_partition:daystr=201501120100	new_partition:daystr=201501120130	new_p
artition:daystr=201501120200	new_partition:daystr=201501120230	new_partition:daystr=201501120300	new_partition:daystr=201501120330	new_partition:daystr=201501120400	new_partition:daystr=201501120430	new_partition:daystr=201501120530Repair: Added partition to metastore new_partition:daystr=201501120530
Repair: Added partition to metastore new_partition:daystr=201501120330
Repair: Added partition to metastore new_partition:daystr=201501120230
Repair: Added partition to metastore new_partition:daystr=201501120030
Repair: Added partition to metastore new_partition:daystr=201501120130
Repair: Added partition to metastore new_partition:daystr=201501120100
Repair: Added partition to metastore new_partition:daystr=201501120200
Repair: Added partition to metastore new_partition:daystr=201501120300
Repair: Added partition to metastore new_partition:daystr=201501120400
Repair: Added partition to metastore new_partition:daystr=201501120430
Time taken: 0.742 seconds, Fetched: 11 row(s)

> show partitions new_partition;
OK
partition
daystr=201501120030
daystr=201501120100
daystr=201501120130
daystr=201501120200
daystr=201501120230
daystr=201501120300
daystr=201501120330
daystr=201501120400
daystr=201501120430
daystr=201501120530
Time taken: 0.082 seconds, Fetched: 10 row(s)

这样新的分区表已经复制好,新的分区表和旧的分区表有一样的结构和数据,可以从上面两种测试中看出,第一种复制的方法时间明显比第二种方法慢很多,并且测试数据只有几十条,如果上千万的数据会更慢更明显。

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lz6363/article/details/86709038

智能推荐

MySQL自动备份脚本_mysqldump 脚本-程序员宅基地

文章浏览阅读660次,点赞10次,收藏10次。mysqldump命令将数据库中的数据备份成一个文本文件,表的结构和数据将存储在生成的文本文件中。将备份出来的数据还原到某个数据库中。备份一个数据库下的多个表。MySQL自动备份脚本。也可以登入数据之后进行。_mysqldump 脚本

程序员刚写完代码,就被开除了_程序员需求写不出来被开除-程序员宅基地

文章浏览阅读1.1k次。学小编逛贴吧发现一个热帖,就凑了凑热闹~~~看到这样一个帖。一程序员说自己刚写完代码,就别公司老板给开除了。为什么会这样的呢?原来是这位程序员写了一段这样的代码:public static Date getNextDay() {try {Thread.sleep(246060*1000);} catch (InterruptrdException e) {e.printStackTr..._程序员需求写不出来被开除

android 9.0 SystemUI导航栏添加虚拟按键功能(三)_android 导航栏 增加按钮-程序员宅基地

文章浏览阅读970次。在9.0的系统产品开发中,对于在SystemUI的原生系统中默认只有三键导航,想添加其他虚拟按键就需要先在构建导航栏的相关布局中分析结构,然后添加相关的图标xml就可以了,然后添加对应的点击事件,就可以了,接下来先分析第三步关于导航栏的相关布局情况然后实现虚拟按键的点击事件功能_android 导航栏 增加按钮

分享面经 竞争对手,Android多线程,Android岗面试12家大厂成功跳槽-程序员宅基地

文章浏览阅读968次,点赞15次,收藏30次。Java里已内置4种常用的线程池(即 已经配置好核心参数),下面会详细说明。

Android使用自己的字体库让你的APP更漂亮_android 漂亮字库-程序员宅基地

文章浏览阅读1.9k次。Android系统是一个相对开放的系统,在我们平常开发的时候有时候会直接使用原生的控件更换下背景图片。如果想让我们的APP变得大漂亮,那么我们会对UI进行深度定制,变成我们所希望的那样。而一个APP的大漂亮除了UI的漂亮,很多时候字体也是影响大漂亮的因素之一。最近在开发中就有客户的需求是数字要显示为那种液晶仪表盘的那种方正一些的样子。OMG,可难为死我了。试了下Android自带的字体库,支持三种字体:Sans、serif、monospace,然后试了下。格劳资滴,看不出太明显的变化嘛,更别说要满足客户的_android 漂亮字库

操作系统期末复习知识点总结-程序员宅基地

文章浏览阅读4w次,点赞289次,收藏2.5k次。1. 操作系统的定义OS是一组控制和管理计算机硬件和软件资源,合理地对各类作业进行调度,方便用户使用计算机的程序集合。即为用户程序提供服务,是用户与硬件系统之间的接口。2. 操作系统的作用OS是计算机系统的核心,负责管理整个计算机系统的软硬件资源,制定各种资源的分配策略,调度系统中运行的用户程序,协调用户对资源的需求,从而使真个计算机系统高效有序的工作3. 操作系统的常见的分类①批处理操作系统:单道批处理(自动、顺序and单道性),多道批处理(宏观上并行,微观上串行,资源利用率高系统吞吐量大;用户响应时间长_操作系统期末复习

随便推点

Ubuntu系统使用技巧 Vim基本技巧介绍_ubuntu系统vim操作-程序员宅基地

文章浏览阅读231次。Ubuntu系统使用技巧: Ctrl+alt+t: 打开终端​ Ctrl+L:清理屏幕​ Tab: 自动补全​ up/down: 调出命令执行记录​ Ctrl+Alt+方向键:切换工作区​ Alt+Tab: 切换任务重定向、管道、通配符:重定向:就是把命令的执行结果写入到文件中​ cmd > filename 把命令的执行结果以清空的方式写入到文件中(先清空再写入)​ cmd >> filename 把命令的执行结果以追加的方式写入到文件中管道:_ubuntu系统vim操作

Nacos 使用指南-程序员宅基地

文章浏览阅读420次,点赞3次,收藏8次。123。

vue基础指令_在vue中,可以通过____语法将数据输出到页面中。-程序员宅基地

文章浏览阅读1k次。指令概述:指令:英文单词,描述了一种简单的功能操作,如获取焦点;简单的DOM操作在Vue中统一的被封装成指令进行操作;什么是指令:包含了简单的DOM操作功能的组件!Vue中提供了自己的内建指令(掌握)、根据实际情况自定义指令(了解)Vue中的指令都是: 固定语法格式一、v-text指令指令名称:文本渲染指令指令描述:输出指令对应表达式的数据,即使数据中包含了标签,不会进行编译解释直接原样输出二、v-html指令名称:超文本/富文本渲染指令指令描述:输出指令对应表达式的数据,数据中一旦包含,会进行渲染解_在vue中,可以通过____语法将数据输出到页面中。

Navicat将Oracle数据导入到MySQL_navicate导出的oracel脚本可以导入到其它数据库内吗-程序员宅基地

文章浏览阅读1.1k次。本文介绍如何使用Navicat这款数据库工具,将Oracle上的数据导入到MySQL中。我是想将Oracle数据中的boot_init数据库数据传输到MySQL中。首先,你要有两个已经打开的Oracle和MySQL的数据库连接。然后点击 工具 》 数据传输 :然后进入数据传输界面,设置好源和目标。源连接——选择Oracle连接源模式——选择boot_init数据库..._navicate导出的oracel脚本可以导入到其它数据库内吗

有道云生成html,有道云笔记添加收藏功能实现原理-程序员宅基地

文章浏览阅读670次。有道云笔记收藏功能的本质就是在浏览器上执行一段js脚本,用js脚本来实现收藏功能,这和sec-wiki的快速分享到wiki功能类似。通过执行js这种方法而不是采用插件方式的好处是不用考虑浏览器的兼容性,具有通用性(虽然具体每个浏览器的js代码不一样,但可以写出具有通用性的js代码来兼容所有的浏览器)。使用js进行收藏的缺点是对某些网站失效,如github等https网站.下面是针对有道云笔记的收藏..._有道云网页收藏

Kali安装完成后的基本配置_kali tab预选-程序员宅基地

文章浏览阅读3.6k次,点赞2次,收藏9次。原文:https://blog.csdn.net/chaootis1/article/details/84137460Vmware tool安装,安装好的kali你会发现不能全屏,也不能从虚拟机到本机或者本机到虚拟机复制文件和文本,这样很不方便。。。我在初学的时候以为虚拟机就是这么难用。事实上,虚拟机为我们提供了一个工具可以解决这些问题。安装方法点击虚拟机上方的 虚拟机选项,倒数..._kali tab预选