Hive表的分区就是一个目录,分区字段不和表的字段重复

创建分区表:

  1. create table tb_partition(id string, name string)
  2. PARTITIONED BY (month string)
  3. row format delimited fields terminated by '\t';

加载数据到hive分区表中

方法一:通过load方式加载

  1. load data local inpath '/home/hadoop/files/nameinfo.txt' overwrite into table tb_partition partition(month='');

方法二:insert select 方式

  1. insert overwrite table tb_partition partition(month='') select id, name from name;
  1. hive> insert into table tb_partition partition(month='') select id, name from name;
  2. Query ID = hadoop_20170918222525_7d074ba1-bff9-44fc-a664-508275175849
  3. Total jobs = 3
  4. Launching Job 1 out of 3
  5. Number of reduce tasks is set to 0 since there's no reduce operator

方法三:可通过手动上传文件到分区目录,进行加载

  1. 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 表名

  1. hive> msck repair table tb_partition;
  2. OK
  3. Partitions not in metastore: tb_partition:month=201710
  4. Repair: Added partition to metastore tb_partition:month=201710
  5. Time taken: 0.265 seconds, Fetched: 2 row(s)

方法二:alter table tb_partition add partition(month='201708');

  1. hive> alter table tb_partition add partition(month='');
  2. OK
  3. Time taken: 0.126 seconds

查询表数据:

  1. hive> select *from tb_partition ;
  2. OK
  3. 1 Lily 201708
  4. 2 Andy 201708
  5. 3 Tom 201708
  6. 1 Lily 201709
  7. 2 Andy 201709
  8. 3 Tom 201709
  9. 1 Lily 201710
  10. 2 Andy 201710
  11. 3 Tom 201710
  12. Time taken: 0.161 seconds, Fetched: 9 row(s)

查询分区信息: show partitions 表名

  1. hive> show partitions tb_partition;
  2. OK
  3. month=201708
  4. month=201709
  5. month=201710
  6. Time taken: 0.154 seconds, Fetched: 3 row(s)

查看hdfs中的文件结构

  1. [hadoop@node11 files]$ hdfs dfs -ls /user/hive/warehouse/tb_partition/
  2. 17/09/18 22:33:25 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
  3. Found 4 items
  4. drwxr-xr-x - hadoop supergroup 0 2017-09-18 22:25 /user/hive/warehouse/tb_partition/month=201707
  5. drwxr-xr-x - hadoop supergroup 0 2017-09-18 22:15 /user/hive/warehouse/tb_partition/month=201708
  6. drwxr-xr-x - hadoop supergroup 0 2017-09-18 05:55 /user/hive/warehouse/tb_partition/month=201709
  7. drwxr-xr-x - hadoop supergroup 0 2017-09-18 22:03 /user/hive/warehouse/tb_partition/month=201710

创建多级分区

  1. create table tb_mul_partition(id string, name string)
  2. PARTITIONED BY (month string, code string)
  3. row format delimited fields terminated by '\t';

加载数据:

  1. load data local inpath '/home/hadoop/files/nameinfo.txt' into table tb_mul_partition partition(month='',code='');
  2. load data local inpath '/home/hadoop/files/nameinfo.txt' into table tb_mul_partition partition(month='',code='');

查询数据:

  1. hive> select *From tb_mul_partition where code='';
  2. OK
  3. 1 Lily 201709 10000
  4. 2 Andy 201709 10000
  5. 3 Tom 201709 10000
  6. 1 Lily 201710 10000
  7. 2 Andy 201710 10000
  8. 3 Tom 201710 10000
  9. Time taken: 0.208 seconds, Fetched: 6 row(s)

测试以下指定一个分区:

  1. hive> load data local inpath '/home/hadoop/files/nameinfo.txt' into table tb_mul_partition partition(month='');
  2. FAILED: SemanticException [Error 10006]: Line 1:95 Partition not found ''201708''
  1. hive> load data local inpath '/home/hadoop/files/nameinfo.txt' into table tb_mul_partition partition(code='');
  2. FAILED: SemanticException [Error 10006]: Line 1:95 Partition not found ''20000''

创建是多级分区,指定一个分区是不可以的。

查看一下在hdfs中存储的结构:

  1. [hadoop@node11 files]$ hdfs dfs -ls /user/hive/warehouse/tb_mul_partition/month=201710
  2. drwxr-xr-x - hadoop supergroup 0 2017-09-18 22:36 /user/hive/warehouse/tb_mul_partition/month=201710/code=10000

动态分区

回顾一下之前的向分区插入数据:

  1. insert overwrite table tb_partition partition(month='201707') select id, name from name;

这里需要指定具体的分区信息‘201707’,这里通过动态操作,向表里插入数据。

新建表:

  1. hive> create table tb_copy_partition like tb_partition;
  2. OK
  3. Time taken: 0.118 seconds

查看一下表结构:

  1. hive> desc tb_copy_partition;
  2. OK
  3. id string
  4. name string
  5. month string
  6.  
  7. # Partition Information
  8. # col_name data_type comment
  9.  
  10. month string
  11. 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放到最后。

  1. hive> insert into table tb_copy_partition partition(month) select id, name, month from tb_partition;
  2. 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

那根据错误信息设置一下

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

查询设置信息,设置成功

  1. hive> set hive.exec.dynamic.partition.mode;
  2. hive.exec.dynamic.partition.mode=nonstrict

重新执行:

  1. hive> insert into table tb_copy_partition partition(month) select id, name, month from tb_partition;
  2. Query ID = hadoop_20170918230808_0bf202da-279f-4df3-a153-ece0e457c905
  3. Total jobs =
  4. Launching Job out of
  5. Number of reduce tasks is set to since there's no reduce operator
  6. Starting Job = job_1505785612206_0002, Tracking URL = http://node11:8088/proxy/application_1505785612206_0002/
  7. Kill Command = /home/hadoop/app/hadoop-2.6.-cdh5.10.0/bin/hadoop job -kill job_1505785612206_0002
  8. Hadoop job information for Stage-: number of mappers: ; number of reducers:
  9. -- ::, Stage- map = %, reduce = %
  10. -- ::, Stage- map = %, reduce = %, Cumulative CPU 1.94 sec
  11. -- ::, Stage- map = %, reduce = %, Cumulative CPU 3.63 sec
  12. MapReduce Total cumulative CPU time: seconds msec
  13. Ended Job = job_1505785612206_0002
  14. Stage- is selected by condition resolver.
  15. Stage- is filtered out by condition resolver.
  16. Stage- is filtered out by condition resolver.
  17. 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
  18. Loading data to table default.tb_copy_partition partition (month=null)
  19. Time taken for load dynamic partitions :
  20. Loading partition {month=}
  21. Loading partition {month=}
  22. Loading partition {month=}
  23. Loading partition {month=}
  24. Time taken for adding to write entity :
  25. Partition default.tb_copy_partition{month=} stats: [numFiles=, numRows=, totalSize=, rawDataSize=]
  26. Partition default.tb_copy_partition{month=} stats: [numFiles=, numRows=, totalSize=, rawDataSize=]
  27. Partition default.tb_copy_partition{month=} stats: [numFiles=, numRows=, totalSize=, rawDataSize=]
  28. Partition default.tb_copy_partition{month=} stats: [numFiles=, numRows=, totalSize=, rawDataSize=]
  29. MapReduce Jobs Launched:
  30. Stage-Stage-: Map: Cumulative CPU: 3.63 sec HDFS Read: HDFS Write: SUCCESS
  31. Total MapReduce CPU Time Spent: seconds msec
  32. OK
  33. Time taken: 28.932 seconds

查询一下数据:

  1. hive> select *From tb_copy_partition;
  2. OK
  3. 1 Lily 201707
  4. 2 Andy 201707
  5. 3 Tom 201707
  6. 1 Lily 201708
  7. 2 Andy 201708
  8. 3 Tom 201708
  9. 1 Lily 201709
  10. 2 Andy 201709
  11. 3 Tom 201709
  12. 1 Lily 201710
  13. 2 Andy 201710
  14. 3 Tom 201710
  15. 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. 【读书笔记】iOS-iOS流媒体

    在网络上直接看电影已经不是什么新鲜的事情,在iOS等移动设备上也有很多在线视频应用,如国内的PPS和PPLive应用,还有一些新闻视频都可以在线观看,如USA TODY.所以这些在线视频都采用流媒体技 ...

  2. docker第一章:docker核心概念及centos6下安装

    Docker三大核心概念 镜像 容器 仓库 镜像 docker镜像类似于虚拟机镜像,可以将它理解为一个面向Docker引擎的只读模板,包含了文件系统. 容器 1.容器是从镜像创建的应用运行实例,容器和 ...

  3. Linux 操作系统下为网卡配置ip

    Linux操作系统下为网卡配置ip by:授客 QQ:1033553122 1.   Linux单一网卡设置多IP的配置方法 在Linux下网卡接口逻辑名被称为eth0,eth1,eth2,..... ...

  4. loadrunner 运行场景-命令行运行场景

    运行场景-命令行运行场景 by:授客 QQ:1033553122 1 相对路径与绝对路径 在场景中为脚本指定一个相对位置,可以是相对于当前场景目录或lr安装目录. 当你运行一个场景,场景自动从这个相对 ...

  5. Flutter 布局(九)- Flow、Table、Wrap详解

    本文主要介绍Flutter布局中的Flow.Table.Wrap控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析. 1. Flow A widget that implements the ...

  6. Java并发编程(十二)Callable、Future和FutureTask

    一.Callable与Runnable 先说一下java.lang.Runnable吧,它是一个接口,在它里面只声明了一个run()方法: public interface Runnable { pu ...

  7. 通过url动态获取图片大小方法总结

    很多时候再项目中,我们往往需要先获取图片的大小再加载图片,但是某些特定场景,如用过cocos2d-js的人都知道,在它那里只能按比例缩放大小,是无法设置指定大小的图片的,这就是cocos2d-js 的 ...

  8. python变量的命名空间

    首先必须要提一下python程序执行过程中变量的查找规则 较官方的查找机制是: 局部作用域--外部函数作用域--全局作用域--内建函数作用域 其实一般内建函数中的作用域很少会涉及到,因为内建函数其实是 ...

  9. linux设置自动更换壁纸

    #!/bin/bash let n=0 files=($HOME/wallpapers/*.jpg) count=${#files[@]} while [ 1 ] do let "n=n%$ ...

  10. 让bootstrap-table支持高度百分比

    更改BootstrapTable.prototype.resetView 方法,以支持高度百分比定义,适应不同高度屏幕 BootstrapTable.prototype.resetView = fun ...