Hive UDF

Hive Bitmap UDF 提供了在 hive 表中生成 bitmap 、bitmap 运算等 UDF,Hive 中的 bitmap 与 Doris bitmap 完全一致 ,Hive 中的 bitmap 可以通过 spark bitmap load 导入 doris

主要目的:

  1. 减少数据导入 doris 时间 , 除去了构建字典、bitmap 预聚合等流程;
  2. 节省 hive 存储 ,使用 bitmap 对数据压缩 ,减少了存储成本;
  3. 提供在 hive 中 bitmap 的灵活运算 ,比如:交集、并集、差集运算 ,计算后的 bitmap 也可以直接导入 doris;

使用方法

在 Hive 中创建 Bitmap 类型表

  1. -- 例子:创建 Hive Bitmap
  2. CREATE TABLE IF NOT EXISTS `hive_bitmap_table`(
  3. `k1` int COMMENT '',
  4. `k2` String COMMENT '',
  5. `k3` String COMMENT '',
  6. `uuid` binary COMMENT 'bitmap'
  7. ) comment 'comment'
  8. -- 例子:创建普通 Hive
  9. CREATE TABLE IF NOT EXISTS `hive_table`(
  10. `k1` int COMMENT '',
  11. `k2` String COMMENT '',
  12. `k3` String COMMENT '',
  13. `uuid` int COMMENT ''
  14. ) comment 'comment'

Hive Bitmap UDF 使用:

Hive Bitmap UDF 需要在 Hive/Spark 中使用,首先需要编译fe得到hive-udf-jar-with-dependencies.jar。 编译准备工作:如果进行过ldb源码编译可直接编译fe,如果没有进行过ldb源码编译,则需要手动安装thrift,可参考:FE开发环境搭建 中的编译与安装

  1. --clone doris源码
  2. git clone https://github.com/apache/doris.git
  3. --安装thrift
  4. --进入fe目录
  5. cd fe
  6. --执行maven打包命令(fe的子module会全部打包)
  7. mvn package -Dmaven.test.skip=true
  8. --也可以只打hive-udf module
  9. mvn package -pl hive-udf -am -Dmaven.test.skip=true

打包编译完成进入hive-udf目录会有target目录,里面就会有打包完成的hive-udf-jar-with-dependencies.jar包

  1. -- 加载hive bitmap udf jar (需要将编译好的 hive-udf jar 包上传至 HDFS)
  2. add jar hdfs://node:9001/hive-udf-jar-with-dependencies.jar;
  3. -- 创建UDAF函数
  4. create temporary function to_bitmap as 'org.apache.doris.udf.ToBitmapUDAF' USING JAR 'hdfs://node:9001/hive-udf-jar-with-dependencies.jar';
  5. create temporary function bitmap_union as 'org.apache.doris.udf.BitmapUnionUDAF' USING JAR 'hdfs://node:9001/hive-udf-jar-with-dependencies.jar';
  6. -- 创建UDF函数
  7. create temporary function bitmap_count as 'org.apache.doris.udf.BitmapCountUDF' USING JAR 'hdfs://node:9001/hive-udf-jar-with-dependencies.jar';
  8. create temporary function bitmap_and as 'org.apache.doris.udf.BitmapAndUDF' USING JAR 'hdfs://node:9001/hive-udf-jar-with-dependencies.jar';
  9. create temporary function bitmap_or as 'org.apache.doris.udf.BitmapOrUDF' USING JAR 'hdfs://node:9001/hive-udf-jar-with-dependencies.jar';
  10. create temporary function bitmap_xor as 'org.apache.doris.udf.BitmapXorUDF' USING JAR 'hdfs://node:9001/hive-udf-jar-with-dependencies.jar';
  11. -- 例子:通过 to_bitmap 生成 bitmap 写入 Hive Bitmap
  12. insert into hive_bitmap_table
  13. select
  14. k1,
  15. k2,
  16. k3,
  17. to_bitmap(uuid) as uuid
  18. from
  19. hive_table
  20. group by
  21. k1,
  22. k2,
  23. k3
  24. -- 例子:bitmap_count 计算 bitmap 中元素个数
  25. select k1,k2,k3,bitmap_count(uuid) from hive_bitmap_table
  26. -- 例子:bitmap_union 用于计算分组后的 bitmap 并集
  27. select k1,bitmap_union(uuid) from hive_bitmap_table group by k1

Hive Bitmap UDF 说明

Hive bitmap 导入 doris

详见: Spark Load -> 基本操作 -> 创建导入 (示例3:上游数据源是hive binary类型情况)