hive学习--建分区表

29 篇文章 0 订阅
17 篇文章 0 订阅

创建内部表

创建表语句:create table [if not exists] linuxidc_hive_log (  num string,  sn string,userkey string);

执行后:会在库目录下生成表目录,默认分隔符为^A。

create table [if not exists] linuxidc_hive_log (  num string,  sn string,userkey string)

row format delimited fields terminated by ',';    指定表分隔符为 “,”。

删除表: drop table 表名;

会删除表信息,

(内部表)还会从HDFS中删除表目录。-----引入内部表,外部表

创建外部表

外部表:可以任意指定映射目录(表目录路的路径)。即不会对数据进行迁移。

create external table [if not exists] linuxidc_hive_log (  num string,  sn string,userkey string)

row format delimited fields terminated by ','   --指定表分隔符为 “,”。

location '/user/bushu07/flume/aaaa' ; -- 指定映射目录为,不会再迁移到/hive/warehouse下

创建分区表:方便查询

分区避免查询数据进行HDFS全盘扫描,一个表分区数量不要太多(导致每一个分区创建一个task,每一个task是一个jvm实例),没个分区中的文件尽量大(与HDFS数据块保持一致,默认128M)

按照某一个标记分区。比如地址、时间等

create external table [if not exists] linuxidc_hive_log (  num string,  sn string,userkey string)

partitioned by (day string,city string)   --按照day进行分区,自动创建分区子目录,在day目录下,创建city分区目录

row format delimited fields terminated by ', '

location '/user/bushu07/flume/aaaa' ;

对应导入分区数据时:

load data location inpath '/home/hadoop/logs/aaa.log' into table t_name partiton(分区字段名='分区字段内容');

注意:分区字段不能是表定义中的已经存在的字段

查看分区表:select ..........from 表名 where 分区字段名=分区字段内容

创建两个表结构一样的表:create table 新表名 like 旧表名;

建表时插入数据:create table 新表名 as select..........from 表名 where 判断条件(注意:不能建外部表

剪切hive数据目录
    hadoop distcp hdfs://ping1:9000/hive/warehouse/mydb.db/tabuser/2014/11/small_user.csv hdfs://ping1:9000/2014/11 

将新修改的数据目录和hive表的分区完成映射
    hive> alter table tabuser partition(year=2014,month=11) set location 'hdfs://ping1:9000/ 2014/11'
    可以删除原有目录:
    hive>dfs -rmr hdfs://ping1:9000/hive/warehouse/mydb.db/tabuser;

查看表已有分区信息
    hive> show partitions 表名;    
    hive> desc extends tabuser;

查看分区的location
    hive> desc extended tabuser partition(year=2014,month=11); 
    注意:
     如果没有partition子句,显示显示中location是table的存储路径,一旦添加
     了该语句,则location中显示分区存储路径;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值