Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dinky
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangpeng
dinky
Commits
e1901b7b
Unverified
Commit
e1901b7b
authored
Nov 11, 2023
by
zhangyongtian
Committed by
GitHub
Nov 11, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[optimization][docs] add udfdemo (#2519)
parent
928fcff1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
90 additions
and
37 deletions
+90
-37
Java&Scala.md
...docs/administrator_guide/udf_develop/how_to/Java&Scala.md
+90
-37
No files found.
docs/docs/administrator_guide/udf_develop/how_to/Java&Scala.md
View file @
e1901b7b
...
@@ -3,54 +3,107 @@ sidebar_position: 1
...
@@ -3,54 +3,107 @@ sidebar_position: 1
id
:
java&scala_udf
id
:
java&scala_udf
title
:
Java&Scala UDF
title
:
Java&Scala UDF
---
---
## 作业创建
1.
选择java作业,并输入对应参数
```
text
sub
截取函数
com.test.SubFunction
```


> 此时从模板构建了代码,剩下函数逻辑填补即可。

### 初始环境准备
> 这里为了方便测试,返回一段字符串
1.
创建对应的表。
```
sql
CREATE
TABLE
`Order`
(
id
INT
,
product_id
INT
,
quantity
INT
,
order_time
TIMESTAMP
,
PRIMARY
KEY
(
id
)
NOT
ENFORCED
)
WITH
(
'connector'
=
'datagen'
,
'fields.id.kind'
=
'sequence'
,
'fields.id.start'
=
'1'
,
'fields.id.end'
=
'100000'
,
'fields.product_id.min'
=
'1'
,
'fields.product_id.max'
=
'100'
,
'rows-per-second'
=
'1'
);
2.
接下来创建一个
`FlinkSql`
作业
CREATE
TABLE
`Product`

(
id
INT
,
name
VARCHAR
,
price
DOUBLE
,
PRIMARY
KEY
(
id
)
NOT
ENFORCED
)
WITH
(
'connector'
=
'datagen'
,
'fields.id.min'
=
'1'
,
'fields.id.max'
=
'100'
,
'rows-per-second'
=
'1'
);
`
``
创建函数时,复制类名,以下为测试代码
在数据开发界面点击保存,并且检查成功没有问题一会,运行就会在 **Catalog** 界面看到创建的
``
`
Order
`
``
和
``
`
Product
`
``
表。
```
sql
create
temporary
function
sb_j
as
'com.test.SubFunction'
;

2. 执行查询任务。
``
`
sql
SELECT
o
.
id
,
p
.
name
,
o
.
order_time
FROM
`Order`
o
INNER
JOIN
`Product`
p
ON
o
.
product_id
=
p
.
id
;
`
``
CREATE
TABLE
sourceTable
(
点击调试以后得到下面的效果图。
id
int
,
java_c
string
)
WITH
(
'connector'
=
'datagen'
);
CREATE
TABLE
sinkTable

WITH
(
'connector'
=
'print'
)
LIKE
sourceTable
(
EXCLUDING
ALL
);
insert
into
sinkTable
select
id
,
sb_j
(
java_c
)
from
sourceTable
;
### 定义UDF函数
创建一个UDF函数
对应函数的类名。
```
java
com.test.SubFunction
```
```


> 选择执行模式,我这里采用 `pre-job` 进行演示
下面是udf函数的代码。
```
java
package com.test;
3.
执行,结果查看
import org.apache.flink.table.functions.ScalarFunction;
public class SubFunction extends ScalarFunction {
public String eval(String s) {
if(null==s){
return s;
}
return s.substring(0,3);
}
}
```
如下图所示。

### 函数的使用
定义一个flinksql任务,执行下面代码。
```
sql
create temporary function sb_j as 'com.test.SubFunction';
SELECT o.id, sb_j(p.name), o.order_time
FROM
`Order`
o
INNER JOIN
`Product`
p
ON o.product_id = p.id;
```

看到如下那么表示执行成功了。
查看
`Taskmanager`
输出,正常输出,验证成功
## 动图演示


\ No newline at end of file
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment