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 类型表

2. -- 例子:创建 Hive Bitmap 表
3. CREATE TABLE IF NOT EXISTS `hive_bitmap_table`(
4. `k1`   int       COMMENT '',
5. `k2`   String    COMMENT '',
6. `k3`   String    COMMENT '',
7. `uuid` binary    COMMENT 'bitmap'
8. ) comment  'comment'

10. -- 例子:创建普通 Hive 表
11. CREATE TABLE IF NOT EXISTS `hive_table`(
12. `k1`   int       COMMENT '',
13. `k2`   String    COMMENT '',
14. `k3`   String    COMMENT '',
15. `uuid` int       COMMENT ''
16. ) 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. cd doris
4. git submodule update --init --recursive
5. --安装thrift
6. --进入fe目录
7. cd fe
8. --执行maven打包命令(fe的子module会全部打包)
9. mvn package -Dmaven.test.skip=true
10. --也可以只打hive-udf module
11. mvn package -pl hive-udf -am -Dmaven.test.skip=true

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

2. -- 加载hive bitmap udf jar包  (需要将编译好的 hive-udf jar 包上传至 HDFS)
3. add jar hdfs://node:9001/hive-udf-jar-with-dependencies.jar;

5. -- 创建UDAF函数
6. create temporary function to_bitmap as 'org.apache.doris.udf.ToBitmapUDAF' USING JAR 'hdfs://node:9001/hive-udf-jar-with-dependencies.jar';
7. create temporary function bitmap_union as 'org.apache.doris.udf.BitmapUnionUDAF' USING JAR 'hdfs://node:9001/hive-udf-jar-with-dependencies.jar';

9. -- 创建UDF函数
10. create temporary function bitmap_count as 'org.apache.doris.udf.BitmapCountUDF' USING JAR 'hdfs://node:9001/hive-udf-jar-with-dependencies.jar';
11. create temporary function bitmap_and as 'org.apache.doris.udf.BitmapAndUDF' USING JAR 'hdfs://node:9001/hive-udf-jar-with-dependencies.jar';
12. create temporary function bitmap_or as 'org.apache.doris.udf.BitmapOrUDF' USING JAR 'hdfs://node:9001/hive-udf-jar-with-dependencies.jar';
13. create temporary function bitmap_xor as 'org.apache.doris.udf.BitmapXorUDF' USING JAR 'hdfs://node:9001/hive-udf-jar-with-dependencies.jar';

15. -- 例子:通过 to_bitmap 生成 bitmap 写入 Hive Bitmap 表
16. insert into hive_bitmap_table
17. select
18. k1,
19. k2,
20. k3,
21. to_bitmap(uuid) as uuid
22. from
23. hive_table
24. group by
25. k1,
26. k2,
27. k3

29. -- 例子:bitmap_count 计算 bitmap 中元素个数
30. select k1,k2,k3,bitmap_count(uuid) from hive_bitmap_table

32. -- 例子:bitmap_union 用于计算分组后的 bitmap 并集
33. select k1,bitmap_union(uuid) from hive_bitmap_table group by k1

Hive Bitmap UDF 说明

Hive bitmap 导入 doris

SinceVersion 2.0.2

方法一:Catalog (推荐)

创建 Hive 表指定为 TEXT 格式,此时,对于 Binary 类型,Hive 会以 bash64 编码的字符串形式保存,此时可以通过 Hive Catalog 的形式,直接将位图数据通过 bitmap_from_bash64 函数插入到 Doris 内部。

以下是一个完整的例子:

  1. 在 Hive 中创建 Hive 表
1. CREATE TABLE IF NOT EXISTS `test`.`hive_bitmap_table`(
2. `k1`   int       COMMENT '',
3. `k2`   String    COMMENT '',
4. `k3`   String    COMMENT '',
5. `uuid` binary    COMMENT 'bitmap'
6. ) stored as textfile
  1. 在 Doris 中创建 Catalog
1. CREATE CATALOG hive PROPERTIES (
2. 'type'='hms',
3. 'hive.metastore.uris' = 'thrift://127.0.0.1:9083'
4. );
  1. 创建 Doris 内表
1. CREATE TABLE IF NOT EXISTS `test`.`doris_bitmap_table`(
2. `k1`   int                   COMMENT '',
3. `k2`   String                COMMENT '',
4. `k3`   String                COMMENT '',
5. `uuid` BITMAP  BITMAP_UNION  COMMENT 'bitmap'
6. )
7. AGGREGATE KEY(k1, k2, k3)
8. DISTRIBUTED BY HASH(`user_id`) BUCKETS 1
9. PROPERTIES (
10. "replication_allocation" = "tag.location.default: 1"
11. );
  1. 从 Hive 插入数据到 Doris 中
1. insert into doris_bitmap_table select k1, k2, k3, bitmap_from_base64(uuid) from hive.test.hive_bitmap_table;

方法二:Spark Load

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