HIVE创建分区表

一、创建静态分区表(SP static )

1.启动集群

2.进入hive

3.创建test1118数据库并使用

HIVE创建分区表

4.创建 t1 表

create table t1(
 c1 string,
 c2 string
);

HIVE创建分区表

5.查看表结构:

HIVE创建分区表

6.创建t2表

create table t2(
 c1 string
) partitioned by ( c2 string ) row format delimited fields terminated by ',';

partitioned by ( c2 string ) # 创建分区 c1跟c2都是字段,但是创建的时候不能写在t2里面,只能写在分区里面(同时select查询的时候,c2的字段也要写在最后面)

HIVE创建分区表

7.查看表结构:

HIVE创建分区表

8.上传数据到 t2 表的 c2 分区

要加载数据到分区表,只需在原来的加载数据的语句上增加partition关键字,同时指定分区的字段值即可。

注意:当你退出过hive后,再次进入hive,不要忘记使用了哪个database,我这里使用的是test1118数据库

load data inpath '/lyh/bbb.txt' into table t2 partition (c2='a');

HIVE创建分区表

9.需要进行修复,输入以下命令

 msck repair table t2;

HIVE创建分区表

10.查看内容

select * from t2;

HIVE创建分区表

11.添加分区b

先确保集群上有a.txt这个文件退出hive

hdfs dfs -mkdir /user/hive/warehouse/test1118.db/t2/b

HIVE创建分区表

hdfs dfs -put /a.txt /user/hive/warehouse/test1118.db/t2/b

HIVE创建分区表

进入hive 后使用命令

use test1118;
load data inpath '/lyh/a.txt' into table t2 partition(c2='b');

HIVE创建分区表

但是这个时候是查看不了的,需要进行修复,输入以下命令

msck repair table t1;

HIVE创建分区表

现在修复后可以进行查看

select * from t2;

HIVE创建分区表

层次一次建好 分区可以逐个添加

create table t3 (
id int,
name string
)partitioned by (year string,month string)
row format delimited fields terminated by ',';

HIVE创建分区表

load data inpath '/lyh/a.txt' into table t3 partition (year='2022',month='11');

HIVE创建分区表

select * from t3 where year='2022';

HIVE创建分区表

select * from t3 where year='2022' and month='11';

HIVE创建分区表

hdfs dfs -mkdir /user/hive/warehouse/test1118.db/t3/year
hdfs dfs -mkdir /user/hive/warehouse/test1118.db/t3/year/month 
hdfs dfs -mkdir -p /user/hive/warehouse/test1118.db/t3/2023

这里的-p和linux意义不同

退出hive

hdfs dfs -mkdir -p /user/hive/warehouse/test1118.db/t3/2023/11
hdfs dfs -put /a.txt /user/hive/warehouse/test1118.db/t3/2023/11

进入hive,然后输入 use dest1118;

msck repair table t3;

HIVE创建分区表

select * from t3;

HIVE创建分区表

添加分区

alter table t3 add if not exists partition(year='2023',month='11');

HIVE创建分区表

HIVE创建分区表

退出hive

hdfs dfs -mv /d.txt /user/hive/warehouse/test1118.db/t3

进入hive,use test1118;

select * from t3;

HIVE创建分区表

二、创建动态分区表(DP dynamic)

开启动态分区

set hive.exec.dynamic.partition=true

HIVE创建分区表

…=false 关闭

create database test1125;

HIVE创建分区表

use test1125;

HIVE创建分区表

create table t1(
c1 string
) partitioned by(c2 string)
row format delimited fields terminated by ',';

删除表

drop table t1;

HIVE创建分区表

create table t2(
id int,
name string
) partitioned by(year string,month string)
row format delimited fields terminated by ',';
create table t1(
title string,
author_name string,
dynasty string,
c1 string,
c2 string,
c3 string,
c4 string
) row format delimited fields terminated by ',';

HIVE创建分区表

create table t1(
title string,
author_name string,
dynasty string,
c1 string,
c2 string,
c3 string,
c4 string
)partitioned by ( c5 string ) row format delimited fields terminated by ',';

HIVE创建分区表

创建一个a.txt文本,内容为

秋夜寄丘二十二员外,韦应物,唐代,怀君属秋夜,散步咏凉天,空山松子落,幽人应未眠

HIVE创建分区表

上传到集群

hdfs dfs -put a.txt /lyh

HIVE创建分区表

导入文本到 t1 表里

use test1125;
load data inpath '/lyh/a.txt' into table t1 partition(c5='a');

HIVE创建分区表

select * from t1;

HIVE创建分区表

/***
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 */

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/d0b07fa10a.html