Hive表分区

新建一个有一个分区dt的表baseinfo。

hive> create table baseinfo(
    > id int,
    > name string
    > )partitioned by (dt string) row format delimited fields terminated by '\t' lines terminated by '\n' stored as textfile; 
OK
Time taken: 0.132 seconds

在linux下面建立一个文件/baseinfo.txt,文件内容为

1    qiu

2    liu

将文件中的内容导入baseinfo表中,设置分区的值为当前日期'2014-03-03'。

hive> load data local inpath '/baseinfo.txt' overwrite into table baseinfo partition(dt = '2014-03-03');
Copying data from file:/baseinfo.txt
Copying file: file:/baseinfo.txt
Loading data to table hbmsdb.baseinfo partition (dt=2014-03-03)
Deleted hdfs://hadoop00:9000/user/hive/warehous/hbmsdb.db/baseinfo/dt=2014-03-03
OK
Time taken: 0.442 seconds

在linux下面建立一个文件/baseinfo2.txt,文件内容为

3 zheng

4 yang

将文件中的内容导入baseinfo表中,设置分区的值为当期日期‘2014-03-04’。

hive> load data local inpath '/baseinfo.txt' overwrite into table baseinfo partition(dt = '2014-03-04');                                                      
Copying data from file:/baseinfo.txt
Copying file: file:/baseinfo.txt
Loading data to table hbmsdb.baseinfo partition (dt=2014-03-04)
OK
Time taken: 0.189 seconds

查看分区

hive> show partitions baseinfo;                                                                         
OK
dt=2014-03-03
dt=2014-03-04
Time taken: 0.054 seconds

 查看数据

hive> select * from baseinfo;
OK
1       qiu     2014-03-03
2       liu     2014-03-03
3       zheng   2014-03-04
4       yang    2014-03-04
Time taken: 0.141 seconds
hive> select * from baseinfo where dt = '2014-03-03';
OK
1       qiu    2014-03-03
2       liu    2014-03-03
Time taken: 0.149 seconds


通过查询将内容保存在本地文件系统上

hive> insert overwrite  local directory '/home/hive' select * from baseinfo;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive中,分区(Partition)是一种将数据按照指定的列进行逻辑划分和存储的方式。通过将数据按照某个列的值进行分区,可以提高查询效率和数据管理的灵活性。 具体来说,以下是关于Hive分区的一些概念和特点: 1. 分区列(Partition Column):分区列是中用于进行分区的列。通常选择具有高基数(Distinct Value)的列作为分区列,以便更好地划分数据并提高查询性能。 2. 分区目录(Partition Directory):每个分区都会对应一个独立的目录,用于存储该分区的数据文件。分区目录的命名通常基于分区列的值,以便更好地组织和管理数据。 3. 动态分区(Dynamic Partition):Hive支持动态分区,在插入数据时根据数据中的列值动态创建和管理分区。这允许在插入数据时自动创建新的分区目录。 4. 静态分区(Static Partition):与动态分区相反,静态分区需要在创建时明确地定义所有分区,并提前创建对应的分区目录。 5. 多级分区(Multi-level Partitioning):Hive还支持多级分区,即在一个中使用多个列进行分区。这样可以更细粒度地划分数据,提供更灵活的查询和管理能力。 通过使用分区,可以使Hive在处理大规模数据时更高效地执行查询操作。例如,当查询仅涉及特定分区时,Hive可以仅加载相关分区的数据,而不必加载整个的数据。此外,分区还可以帮助优化数据存储和管理,以及提供更灵活的数据查询和过滤功能。 需要注意的是,在设计和使用分区时,需要考虑数据分布的均匀性、查询模式、分区列的选择等因素,以确保最佳的性能和使用效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值