Skip to content

Commit eeccb34

Browse files
authored
增加两个 udf: Exploded StrToArray (#6)
Add `Exploded` and `StrToArray` UDFs
1 parent 8d1e12a commit eeccb34

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Flink 示例程序包(自定义函数 UDF、自定义 Source 和 Sink 等)
1616
3. PrintChar
1717
4. EnhancedSplitIndex
1818
5. ArrayAgg (示例参考 IntArrayAgg, RowArrayAgg1)
19+
6. Exploded
20+
7. StrToArray
1921

2022

2123
## Sink 示例
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.tencent.cloud.oceanus.udf;
2+
3+
import org.apache.flink.table.annotation.DataTypeHint;
4+
import org.apache.flink.table.annotation.FunctionHint;
5+
import org.apache.flink.table.functions.TableFunction;
6+
import org.apache.flink.types.Row;
7+
8+
/**
9+
* 生成多行整数.
10+
*
11+
* <p>SQL 代码声明方式: CREATE TEMPORARY SYSTEM FUNCTION Exploded AS
12+
* 'com.tencent.cloud.oceanus.udf.Exploded' LANGUAGE JAVA;
13+
*
14+
* <p>常用于数据倾斜时的数据打散,把小表数据复制 n 份。例如: CREATE VIEW v_small AS SELECT * FROM small, LATERAL
15+
* TABLE(Exploded(50)) as t(`random_index`);
16+
*/
17+
@FunctionHint(output = @DataTypeHint("ROW<exploded_id int>"))
18+
public class Exploded extends TableFunction<Row> {
19+
public void eval(Integer n) {
20+
for (int i = 0; i < n; i++) {
21+
collect(Row.of(i));
22+
}
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.tencent.cloud.oceanus.udf;
2+
3+
import org.apache.flink.table.functions.ScalarFunction;
4+
5+
import org.apache.commons.lang3.StringUtils;
6+
7+
/**
8+
* 将 str 表示的字符串以 separator 指定的分隔符拆分,返回值拆分后的数组.
9+
*
10+
* <p>SQL 代码声明方式::CREATE TEMPORARY SYSTEM FUNCTION STR_TO_ARRAY AS
11+
* 'com.tencent.cloud.oceanus.udf.StrToArray' LANGUAGE JAVA;
12+
*
13+
* <p>注意相邻的分割符拆分出空字符串,例如 SELECT STR_TO_ARRAY('a,b,,c,d', ','); 返回 [a, b, , c, d].
14+
*/
15+
public class StrToArray extends ScalarFunction {
16+
public String[] eval(String str, String separator) {
17+
if (str == null) {
18+
return null;
19+
}
20+
return StringUtils.splitByWholeSeparatorPreserveAllTokens(str, separator);
21+
}
22+
}

0 commit comments

Comments
 (0)