静态分区表:

一级分区表:

CREATE TABLE order_created_partition (
orderNumber STRING
, event_time STRING
)
PARTITIONED BY (event_month string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';

加载数据方式一:从本地/HDFS目录加载

load data local inpath '/home/spark/software/data/order_created.txt' overwrite into table order_created_partition PARTITION(event_month='2014-05');
select * from order_created_partition where event_month='2014-05';
+-----------------+-----------------------------+--------------+
| ordernumber | event_time | event_month |
+-----------------+-----------------------------+--------------+
| 10703007267488 | 2014-05-01 06:01:12.334+01 | 2014-05 |
| 10101043505096 | 2014-05-01 07:28:12.342+01 | 2014-05 |
| 10103043509747 | 2014-05-01 07:50:12.33+01 | 2014-05 |
| 10103043501575 | 2014-05-01 09:27:12.33+01 | 2014-05 |
| 10104043514061 | 2014-05-01 09:03:12.324+01 | 2014-05 |
+-----------------+-----------------------------+--------------+

加载数据方式二:手工上传文件到hdfs上,然后将数据添加到分区表指定的分区:

1) 创建hdfs目录:在hdfs目录:/user/hive/warehouse/order_created_partition目录下创建event_month=2014-06

hadoop fs -mkdir /user/hive/warehouse/order_created_partition/event_month=-

2)拷贝数据到新创建的目录下:

hadoop fs -put /home/spark/software/data/order_created.txt /user/hive/warehouse/order_created_partition/event_month=-

select * from order_created_partition where event_month='2014-06'; #发现查询结果是空的

3)添加新分区数据到元数据信息中:

msck repair table order_created_partition;

输出日志信息:

Partitions not in metastore: order_created_partition:event_month=-
Repair: Added partition to metastore order_created_partition:event_month=-

或者: alter table order_created_partition add partition(dt='2014-06');

select * from order_created_partition where event_month='2014-06'; 
+-----------------+-----------------------------+--------------+
| ordernumber | event_time | event_month |
+-----------------+-----------------------------+--------------+
| 10703007267488 | 2014-05-01 06:01:12.334+01 | 2014-06 |
| 10101043505096 | 2014-05-01 07:28:12.342+01 | 2014-06 |
| 10103043509747 | 2014-05-01 07:50:12.33+01 | 2014-06 |
| 10103043501575 | 2014-05-01 09:27:12.33+01 | 2014-06 |
| 10104043514061 | 2014-05-01 09:03:12.324+01 | 2014-06 |
+-----------------+-----------------------------+--------------+

加载数据方式三:select查询方式insert/overwrite

CREATE TABLE order_created_4_partition (
orderNumber STRING
, event_time STRING
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
load data local inpath '/home/spark/software/data/order_created.txt' overwrite into table order_created_4_partition;

insert into table order_created_partition partition(event_month='2014-07') select * from order_created_4_partition;
insert overwrite table order_created_partition partition(event_month='2014-07') select * from order_created_4_partition;

对比:

insert overwrite table order_created_partition partition(event_month='2014-07') select ordernumber,event_time from order_created_4_partition;
insert overwrite table order_created_partition partition(event_month='2014-07') select event_time,ordernumber from order_created_4_partition;

发现字段值错位,在使用时一定要注意:字段值顺序要与表中字段顺序一致,名称可以不一致;

查看分区表已有的所有分区:

show partitions order_created_partition;

查看分区表已有的指定分区:

SHOW PARTITIONS order_created_partition PARTITION(event_month='2014-06');

查看表字段信息:

desc order_created_partition;
desc extended order_created_partition;
desc formatted order_created_partition;
desc formatted order_created_partition partition(event_month='2014-05');

二级分区表:

CREATE TABLE order_created_partition2 (
orderNumber STRING
, event_time STRING
)
PARTITIONED BY (event_month string, step string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
show partitions order_created_partition2;

显示结果空

load data local inpath '/home/spark/software/data/order_created.txt' into table order_created_partition2 partition(event_month='2014-09',step='');
show partitions order_created_partition2;
+-----------------------------+
| result |
+-----------------------------+
| event_month=2014-09/step=1 |
+-----------------------------+
insert overwrite table order_created_partition2 partition(event_month='2014-09',step='') select * from order_created_4_partition;
show partitions order_created_partition2;
+-----------------------------+
| result |
+-----------------------------+
| event_month=2014-09/step=1 |
| event_month=2014-09/step=2 |
+-----------------------------+

动态分区表

CREATE TABLE order_created_dynamic_partition (
orderNumber STRING
, event_time STRING
)
PARTITIONED BY (event_month string)
;
insert into table order_created_dynamic_partition PARTITION (event_month)
select orderNumber, event_time, substr(event_time, 1, 7) as event_month from order_created;

报错:

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

解决方案:

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

重新执行:

insert into table order_created_dynamic_partition PARTITION (event_month)
select orderNumber, event_time, substr(event_time, 1, 7) as event_month from order_created;
select * from order_created_dynamic_partition;
+-----------------+-----------------------------+--------------+
| ordernumber | event_time | event_month |
+-----------------+-----------------------------+--------------+
| 10703007267488 | 2014-05-01 06:01:12.334+01 | 2014-05 |
| 10101043505096 | 2014-05-01 07:28:12.342+01 | 2014-05 |
| 10103043509747 | 2014-05-01 07:50:12.33+01 | 2014-05 |
| 10103043501575 | 2014-05-01 09:27:12.33+01 | 2014-05 |
| 10104043514061 | 2014-05-01 09:03:12.324+01 | 2014-05 |
+-----------------+-----------------------------+--------------+

Hive静态分区表&动态分区表的更多相关文章

  1. hadoop笔记之Hive的数据存储(分区表)

    Hive的数据存储(分区表) Hive的数据存储(分区表) 分区表 Partition对应于数据库的Partition列的密集索引 在Hive中,表中的一个Partition对应于表下的一个目录,所有 ...

  2. Hive入门--2.分区表 外部分区表 关联查询

    1.查看mysql中metastore数据存储结构 Metastore中只保存了表的描述信息(名字,列,类型,对应目录) 使用SQLYog连接itcast05 的mysql数据库  查看hive数据库 ...

  3. Android中BroadcastReceiver的两种注册方式(静态和动态)详解

    今天我们一起来探讨下安卓中BroadcastReceiver组件以及详细分析下它的两种注册方式. BroadcastReceiver也就是"广播接收者"的意思,顾名思义,它就是用来 ...

  4. 玩转SQL Server复制回路の变更数据类型、未分区表转为分区表

    玩转SQL Server复制回路の变更数据类型.未分区表转为分区表 复制的应用: 初级应用:读写分离.数据库备份 高级应用:搬迁大型数据库(跨机房).变更数据类型.未分区表转为分区表 京东的复制专家 ...

  5. 生成lua的静态库.动态库.lua.exe和luac.exe

    前些日子准备学习下关于lua coroutine更为强大的功能,然而发现根据lua 5.1.4版本来运行一段代码的话也会导致 "lua: attempt to yield across me ...

  6. Delphi DLL的创建、静态及动态调用

    转载:http://blog.csdn.net/welcome000yy/article/details/7905463 结合这篇博客:http://www.cnblogs.com/xumenger/ ...

  7. 3D touch 静态、动态设置及进入APP的跳转方式

    申明Quick Action有两种方式:静态和动态 静态是在info.plist文件中申明,动态则是在代码中注册,系统支持两者同时存在. -系统限制每个app最多显示4个快捷图标,包括静态和动态 静态 ...

  8. C/C++ 跨平台交叉编译、静态库/动态库编译、MinGW、Cygwin、CodeBlocks使用原理及链接参数选项

    目录 . 引言 . 交叉编译 . Cygwin简介 . 静态库编译及使用 . 动态库编译及使用 . MinGW简介 . CodeBlocks简介 0. 引言 UNIX是一个注册商标,是要满足一大堆条件 ...

  9. RT-Thread创建静态、动态线程

    RT-Thread 实时操作系统核心是一个高效的硬实时核心,它具备非常优异的实时性.稳定性.可剪裁性,当进行最小配置时,内核体积可以到 3k ROM 占用. 1k RAM 占用. RT-Thread ...

随机推荐

  1. 将war文件解压到指定目录

    问:如何将.war文件解压到指定目录? 答:jar命令没有这样的选项. eg:将abc.war解压到当前文件夹? 答:进入目标文件即abc.war文件所在的文件夹,按住shift键并在该文件夹空白处点 ...

  2. HBase(一): c#访问hbase组件开发

    HDP2.4安装系列介绍了通过ambari创建hbase集群的过程,但工作中一直采用.net的技术路线,如何去访问基于Java搞的Hbase呢? Hbase提供基于Java的本地API访问,同时扩展了 ...

  3. [jquery] jQuery jsTree V3.2.1 基础Demo

    引入对应的文件: <link rel="stylesheet" href="../dist/themes/default/style.min.css" / ...

  4. [转]谈谈select, iocp, epoll,kqueue及各种网络I/O复用机制

    参考原文:再谈select, iocp, epoll,kqueue及各种I/O复用机制 一.I/O模型概述 介绍几种常见的I/O模型及其区别,如下: blocking I/O nonblocking ...

  5. 面向对象设计模式--观察者模式(Observer)

    要点: 1.如何使用观察者模式: 对应使用这个模式的用户(main)来说,subject和observer这两个基类是不被关系的,在调用者(main)中只是有concreteSubject和concr ...

  6. IRedisClient 常用方法说明

    事实上,IRedisClient里面的很多方法,其实就是Redis的命令名.只要对Redis的命令熟悉一点就能够非常快速地理解和掌握这些方法,趁着现在对Redis不是特别了解,我也对着命令来了解一下这 ...

  7. 在Myeclipse中移除项目对Hibernate的支持

    在Myeclipse中移除项目对Hibernate的支持 在使用Hibernate框架进行开发时可能会遇到配置错误或者需要删除Hibernate支持的情况.下面就说一下如何彻底移除项目的Hiberna ...

  8. CPU GPU天梯图

    2014年2月

  9. Tomcat无法启动:org.apache.catalina.LifecycleException: Failed to start component 问题解决

    问题如下:需要使用到数据库mysql,于是将mysql-connector-java-5.1.30-bin.jar的数据库驱动复制到WEE-INF/lib目录下.点击运行,但是服务器无法启动. 控制台 ...

  10. Linux常见设备及相应/dev/xxx文件名、Mount Point、挂载点、Mount命令、fstab、挂载分区

    Linux 中的设备有2种类型:字符设备(无缓冲且只能顺序存取).块设备(有缓冲且可以随机存取).这些设备中,有些设备是对实际存在的物理硬件的抽象,而有些设备则是内核自身提供的功能(不依赖于特定的物理 ...