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 表分区的更多相关文章

  1. hive表分区相关操作

    Hive 表分区 Hive表的分区就是一个目录,分区字段不和表的字段重复 创建分区表: create table tb_partition(id string, name string) PARTIT ...

  2. Hive表分区

    必须在表定义时创建partition a.单分区建表语句:create table day_table (id int, content string) partitioned by (dt stri ...

  3. [Hive]使用HDFS文件夹数据创建Hive表分区

    描写叙述: Hive表pms.cross_sale_path建立以日期作为分区,将hdfs文件夹/user/pms/workspace/ouyangyewei/testUsertrack/job1Ou ...

  4. hive表分区的修复

    hive从低版本升级到高版本或者做hadoop的集群数据迁移时,需要重新创建表和表分区,由于使用的是动态分区,所以需要重新刷新分区表字段,否则无法查看数据. 在hive中执行中以下命令即可自动更新元数 ...

  5. 使用MSCK命令修复Hive表分区

    set hive.strict.checks.large.query=false; set hive.mapred.mode=nostrict; MSCK REPAIR TABLE 表名; 通常是通过 ...

  6. hive 表分区操作

    hive的数据查询一般会扫描整个表,当表数据太大时,就会消耗些时间,有时候我们只需要对部分数据感兴趣,所以hive引入了分区的概念    hive的表分区区别于一般的分布式分区(hash分区,范围分区 ...

  7. hive 表优化

    一.外部表和内部表的区别 (1)创建表时指定external关键字,就是外部表,不指定external就是内部表 (2)内部表删除后把元数据和数据都删除了,外部表删除后只是删除了元数据,不会删除hdf ...

  8. Hive管理表分区的创建,数据导入,分区的删除操作

    Hive分区和传统数据库的分区的异同: 分区技术是处理大型数据集经常用到的方法.在Oracle中,分区表中的每个分区是一个独立的segment段对象,有多少个分区,就存在多少个相应的数据库对象.而在P ...

  9. 分析Hive表和分区的统计信息(Statistics)

    类似于Oracle的分析表,Hive中也提供了分析表和分区的功能,通过自动和手动分析Hive表,将Hive表的一些统计信息存储到元数据中. 表和分区的统计信息主要包括:行数.文件数.原始数据大小.所占 ...

随机推荐

  1. docker 安装jenkins

    基于docker 进行安装 软件,首先需要有docker环境. 1.docker 下载 jenkins 镜像 指定版本 ,因为低版本的后面安装 软件会失败(亲测). docker pull jenki ...

  2. TUM数据集rgbd_benchmark工具的使用方法

    # 在学习视觉slam过程中,需要对数据集合进行预处理和对slam或者跟踪结果进行评价,TUM提供一组这样的工具,为了自己以后方便查找,于是把它记录下来 一.RGBD_Benchmark工具下载链接: ...

  3. Android basics

    只要是Android中的控件,最终都继承自View.

  4. Java并发编程(九)并发容器

    并发容器的简单介绍: ConcurrentHashMap代替同步的Map(Collections.synchronized(new HashMap())),众所周知,HashMap是根据散列值分段存储 ...

  5. 从零自学Java-10.充分利用现有对象

    1.超类和子类的设计:2.建立继承层次:3.覆盖方法. 程序StringLister:使用数组列表和特殊的for循环将一系列字符串按字母顺序显示到屏幕上.这些字符串来自一个数组和命令行参数 packa ...

  6. C# 异步编程1 APM 异步程序开发

    C#已有10多年历史,单从微软2年一版的更新进度来看活力异常旺盛,C#中的异步编程也经历了多个版本的演化,从今天起着手写一个系列博文,记录一下C#中的异步编程的发展历程.广告一下:喜欢我文章的朋友,请 ...

  7. [20171205]uniq命令的输入输出.txt

    [20171205]uniq命令的输入输出.txt --//前几天遇到XXD与通配符问题,链接http://blog.itpub.net/267265/viewspace-2147702/--//今天 ...

  8. python第十九天——感冒中

    ConfigParser模块,hashlib模块,hmac模块: 创建配置文件: import configparser config = configparser.ConfigParser()#创建 ...

  9. sql语句进阶教程

    转载自:http://blog.csdn.net/u011001084/article/details/51318434 最近从图书馆借了本介绍SQL的书,打算复习一下基本语法,记录一下笔记,整理一下 ...

  10. 出现error: stray ‘\357’ in program的根源

    分类: 编程语言/ C#/ 文章 这次又遇到这个这种问题,想找到它的根源.找到一个表格: The characters at a glance Here are all the printable c ...