Hive静态分区(一级&二级)&动态分区
1.创建分区表
1 | hive> create table testpartition(ordernumber string,event_time string) |

2.在/opt/modules/hive-1.2.1/demo目录下创建order_created.txt文件,并将数据加载进表中
1 | [root@node1 demo]# vi order_created.txt |

1 | 107030072674882015-06-01 06:01:12.334+01 |

1 | hive> load data local inpath '/opt/modules/hive-1.2.1/demo/order_created.txt' |

3.查询testpartition表中的全部数据
1 | select * 操作不执行mapreduce |
1 | hive> select * from testpartition; |

4.查看testpartition表结构
1 | hive> desc formatted testpartition; |

5.加载HDFS数据到分区表中
5.1.创建order_created2.txt文件,并将文件上传到HDFS文件系统上
1 | [root@node1 demo]# vi order_created2.txt |

1 | 107030072674882015-07-01 06:01:12.334+01 |

5.2.创建HDFS文件系统上的目录
1 | [root@node1 hadoop-2.5.1]# ./bin/hdfs dfs -mkdir -p |
5.3.将order_created2.txt文件上传到HDFS文件系统上
1 | [root@node1 hadoop-2.5.1]# ./bin/hdfs dfs -put /opt/modules/hive-1.2.1/demo/order_created2.txt |
5.4.查询testpartition表中数据,发现只有2015-06的记录,并没有2015-07的记录
1 | hive> select * from testpartition; |

5.5.将手工维护的分区信息刷新hive元数据中(mysql中)
1 | hive> msck repair table testpartition; |

5.6.重新查询testpartition表中的数据,发现有2015-07的记录了
1 | hive> select * from testpartition; |

6.insert/overwrite方式去添加分区表的数据
6.1.创建表
1 | hive> create table testpartition2(ordername string,event_time string) |

6.2.导入数据到testpartition2表中
1 | hive> load data local inpath '/opt/modules/hive-1.2.1/demo/order_created.txt' overwrite into table testpartition2; |

6.3.查询testpartition2表的数据
1 | hive> select * from testpartition2; |

1 | 再次执行后,order_created+partition(event_month='2014-07')记录会翻倍 |
本文作者 : Matrix
原文链接 : https://matrixsparse.github.io/2016/02/12/Hive静态分区(一级&两级) & 动态分区/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
知识 & 情怀 | 二者兼得