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 表名; 

  • 相关阅读:
    15.RDD 创建内幕解析
    14.spark RDD解密
    我的读书笔记-《异类》
    深入解析单例线程安全问题的迷思
    一个关于数学归纳法的悖论问题-续
    一个关于数学归纳法的悖论问题
    简易解说拉格朗日对偶(Lagrange duality)(转载)
    unity3d NavMeshAgent 寻路画线/画路径
    unity3d easytouch计算摇杆旋转角度以及摇杆八方向控制角色
    unity3d 摄像机跟随角色时被物体遮挡解决方案
  • 原文地址:https://www.cnblogs.com/chenyaling/p/5575386.html
Copyright © 2011-2022 走看看