Unverified Commit 435bde76 authored by Zzm0809's avatar Zzm0809 Committed by GitHub

refactor globalvar (#2625)

* refactor-globalvar * Spotless Apply * refactor-globalvar * fix CatalogueServiceImpl delete npe bug * fix CatalogueServiceImpl delete npe bug --------- Co-authored-by: 's avatarZzm0809 <Zzm0809@users.noreply.github.com>
parent 8079fd6f
...@@ -400,11 +400,11 @@ ...@@ -400,11 +400,11 @@
<configuration> <configuration>
<classesDirectory>target/classes/</classesDirectory> <classesDirectory>target/classes/</classesDirectory>
<excludes> <excludes>
<!--注意这玩意默认从编译结果目录开始算目录结构-->
<exclude>/application*.yml</exclude> <exclude>/application*.yml</exclude>
<exclude>/mybatis*.xml</exclude> <exclude>/mybatis*.xml</exclude>
<exclude>/DinkyFlinkDockerfile</exclude> <exclude>/DinkyFlinkDockerfile</exclude>
<exclude>/FlinkConfClass</exclude> <exclude>/dinky-loader/FlinkConfClass</exclude>
<exclude>/dinky-loader/ExpressionVariableClass</exclude>
</excludes> </excludes>
</configuration> </configuration>
<executions> <executions>
......
...@@ -45,7 +45,6 @@ import java.io.InputStreamReader; ...@@ -45,7 +45,6 @@ import java.io.InputStreamReader;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
...@@ -100,8 +99,7 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata ...@@ -100,8 +99,7 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata
} }
List<Catalogue> returnList = new ArrayList<>(); List<Catalogue> returnList = new ArrayList<>();
for (Iterator<Catalogue> iterator = catalogueList.iterator(); iterator.hasNext(); ) { for (Catalogue catalogue : catalogueList) {
Catalogue catalogue = iterator.next();
// get all child catalogue of parent catalogue id , the 0 is root catalogue // get all child catalogue of parent catalogue id , the 0 is root catalogue
if (catalogue.getParentId() == 0) { if (catalogue.getParentId() == 0) {
recursionBuildCatalogueAndChildren(catalogueList, catalogue); recursionBuildCatalogueAndChildren(catalogueList, catalogue);
...@@ -147,7 +145,7 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata ...@@ -147,7 +145,7 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata
* @return * @return
*/ */
private boolean hasChild(List<Catalogue> list, Catalogue catalogue) { private boolean hasChild(List<Catalogue> list, Catalogue catalogue) {
return getChildList(list, catalogue).size() > 0; return !getChildList(list, catalogue).isEmpty();
} }
/** /**
...@@ -341,7 +339,7 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata ...@@ -341,7 +339,7 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata
File file = new File(sourcePath); File file = new File(sourcePath);
File[] fs = file.listFiles(); File[] fs = file.listFiles();
if (fs == null) { if (fs == null) {
throw new RuntimeException("目录层级有误"); throw new RuntimeException("the dir is error");
} }
for (File fl : fs) { for (File fl : fs) {
if (fl.isFile()) { if (fl.isFile()) {
...@@ -373,7 +371,7 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata ...@@ -373,7 +371,7 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata
} }
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error("read file error, {} ", e);
} }
return sb.toString(); return sb.toString();
} }
...@@ -416,10 +414,12 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata ...@@ -416,10 +414,12 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata
List<History> historyList = historyService.list( List<History> historyList = historyService.list(
new LambdaQueryWrapper<History>().eq(History::getTaskId, catalogue.getTaskId())); new LambdaQueryWrapper<History>().eq(History::getTaskId, catalogue.getTaskId()));
historyList.forEach(history -> { historyList.forEach(history -> {
// 查询 job history 表中的作业 通过 id 关联查询 // 查询 job history 表中的作业 通过 id 关联查询 // TODO npe
JobHistory historyServiceById = jobHistoryService.getById(history.getId()); JobHistory historyServiceById = jobHistoryService.getById(history.getId());
if (historyServiceById != null) {
// 删除 job history 表中的作业 // 删除 job history 表中的作业
jobHistoryService.removeById(historyServiceById.getId()); jobHistoryService.removeById(historyServiceById.getId());
}
// 删除 history 表中的作业 // 删除 history 表中的作业
historyService.removeById(history.getId()); historyService.removeById(history.getId());
}); });
...@@ -446,6 +446,7 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata ...@@ -446,6 +446,7 @@ public class CatalogueServiceImpl extends SuperServiceImpl<CatalogueMapper, Cata
* @return * @return
*/ */
@Override @Override
@Transactional(rollbackFor = Exception.class)
public Boolean saveOrUpdateOrRename(Catalogue catalogue) { public Boolean saveOrUpdateOrRename(Catalogue catalogue) {
if (taskService.getById(catalogue.getTaskId()) != null) { if (taskService.getById(catalogue.getTaskId()) != null) {
toRename(catalogue); toRename(catalogue);
......
...@@ -91,6 +91,8 @@ public class FlinkConfigOptionsUtils { ...@@ -91,6 +91,8 @@ public class FlinkConfigOptionsUtils {
} }
public static String[] getConfigOptionsClass() { public static String[] getConfigOptionsClass() {
return ResourceUtil.readUtf8Str("FlinkConfClass").replace("\r", "").split("\n"); return ResourceUtil.readUtf8Str("dinky-loader/FlinkConfClass")
.replace("\r", "")
.split("\n");
} }
} }
cn.hutool.core.date.DateUtil
cn.hutool.core.util.IdUtil
cn.hutool.core.util.RandomUtil
\ No newline at end of file
...@@ -41,7 +41,15 @@ ...@@ -41,7 +41,15 @@
<include>**/*.yaml</include> <include>**/*.yaml</include>
<include>**/log4j2.xml</include> <include>**/log4j2.xml</include>
<include>**/DinkyFlinkDockerfile</include> <include>**/DinkyFlinkDockerfile</include>
<include>**/FlinkConfClass</include>
</includes>
</fileSet>
<fileSet>
<directory>${project.parent.basedir}/dinky-admin/target/classes</directory>
<outputDirectory>dinky-loader</outputDirectory>
<includes>
<include>**/dinky-loader/FlinkConfClass</include>
<include>**/dinky-loader/ExpressionVariableClass</include>
</includes> </includes>
</fileSet> </fileSet>
<fileSet> <fileSet>
......
...@@ -25,6 +25,8 @@ import static org.apache.flink.util.Preconditions.checkNotNull; ...@@ -25,6 +25,8 @@ import static org.apache.flink.util.Preconditions.checkNotNull;
import org.dinky.assertion.Asserts; import org.dinky.assertion.Asserts;
import org.dinky.constant.FlinkSQLConstant; import org.dinky.constant.FlinkSQLConstant;
import org.dinky.data.exception.DinkyException;
import org.dinky.utils.StringUtil;
import org.apache.flink.table.api.DataTypes; import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.Table; import org.apache.flink.table.api.Table;
...@@ -34,6 +36,7 @@ import org.apache.flink.types.Row; ...@@ -34,6 +36,7 @@ import org.apache.flink.types.Row;
import org.apache.flink.util.StringUtils; import org.apache.flink.util.StringUtils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
...@@ -41,17 +44,19 @@ import java.util.Map; ...@@ -41,17 +44,19 @@ import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Dict; import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.extra.expression.engine.jexl.JexlEngine; import cn.hutool.extra.expression.engine.jexl.JexlEngine;
import lombok.extern.slf4j.Slf4j;
/** /**
* Flink Sql Variable Manager * Flink Sql Variable Manager
* *
* @since 2021/6/7 22:06 * @since 2021/6/7 22:06
*/ */
@Slf4j
public final class VariableManager { public final class VariableManager {
public static final String VARIABLE = "variable"; public static final String VARIABLE = "variable";
static final String SHOW_VARIABLES = "SHOW VARIABLES"; static final String SHOW_VARIABLES = "SHOW VARIABLES";
...@@ -59,24 +64,43 @@ public final class VariableManager { ...@@ -59,24 +64,43 @@ public final class VariableManager {
public static final JexlEngine ENGINE = new JexlEngine(); public static final JexlEngine ENGINE = new JexlEngine();
public static final Dict ENGINE_CONTEXT = Dict.create();
/** /**
* <p> * load expression variable class
* engine , key is variable name , value is class .
* for example:
* random -> RandomUtil -> about random operation
* date -> DateUtil -> about date operation
* id -> IdUtil -> to generate random uuid
* ...
*/ */
public static final Dict ENGINE_CONTEXT = Dict.create() private static void loadExpressionVariableClass() {
.set("random", RandomUtil.class) List<String> classLoaderVariableJexlClass = getClassLoaderVariableJexlClass();
.set("date", DateUtil.class) if (CollUtil.isEmpty(classLoaderVariableJexlClass)) {
.set("id", IdUtil.class); return;
}
classLoaderVariableJexlClass.forEach(fullClassName -> {
try {
String classSimpleName =
BeanUtil.getBeanDesc(Class.forName(fullClassName)).getSimpleName();
String snakeCaseClassName = StringUtil.toSnakeCase(true, classSimpleName);
ENGINE_CONTEXT.set(snakeCaseClassName, Class.forName(fullClassName));
log.info("load class : {}", fullClassName);
} catch (ClassNotFoundException e) {
log.error(
"The class [{}] that needs to be loaded may not be loaded by dinky or there is no jar file of this class under dinky's lib/plugins. Please check, and try again. {}",
fullClassName,
e.getMessage(),
e);
}
});
}
public VariableManager() { public VariableManager() {
variables = new HashMap<>(); variables = new HashMap<>();
} }
public static List<String> getClassLoaderVariableJexlClass() {
return Arrays.asList(ResourceUtil.readUtf8Str("dinky-loader/ExpressionVariableClass")
.replace("\r", "")
.split("\n"));
}
/** /**
* Get names of sql variables loaded. * Get names of sql variables loaded.
* *
...@@ -140,15 +164,18 @@ public final class VariableManager { ...@@ -140,15 +164,18 @@ public final class VariableManager {
*/ */
public Object getVariable(String variableName) { public Object getVariable(String variableName) {
checkArgument( checkArgument(
!StringUtils.isNullOrWhitespaceOnly(variableName), "sql variableName name cannot be null or empty."); !StringUtils.isNullOrWhitespaceOnly(variableName),
"sql variable name or jexl key cannot be null or empty.");
try { try {
if (variables.containsKey(variableName)) { if (variables.containsKey(variableName)) {
return variables.get(variableName); return variables.get(variableName);
} }
// load expression variable class
loadExpressionVariableClass();
// use jexl to parse variable value // use jexl to parse variable value
return ENGINE.eval(variableName, ENGINE_CONTEXT, null); return ENGINE.eval(variableName, ENGINE_CONTEXT, null);
} catch (Exception e) { } catch (Exception e) {
throw new CatalogException(format("The variable of sql %s does not exist.", variableName)); throw new DinkyException(format("The variable name or jexl key of sql %s does not exist.", variableName));
} }
} }
......
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.dinky.utils;
import cn.hutool.core.util.StrUtil;
public class StringUtil {
/**
* 驼峰 -> 蛇形命名
*
* @param content 内容
* @param isFirstUpper 是否首字母大写
* @param isFirstLower 是否首字母小写
* @return 转换后的内容
*/
public static String toSnakeCase(String content, boolean isFirstUpper, boolean isFirstLower) {
if (StrUtil.isEmpty(content)) {
return content;
}
StringBuilder sb = new StringBuilder(content.length());
for (int i = 0; i < content.length(); i++) {
char c = content.charAt(i);
if (isFirstUpper && i == 0) {
sb.append(Character.toUpperCase(c));
} else if (isFirstLower && i == 0) {
sb.append(Character.toLowerCase(c));
} else {
sb.append(c);
}
}
return sb.toString();
}
/**
* 蛇形命名转换 -> 小驼峰
*
* @param content 内容
* @param isFirstUpper 是否首字母大写
* @return 转换后的内容
*/
public static String toSnakeCase(String content, boolean isFirstUpper) {
return toSnakeCase(content, isFirstUpper, false);
}
/**
* 蛇形命名转换 -> 小驼峰
*
* @param isFirstLower 是否首字母小写
* @param content 内容
* @return 转换后的内容
*/
public static String toSnakeCase(boolean isFirstLower, String content) {
return toSnakeCase(content, false, isFirstLower);
}
}
...@@ -111,23 +111,24 @@ SHOW FRAGMENT var1; ...@@ -111,23 +111,24 @@ SHOW FRAGMENT var1;
如您升级到 `1.0.0` 及以上版本, 请修改引用 `${_CURRENT_DATE_}` `${_CURRENT_TIMESTAMP_}` 变量的相关作业 如您升级到 `1.0.0` 及以上版本, 请修改引用 `${_CURRENT_DATE_}` `${_CURRENT_TIMESTAMP_}` 变量的相关作业
新的表达式变量无需声明, 已由程序启动时自动加载初始化 ,直接使用即可 新的表达式变量无需声明 ,直接使用即可,
请注意: 表达式变量调用时请使用小驼峰命名法
::: :::
### 日期时间类表达式变量 ### 日期时间类表达式变量
> 日期相关: `date` 为实例名用来调用方法`(必须固定)`,使用 [DateUtil](https://doc.hutool.cn/pages/DateUtil/)工具类实现,且支持此工具类的所有方法调用 > 日期相关: `dateUtil` 为实例名用来调用方法`(必须固定)`,使用 [DateUtil](https://doc.hutool.cn/pages/DateUtil/)工具类实现,且支持此工具类的所有方法调用
```sql ```sql
-- 获取当前秒 -- 获取当前秒
select '${date.currentSeconds()}'; select '${dateUtil.currentSeconds()}';
# 获取日期 减去 10 天 # 获取日期 减去 10 天
select '${date.offsetDay(date.date(), -10)}'; select '${dateUtil.offsetDay(date.date(), -10)}';
# 获取当前日期 标准格式 yyyy-MM-dd HH:mm:ss # 获取当前日期 标准格式 yyyy-MM-dd HH:mm:ss
select '${date.now()}'; select '${dateUtil.now()}';
# etc ..... # etc .....
...@@ -138,30 +139,30 @@ select '${date.now()}'; ...@@ -138,30 +139,30 @@ select '${date.now()}';
### 随机串相关表达式变量 ### 随机串相关表达式变量
> `random` 为实例名用来调用方法`(必须固定)`, 使用 [RandomUtil](https://doc.hutool.cn/pages/RandomUtil/)工具类实现,且支持此工具类的所有方法调用 > `randomUtil` 为实例名用来调用方法`(必须固定)`, 使用 [RandomUtil](https://doc.hutool.cn/pages/RandomUtil/)工具类实现,且支持此工具类的所有方法调用
```sql ```sql
# 产生一个[10, 100)的随机数 # 产生一个[10, 100)的随机数
select '${random.randomInt(10, 100)}'; select '${randomUtil.randomInt(10, 100)}';
# 随机字符串(只包含数字和字符) # 随机字符串(只包含数字和字符)
select '${random.randomString()}'; select '${randomUtil.randomString()}';
# 获得一个只包含数字的字符串 # 获得一个只包含数字的字符串
select '${random.randomNumbers()}'; select '${randomUtil.randomNumbers()}';
``` ```
### 唯一 ID 相关表达式变量 ### 唯一 ID 相关表达式变量
> id 为实例名用来调用方法`(必须固定)` , 使用 [IdUtil](https://doc.hutool.cn/pages/IdUtil/) 工具类实现 > idUtil 为实例名用来调用方法`(必须固定)` , 使用 [IdUtil](https://doc.hutool.cn/pages/IdUtil/) 工具类实现
```sql ```sql
# 生成的UUID是带-的字符串,类似于:a5c8a5e8-df2b-4706-bea4-08d0939410e3 # 生成的UUID是带-的字符串,类似于:a5c8a5e8-df2b-4706-bea4-08d0939410e3
select '${id.randomUUID()}'; select '${idUtil.randomUUID()}';
# 生成的是不带-的字符串,类似于:b17f24ff026d40949c85a24f4f375d42 # 生成的是不带-的字符串,类似于:b17f24ff026d40949c85a24f4f375d42
select '${id.simpleUUID()}'; select '${idUtil.simpleUUID()}';
``` ```
:::tip 扩展 :::tip 扩展
......
...@@ -6,49 +6,36 @@ title: 表达式变量扩展 ...@@ -6,49 +6,36 @@ title: 表达式变量扩展
:::tip :::tip
本扩展文档适用于 v1.0.0 及以上版本 本扩展文档适用于 v1.0.0 及以上版本,可以直接在已部署服务中直接扩展,无需重新编译部署
::: :::
## 介绍 ## 介绍
> 本扩展用于扩展表达式中的变量,以满足更多场景需求 > 本扩展用于扩展表达式中的变量,以满足更多场景需求
## 前置环境
> 扩展前需要启动 Dinky 项目, 本地开发环境搭建请参考 [本地调试](../../developer_guide/local_debug)
## 扩展方法 ## 扩展方法
找到 `dinky-core` 模块下的 `org.dinky.executor.VariableManager.ENGINE_CONTEXT` 变量, 并在其中添加 找到 `部署的 dinky/dinky-loader` 目录下的 `ExpressionVariableClass` 文件, 并在其中添加
举例: 举例:
> 假设需要扩展 [`Hash 算法`](https://doc.hutool.cn/pages/HashUtil/) 相关的表达式变量 > 假设需要扩展 [`Hash 算法`](https://doc.hutool.cn/pages/HashUtil/) 相关的表达式变量
```java 只需要在 `ExpressionVariableClass` 文件中添加如下类的全限定名即可
```text
import cn.hutool.core.util.HashUtil;
public final class VariableManager {
public static final Dict ENGINE_CONTEXT = Dict.create() cn.hutool.core.util.HashUtil
.set("random", RandomUtil.class)
.set("date", DateUtil.class)
// 只需要在此处添加即可
// 需要注意的是, 此处的 key 必须与表达式中的变量名一致,随后便可以使用该变量调用其方法
.set("hash", HashUtil.class)
.set("id", IdUtil.class);
//... 其他代码无需关心,因此省略...
}
``` ```
如上示例,将会扩展表达式变量中的 `Hash 算法`,你就可以在表达式中使用 `Hash 算法` 相关的表达式了
:::tip 说明 :::tip 说明
如您扩展完成需要生产中使用, 参考[编译](../../deploy_guide/compiler) , 打包完成 替换服务器部署的 `dinky/lib/dinky-core-1.0.0.jar` 文件, 重启 dinky 服务即可 1. 请确保您的扩展类在 `dinky-loader` 中存在
2. 请确保您的扩展类在 `dinky 中已被加载(类加载机制)`
3. 请确保您的扩展类中的方法为 `public static` 修饰
考虑到多数用户通用情况下,当然也欢迎您将此次扩展贡献给社区, 请参考 [如何贡献](../../developer_guide/contribution/how_contribute) 进行 [PR](../../developer_guide/contribution/pull_request)
考虑到多数用户通用情况下,当然也欢迎您将此次扩展贡献给社区, 请参考 [如何贡献](../../developer_guide/contribution/how_contribute) 进行 [PR](../../developer_guide/contribution/pull_request)
::: :::
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment