Hive 表分区
Hive表的分区就是一个目录,分区字段不和表的字段重复
创建分区表:
create table tb_partition(id string, name string)
PARTITIONED BY (month string)
row format delimited fields terminated by '\t';
加载数据到hive分区表中
方法一:通过load方式加载
load data local inpath '/home/hadoop/files/nameinfo.txt' overwrite into table tb_partition partition(month='');
方法二:insert select 方式
insert overwrite table tb_partition partition(month='') select id, name from name;
hive> insert into table tb_partition partition(month='') select id, name from name;
Query ID = hadoop_20170918222525_7d074ba1-bff9-44fc-a664-508275175849
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks is set to 0 since there's no reduce operator
方法三:可通过手动上传文件到分区目录,进行加载
hdfs dfs -mkdir /user/hive/warehouse/tb_partition/month=201710
hdfs dfs -put nameinfo.txt /user/hive/warehouse/tb_partition/month=201710
虽然方法三手动上传文件到分区目录,但是查询表的时候是查询不到数据的,需要更新元数据信息。
更新源数据的两种方法:
方法一:msck repair table 表名
hive> msck repair table tb_partition;
OK
Partitions not in metastore: tb_partition:month=201710
Repair: Added partition to metastore tb_partition:month=201710
Time taken: 0.265 seconds, Fetched: 2 row(s)
方法二:alter table tb_partition add partition(month='201708');
hive> alter table tb_partition add partition(month='');
OK
Time taken: 0.126 seconds
查询表数据:
hive> select *from tb_partition ;
OK
1 Lily 201708
2 Andy 201708
3 Tom 201708
1 Lily 201709
2 Andy 201709
3 Tom 201709
1 Lily 201710
2 Andy 201710
3 Tom 201710
Time taken: 0.161 seconds, Fetched: 9 row(s)
查询分区信息: show partitions 表名
hive> show partitions tb_partition;
OK
month=201708
month=201709
month=201710
Time taken: 0.154 seconds, Fetched: 3 row(s)
查看hdfs中的文件结构
[hadoop@node11 files]$ hdfs dfs -ls /user/hive/warehouse/tb_partition/
17/09/18 22:33:25 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 4 items
drwxr-xr-x - hadoop supergroup 0 2017-09-18 22:25 /user/hive/warehouse/tb_partition/month=201707
drwxr-xr-x - hadoop supergroup 0 2017-09-18 22:15 /user/hive/warehouse/tb_partition/month=201708
drwxr-xr-x - hadoop supergroup 0 2017-09-18 05:55 /user/hive/warehouse/tb_partition/month=201709
drwxr-xr-x - hadoop supergroup 0 2017-09-18 22:03 /user/hive/warehouse/tb_partition/month=201710
创建多级分区
create table tb_mul_partition(id string, name string)
PARTITIONED BY (month string, code string)
row format delimited fields terminated by '\t';
加载数据:
load data local inpath '/home/hadoop/files/nameinfo.txt' into table tb_mul_partition partition(month='',code='');
load data local inpath '/home/hadoop/files/nameinfo.txt' into table tb_mul_partition partition(month='',code='');
查询数据:
hive> select *From tb_mul_partition where code='';
OK
1 Lily 201709 10000
2 Andy 201709 10000
3 Tom 201709 10000
1 Lily 201710 10000
2 Andy 201710 10000
3 Tom 201710 10000
Time taken: 0.208 seconds, Fetched: 6 row(s)
测试以下指定一个分区:
hive> load data local inpath '/home/hadoop/files/nameinfo.txt' into table tb_mul_partition partition(month='');
FAILED: SemanticException [Error 10006]: Line 1:95 Partition not found ''201708''
hive> load data local inpath '/home/hadoop/files/nameinfo.txt' into table tb_mul_partition partition(code='');
FAILED: SemanticException [Error 10006]: Line 1:95 Partition not found ''20000''
创建是多级分区,指定一个分区是不可以的。
查看一下在hdfs中存储的结构:
[hadoop@node11 files]$ hdfs dfs -ls /user/hive/warehouse/tb_mul_partition/month=201710
drwxr-xr-x - hadoop supergroup 0 2017-09-18 22:36 /user/hive/warehouse/tb_mul_partition/month=201710/code=10000
动态分区
回顾一下之前的向分区插入数据:
insert overwrite table tb_partition partition(month='201707') select id, name from name;
这里需要指定具体的分区信息‘201707’,这里通过动态操作,向表里插入数据。
新建表:
hive> create table tb_copy_partition like tb_partition;
OK
Time taken: 0.118 seconds
查看一下表结构:
hive> desc tb_copy_partition;
OK
id string
name string
month string # Partition Information
# col_name data_type comment month string
Time taken: 0.127 seconds, Fetched: 8 row(s)
接下来通过动态操作,向tb_copy_partitioon里面插入数据,
insert into table tb_copy_partition partition(month) select id, name, month from tb_partition; 这里注意需要将分区字段month放到最后。
hive> insert into table tb_copy_partition partition(month) select id, name, month from tb_partition;
FAILED: SemanticException [Error 10096]: Dynamic partition strict mode requires at least one static partition column. To turn this off set hive.exec.dynamic.partition.mode=nonstrict
这里报错,使用动态加载,需要 To turn this off set hive.exec.dynamic.partition.mode=nonstrict
那根据错误信息设置一下
hive> set hive.exec.dynamic.partition.mode=nonstrict;
查询设置信息,设置成功
hive> set hive.exec.dynamic.partition.mode;
hive.exec.dynamic.partition.mode=nonstrict
重新执行:
hive> insert into table tb_copy_partition partition(month) select id, name, month from tb_partition;
Query ID = hadoop_20170918230808_0bf202da-279f-4df3-a153-ece0e457c905
Total jobs =
Launching Job out of
Number of reduce tasks is set to since there's no reduce operator
Starting Job = job_1505785612206_0002, Tracking URL = http://node11:8088/proxy/application_1505785612206_0002/
Kill Command = /home/hadoop/app/hadoop-2.6.-cdh5.10.0/bin/hadoop job -kill job_1505785612206_0002
Hadoop job information for Stage-: number of mappers: ; number of reducers:
-- ::, Stage- map = %, reduce = %
-- ::, Stage- map = %, reduce = %, Cumulative CPU 1.94 sec
-- ::, Stage- map = %, reduce = %, Cumulative CPU 3.63 sec
MapReduce Total cumulative CPU time: seconds msec
Ended Job = job_1505785612206_0002
Stage- is selected by condition resolver.
Stage- is filtered out by condition resolver.
Stage- is filtered out by condition resolver.
Moving data to: hdfs://cluster1/user/hive/warehouse/tb_copy_partition/.hive-staging_hive_2017-09-18_23-08-01_475_7542657053989652968-1/-ext-10000
Loading data to table default.tb_copy_partition partition (month=null)
Time taken for load dynamic partitions :
Loading partition {month=}
Loading partition {month=}
Loading partition {month=}
Loading partition {month=}
Time taken for adding to write entity :
Partition default.tb_copy_partition{month=} stats: [numFiles=, numRows=, totalSize=, rawDataSize=]
Partition default.tb_copy_partition{month=} stats: [numFiles=, numRows=, totalSize=, rawDataSize=]
Partition default.tb_copy_partition{month=} stats: [numFiles=, numRows=, totalSize=, rawDataSize=]
Partition default.tb_copy_partition{month=} stats: [numFiles=, numRows=, totalSize=, rawDataSize=]
MapReduce Jobs Launched:
Stage-Stage-: Map: Cumulative CPU: 3.63 sec HDFS Read: HDFS Write: SUCCESS
Total MapReduce CPU Time Spent: seconds msec
OK
Time taken: 28.932 seconds
查询一下数据:
hive> select *From tb_copy_partition;
OK
1 Lily 201707
2 Andy 201707
3 Tom 201707
1 Lily 201708
2 Andy 201708
3 Tom 201708
1 Lily 201709
2 Andy 201709
3 Tom 201709
1 Lily 201710
2 Andy 201710
3 Tom 201710
Time taken: 0.121 seconds, Fetched: 12 row(s)
完成
Hive 表分区的更多相关文章
- hive表分区相关操作
Hive 表分区 Hive表的分区就是一个目录,分区字段不和表的字段重复 创建分区表: create table tb_partition(id string, name string) PARTIT ...
- Hive表分区
必须在表定义时创建partition a.单分区建表语句:create table day_table (id int, content string) partitioned by (dt stri ...
- [Hive]使用HDFS文件夹数据创建Hive表分区
描写叙述: Hive表pms.cross_sale_path建立以日期作为分区,将hdfs文件夹/user/pms/workspace/ouyangyewei/testUsertrack/job1Ou ...
- hive表分区的修复
hive从低版本升级到高版本或者做hadoop的集群数据迁移时,需要重新创建表和表分区,由于使用的是动态分区,所以需要重新刷新分区表字段,否则无法查看数据. 在hive中执行中以下命令即可自动更新元数 ...
- 使用MSCK命令修复Hive表分区
set hive.strict.checks.large.query=false; set hive.mapred.mode=nostrict; MSCK REPAIR TABLE 表名; 通常是通过 ...
- hive 表分区操作
hive的数据查询一般会扫描整个表,当表数据太大时,就会消耗些时间,有时候我们只需要对部分数据感兴趣,所以hive引入了分区的概念 hive的表分区区别于一般的分布式分区(hash分区,范围分区 ...
- hive 表优化
一.外部表和内部表的区别 (1)创建表时指定external关键字,就是外部表,不指定external就是内部表 (2)内部表删除后把元数据和数据都删除了,外部表删除后只是删除了元数据,不会删除hdf ...
- Hive管理表分区的创建,数据导入,分区的删除操作
Hive分区和传统数据库的分区的异同: 分区技术是处理大型数据集经常用到的方法.在Oracle中,分区表中的每个分区是一个独立的segment段对象,有多少个分区,就存在多少个相应的数据库对象.而在P ...
- 分析Hive表和分区的统计信息(Statistics)
类似于Oracle的分析表,Hive中也提供了分析表和分区的功能,通过自动和手动分析Hive表,将Hive表的一些统计信息存储到元数据中. 表和分区的统计信息主要包括:行数.文件数.原始数据大小.所占 ...
随机推荐
- LVS主从部署配置和使用
LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的自由软件项目之一. LVS是L ...
- (网页)AngularJS中【Error: [$rootScope:inprog]】的解决办法(转)
转自CSDN: Error: [$rootScope:inprog] http://errors.angularjs.org/1.5.8/$rootScope/inprog?p0=%24apply 如 ...
- 洗礼灵魂,修炼python(10)--有趣的判断分支+从实例中掌握循环语句
所有的编程语言里都有判断语句和循环语句. 判断语句则是用来分支程序流程的 循环语句则是为了实现一个效果,让程序的规律性的重复操作 不用说,分支和循环自然在python里也是有的 一,条件判断:if,i ...
- What To Do When MySQL Runs Out of Memory: Troubleshooting Guide
In this article, I will show you how to use the new version of MySQL (5.7+) and how to troubleshoot ...
- HDMI驱动热插拔检测方法
1. 使用poll机制 1.1 如何使用? a. open("/dev/HPD"); b. poll状态发生变化 c. read确定接上还是接下 1.2 情景分析: APP使用op ...
- Win10 + MASM32 + EditPlus 汇编语言编程环境设置
下载安装MASM32汇编环境 官方下载站:MASM32 环境变量配置 配置MasmHome变量,值为masm32的安装目录: 配置include和lib变量 include : %MasmHome%\ ...
- 教你优化yum源。配置阿里云的yum镜像源(base和epel)
一.Centos7的base源配置阿里云的yum源: 1.备份旧的yum源目录下的所有文件 [root@ELK-chaofeng07 yum.repos.d]# mkdir ../yum.repos. ...
- 6.2Python文件的操作(二)
目录 目录 前言 (一)文件的定位 (二)文件的读操作 ==1.read()方法== ==2.readline()方法== ==3.readlines()方法== ==4.文件的遍历读法== (三)文 ...
- Hadoop2.7.6_06_mapreduce参数优化
MapReduce重要配置参数 1. 资源相关参数 //以下参数是在用户自己的mr应用程序中配置就可以生效 () mapreduce.map.memory.mb: 一个Map Task可使用的资源上限 ...
- Linux 小知识翻译 - 「Shell 脚本」
这次说说「Shell 脚本」. 根据上回的介绍,Shell就是「作为联系Linux和用户的接口而存在的软件」.在Linux环境中,通过Shell来操作系统很普遍. 这里,考虑到有时候可能想要「多次的进 ...