zoukankan      html  css  js  c++  java
  • Hive分区与桶表

    1、分区

    在hive中使用select查询一般会扫描整个表的内容,从而降低降低查询的效率。引入分区的概念,使得查询时只扫描表中关心的部分数据。

    一个表中可以有一个或多个分区,每个分区以文件夹的形式单独存在表文件夹的目录下。

    1.1分区建表分为单分区和双分区建表:

    单分区建表语句:create table sample_table (id int, value string) partitioned by (age int) row format delimited fields terminated by ',' stored as textfile;;表中有id,value,age三列,以age分区

    双分区建表语句:create table sample_table (id int, value string) partitioned by (age int, sex string) row format delimited fields terminated by ',' stored as textfile;;表中有id,value,age,sex四列,按照age和sex分区

    【注:set hive.cli.print.current.db=true查看当前是什么数据库

    row format delimited通过新的行将记录分开

    fields terminated by ','各列之间以逗号隔开

    stored as textfile存储为一个文本文件】

    1.2添加数据:

    load data local inpath ‘路径’ overwrite into table 表名 partition (分区名=’某值’)

    【注:overwrite意味着表中原来的数据会被删除】

    2、桶(Bucket)

    分桶其实就是把大表化成了“小表”,然后 Map-Side Join 解决之,这是用来解决大表与小表之间的连接问题。将桶中的数据按某列进行排序会提高查询效率。

    2.1创建带桶的table:

    Create table 表名(id int,name string) clustered by (id) sorted by(name) into 4 buckets row format delimited fields terminated by ' ' stored as textfile; ;

    2.2设置环境变量:

    set hive.enforce.bucketing = true,使得Hive 就知道用表定义中声明的数量来创建桶

    2.3插入数据:

    insert table 桶表名 select * from 表名; 

  • 相关阅读:
    ASP+Access UTF-8 网页乱码问题解决办法
    使用PowerDesigner生成Access数据库
    crontab详解
    Pending Statistics
    Recovery with Incremental Backups
    ASP的Global.asa使用说明
    cocos基础教程(5)数据结构介绍之cocos2d::Map<K,V>
    cocos基础教程(5)数据结构介绍之cocos2d::Vector
    cocos基础教程(4)基础概念介绍
    cocos进阶教程(1)Lua调用自定义C++类和函数的最佳实践
  • 原文地址:https://www.cnblogs.com/chenyaling/p/5575386.html
Copyright © 2011-2022 走看看