Git Product home page Git Product logo

hive-bitmap-udf's Introduction

hive-bitmap-udf

在hive中使用Roaring64Bitmap实现精确去重功能 主要目的:

  1. 提升 hive 中精确去重性能,代替hive 中的 count(distinct uuid);
  2. 节省 hive 存储 ,使用 bitmap 对数据压缩 ,减少了存储成本;
  3. 提供在 hive 中 bitmap 的灵活运算 ,比如:交集、并集、差集运算 ,计算后的 bitmap 也可以直接写入 hive;

1. 在hive中创建UDF

add jar hdfs://node:9000/hive-bitmap-udf.jar;

CREATE TEMPORARY FUNCTION to_bitmap AS 'com.hive.bitmap.udf.ToBitmapUDAF';
CREATE TEMPORARY FUNCTION bitmap_union AS 'com.hive.bitmap.udf.BitmapUnionUDAF';

CREATE TEMPORARY FUNCTION bitmap_count AS 'com.hive.bitmap.udf.BitmapCountUDF';
CREATE TEMPORARY FUNCTION bitmap_and AS 'com.hive.bitmap.udf.BitmapAndUDF';
CREATE TEMPORARY FUNCTION bitmap_or AS 'com.hive.bitmap.udf.BitmapOrUDF';
CREATE TEMPORARY FUNCTION bitmap_xor AS 'com.hive.bitmap.udf.BitmapXorUDF';

2. UDF说明

UDF 描述 案例 结果类型
to_bitmap 将num(int或bigint) 转化为 bitmap to_bitmap(num) bitmap
bitmap_union 多个bitmap合并为一个bitmap(并集) bitmap_union(bitmap) bitmap
bitmap_count 计算bitmap中存储的num个数 bitmap_count(bitmap) long
bitmap_and 计算两个bitmap交集 bitmap_and(bitmap1,bitmap2) bitmap
bitmap_or 计算两个bitmap并集 bitmap_or(bitmap1,bitmap2) bitmap
bitmap_xor 计算两个bitmap差集 bitmap_xor(bitmap1,bitmap2) bitmap

3. 在 hive 中创建 bitmap 类型表,导入数据并查询

CREATE TABLE IF NOT EXISTS `hive_bitmap_table`
( 
    k      int      comment 'id',
    bitmap binary   comment 'bitmap'
) comment 'hive bitmap 类型表' 
STORED AS ORC;

-- 数据写入
insert into table  hive_bitmap_table select  1 as id,to_bitmap(1) as bitmap;
insert into table hive_bitmap_table select  2 as id,to_bitmap(2) as bitmap;

-- 查询

select bitmap_union(bitmap) from hive_bitmap_table;
select bitmap_count(bitmap_union(bitmap)) from hive_bitmap_table;

4. 在 hive 中使用 bitmap 实现精确去重

CREATE TABLE IF NOT EXISTS `hive_table`
( 
    k      int      comment 'id',
    uuid   bigint   comment '用户id'
) comment 'hive 普通类型表' 
STORED AS ORC;

-- 普通查询(计算去重人数)

select count(distinct uuid) from hive_table;

-- bitmap查询(计算去重人数)

select bitmap_count(to_bitmap(uuid)) from hive_table;

hive-bitmap-udf's People

Contributors

lihuigang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.