zoukankan      html  css  js  c++  java
  • hive中的分桶表

    桶表也是一种用于优化查询而设计的表类型。
    创建通表时,指定桶的个数、分桶的依据字段,hive就可以自动将数据分桶存储。
    查询时只需要遍历一个桶里的数据,或者遍历部分桶,这样就提高了查询效率

    ------创建订单表
    create table user_leads
    (
    leads_id string,
    user_id string,
    user_id string,
    user_phone string,
    user_name string,
    create_time string
    )
    clustered by (user_id)
    sorted by(leads_id)
    into 10 buckets
    row format delimited fields terminated by ' '
    stored as textfile;


    clustered by是指根据 user_id 的值进行哈希后模除分桶个数,
    根据得到的结果,确定这行数据分入哪个桶中,这样的分法,
    可以确保相同 user_id 的数据放入同一个桶中。
    而经销商的订单数据,大部分是根据user_id进行查询的。
    这样大部分情况下是只需要查询一个桶中的数据就可以了。
    sorted by 是指定桶中的数据以哪个字段进行排序,排序的好处是,在join操作时能获得很高的效率。
    into 10 buckets是指定一共分10个桶
    在HDFS上存储时,一个桶存入一个文件中,这样根据user_id进行查询时,可以快速确定数据存在于哪个桶中,而只遍历一个桶可以提供查询效率


    加载到分桶表
    ------先创建普通临时表
    create table user_leads_tmp
    (
    leads_id string,
    user_id string,
    user_id string,
    user_phone string,
    user_name string,
    create_time string
    )
    row format delimited fields terminated by ','
    stored as textfile;
    ------数据载入临时表
    load data local inpath '/home/hadoop/lead.txt' overwrite into table user_leads_tmp;
    ------导入分桶表
    set hive.enforce.bucketing = true; -- 为true就是设置为启用分桶。
    insert overwrite table user_leads select * from user_leads_tmp;

    drop table sospdm.tmp_yinfei_test;
    create table sospdm.tmp_yinfei_test
    (
    id string,cust_num string
    )partitioned by (statis_date string) clustered by (id) sorted by (id) into 5 buckets
    row format delimited fields terminated by ','
    ;
    1,cust_num_1
    2,cust_num_2
    3,cust_num_3
    4,cust_num_4
    5,cust_num_5
    6,cust_num_6
    7,cust_num_7
    8,cust_num_8
    9,cust_num_9

    drop table sospdm.tmp_yinfei_test_tmp;
    create table sospdm.tmp_yinfei_test_tmp
    (
    id string,cust_num string
    )partitioned by (statis_date string)
    row format delimited fields terminated by ','
    ;

    load data local inpath '/home/sospdm/yf/test.txt' overwrite into table tmp_yinfei_test_tmp partition (statis_date='20190408');

    set hive.enforce.bucketing = true;
    insert overwrite table tmp_yinfei_test partition(statis_date='20190408') select id,cust_num from tmp_yinfei_test_tmp;

  • 相关阅读:
    实验吧_简单的sql注入_1、2、3
    实验吧_天下武功唯快不破&让我进去(哈希长度拓展攻击)
    实验吧_密码忘记了(vim编辑器+代码审计)&天网管理系统(php弱比较+反序列化)
    实验吧_Guess Next Session&Once More(代码审计)
    实验吧_NSCTF web200&FALSE(代码审计)
    实验吧_程序逻辑问题(代码审计)&上传绕过
    实验吧_貌似有点难(php代码审计)&头有点大
    网络安全实验室_上传关writeup
    php文件包含漏洞(input与filter)
    我为什么要写LeetCode的博客?
  • 原文地址:https://www.cnblogs.com/yin-fei/p/10752004.html
Copyright © 2011-2022 走看看