Unverified Commit 443f5011 authored by ZackYoung's avatar ZackYoung Committed by GitHub

[BugFix] [UDF] Fix the issue where UDF cannot find classes in Flink SQL tasks (#3525)

parent 4cfd123d
...@@ -21,6 +21,8 @@ package org.dinky.function.compiler; ...@@ -21,6 +21,8 @@ package org.dinky.function.compiler;
import org.dinky.function.constant.PathConstant; import org.dinky.function.constant.PathConstant;
import org.apache.flink.table.catalog.FunctionLanguage;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import scala.runtime.AbstractFunction1; import scala.runtime.AbstractFunction1;
import scala.runtime.BoxedUnit; import scala.runtime.BoxedUnit;
...@@ -40,12 +42,12 @@ public class CustomStringScalaCompiler { ...@@ -40,12 +42,12 @@ public class CustomStringScalaCompiler {
} }
} }
public static IMain getInterpreter(Integer missionId) { public static IMain getInterpreter() {
GenericRunnerSettings settings = new GenericRunnerSettings(new ErrorHandler()); GenericRunnerSettings settings = new GenericRunnerSettings(new ErrorHandler());
settings.usejavacp().tryToSetFromPropertyValue("true"); settings.usejavacp().tryToSetFromPropertyValue("true");
settings.Yreploutdir().tryToSetFromPropertyValue(PathConstant.getUdfCompilerJavaPath(missionId)); settings.Yreploutdir().tryToSetFromPropertyValue(PathConstant.getUdfCompilerPath(FunctionLanguage.JAVA));
return new IMain(settings); return new IMain(settings);
} }
} }
...@@ -35,7 +35,6 @@ import cn.hutool.core.collection.CollUtil; ...@@ -35,7 +35,6 @@ import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
/** @since 0.6.8 */
public class JVMPackage implements FunctionPackage { public class JVMPackage implements FunctionPackage {
@Override @Override
...@@ -57,7 +56,7 @@ public class JVMPackage implements FunctionPackage { ...@@ -57,7 +56,7 @@ public class JVMPackage implements FunctionPackage {
for (int i = 0; i < classNameList.size(); i++) { for (int i = 0; i < classNameList.size(); i++) {
String className = classNameList.get(i); String className = classNameList.get(i);
String classFile = StrUtil.replace(className, ".", "/") + ".class"; String classFile = StrUtil.replace(className, ".", "/") + ".class";
String absoluteFilePath = PathConstant.getUdfCompilerJavaPath(missionId, classFile); String absoluteFilePath = PathConstant.getUdfCompilerPath(FunctionLanguage.JAVA, classFile);
clazzs[i] = classFile; clazzs[i] = classFile;
fileInputStreams[i] = FileUtil.getInputStream(absoluteFilePath); fileInputStreams[i] = FileUtil.getInputStream(absoluteFilePath);
......
...@@ -23,6 +23,7 @@ import org.dinky.function.constant.PathConstant; ...@@ -23,6 +23,7 @@ import org.dinky.function.constant.PathConstant;
import org.dinky.function.data.model.UDF; import org.dinky.function.data.model.UDF;
import org.apache.flink.configuration.ReadableConfig; import org.apache.flink.configuration.ReadableConfig;
import org.apache.flink.table.catalog.FunctionLanguage;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -46,7 +47,7 @@ public class JavaCompiler implements FunctionCompiler { ...@@ -46,7 +47,7 @@ public class JavaCompiler implements FunctionCompiler {
// TODO 改为ProcessStep注释 // TODO 改为ProcessStep注释
log.info("Compiling java code, class: {}", udf.getClassName()); log.info("Compiling java code, class: {}", udf.getClassName());
CustomStringJavaCompiler compiler = new CustomStringJavaCompiler(udf.getCode()); CustomStringJavaCompiler compiler = new CustomStringJavaCompiler(udf.getCode());
boolean res = compiler.compilerToTmpPath(PathConstant.getUdfCompilerJavaPath(missionId)); boolean res = compiler.compilerToTmpPath(PathConstant.getUdfCompilerPath(FunctionLanguage.JAVA));
String className = compiler.getFullClassName(); String className = compiler.getFullClassName();
if (res) { if (res) {
log.info("class compiled successfully:{}", className); log.info("class compiled successfully:{}", className);
......
...@@ -69,7 +69,8 @@ public class PythonFunction implements FunctionCompiler, FunctionPackage { ...@@ -69,7 +69,8 @@ public class PythonFunction implements FunctionCompiler, FunctionPackage {
log.info("正在编译 python 代码 , class: " + udf.getClassName()); log.info("正在编译 python 代码 , class: " + udf.getClassName());
File pyFile = FileUtil.writeUtf8String( File pyFile = FileUtil.writeUtf8String(
udf.getCode(), udf.getCode(),
PathConstant.getUdfCompilerPythonPath(missionId, UDFUtil.getPyFileName(udf.getClassName()) + ".py")); PathConstant.getUdfCompilerPath(
FunctionLanguage.PYTHON, UDFUtil.getPyFileName(udf.getClassName()) + ".py"));
File zipFile = ZipUtil.zip(pyFile); File zipFile = ZipUtil.zip(pyFile);
FileUtil.del(pyFile); FileUtil.del(pyFile);
try { try {
...@@ -83,12 +84,12 @@ public class PythonFunction implements FunctionCompiler, FunctionPackage { ...@@ -83,12 +84,12 @@ public class PythonFunction implements FunctionCompiler, FunctionPackage {
SystemConfiguration.getInstances().getPythonHome()); SystemConfiguration.getInstances().getPythonHome());
PythonFunctionFactory.getPythonFunction(udf.getClassName(), configuration, null); PythonFunctionFactory.getPythonFunction(udf.getClassName(), configuration, null);
log.info("Python udf编译成功 ; className:" + udf.getClassName()); log.info("Python udf compiled successfully; className:{}", udf.getClassName());
} catch (Exception e) { } catch (Exception e) {
log.error("Python udf编译失败 ; className:" log.error(
+ udf.getClassName() "Python udf compilation failed; className:{}\n.reason: {}",
+ " 。 原因: " udf.getClassName(),
+ ExceptionUtil.getRootCauseMessage(e)); ExceptionUtil.getRootCauseMessage(e));
return false; return false;
} }
FileUtil.del(zipFile); FileUtil.del(zipFile);
...@@ -112,8 +113,8 @@ public class PythonFunction implements FunctionCompiler, FunctionPackage { ...@@ -112,8 +113,8 @@ public class PythonFunction implements FunctionCompiler, FunctionPackage {
.map(udf -> { .map(udf -> {
File file = FileUtil.writeUtf8String( File file = FileUtil.writeUtf8String(
udf.getCode(), udf.getCode(),
PathConstant.getUdfCompilerPythonPath( PathConstant.getUdfCompilerPath(
missionId, UDFUtil.getPyFileName(udf.getClassName()) + ".py")); FunctionLanguage.PYTHON, UDFUtil.getPyFileName(udf.getClassName()) + ".py"));
return FileUtil.getInputStream(file); return FileUtil.getInputStream(file);
}) })
.toArray(InputStream[]::new); .toArray(InputStream[]::new);
......
...@@ -39,7 +39,7 @@ public class ScalaCompiler implements FunctionCompiler { ...@@ -39,7 +39,7 @@ public class ScalaCompiler implements FunctionCompiler {
String className = udf.getClassName(); String className = udf.getClassName();
log.info("正在编译 scala 代码 , class: " + className); log.info("正在编译 scala 代码 , class: " + className);
if (CustomStringScalaCompiler.getInterpreter(missionId).compileString(udf.getCode())) { if (CustomStringScalaCompiler.getInterpreter().compileString(udf.getCode())) {
log.info("scala class编译成功:" + className); log.info("scala class编译成功:" + className);
return true; return true;
} else { } else {
......
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
package org.dinky.function.constant; package org.dinky.function.constant;
import org.apache.flink.table.catalog.FunctionLanguage;
import java.io.File; import java.io.File;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
...@@ -58,12 +60,12 @@ public class PathConstant { ...@@ -58,12 +60,12 @@ public class PathConstant {
return StrUtil.join(File.separator, path) + File.separator; return StrUtil.join(File.separator, path) + File.separator;
} }
public static String getUdfCompilerJavaPath(Integer missionId, Object... path) { public static String getUdfCompilerPath(FunctionLanguage language, String fileName) {
return getPath(UDF_PATH, missionId, COMPILER, "java", path); return getPath(UDF_PATH, COMPILER, language.name(), fileName);
} }
public static String getUdfCompilerPythonPath(Integer missionId, Object... path) { public static String getUdfCompilerPath(FunctionLanguage language) {
return getPath(UDF_PATH, missionId, COMPILER, "python", path); return getPath(UDF_PATH, COMPILER, language.name());
} }
public static String getUdfPackagePath(Integer missionId, Object... path) { public static String getUdfPackagePath(Integer missionId, Object... path) {
......
...@@ -214,7 +214,7 @@ public class UDFUtil { ...@@ -214,7 +214,7 @@ public class UDFUtil {
} }
} else if (udf.getFunctionLanguage() == FunctionLanguage.SCALA) { } else if (udf.getFunctionLanguage() == FunctionLanguage.SCALA) {
String className = udf.getClassName(); String className = udf.getClassName();
if (CustomStringScalaCompiler.getInterpreter(null).compileString(udf.getCode())) { if (CustomStringScalaCompiler.getInterpreter().compileString(udf.getCode())) {
log.info("scala class compile successful:{}", className); log.info("scala class compile successful:{}", className);
ClassPool.push(ClassEntity.build(className, udf.getCode())); ClassPool.push(ClassEntity.build(className, udf.getCode()));
successList.add(className); successList.add(className);
......
...@@ -37,43 +37,43 @@ export const buildRunModelOptions = () => { ...@@ -37,43 +37,43 @@ export const buildRunModelOptions = () => {
{ {
label: 'Local', label: 'Local',
value: RUN_MODE.LOCAL, value: RUN_MODE.LOCAL,
key: RUN_MODE.LOCAL, key: RUN_MODE.LOCAL
}, },
{ {
label: 'Standalone', label: 'Standalone',
value: RUN_MODE.STANDALONE, value: RUN_MODE.STANDALONE,
key: RUN_MODE.STANDALONE, key: RUN_MODE.STANDALONE
}, },
{ {
label: 'Yarn Session', label: 'Yarn Session',
value: RUN_MODE.YARN_SESSION, value: RUN_MODE.YARN_SESSION,
key: RUN_MODE.YARN_SESSION, key: RUN_MODE.YARN_SESSION
}, },
{ {
// flink弃用了 yarn per-job 模式 在这写个标签 带横线的 | flink deprecated yarn per-job mode, write a label here with a horizontal line // flink弃用了 yarn per-job 模式 在这写个标签 带横线的 | flink deprecated yarn per-job mode, write a label here with a horizontal line
label: <del>Yarn Per-Job (Deprecated)</del>, label: <del>Yarn Per-Job (Deprecated)</del>,
value: RUN_MODE.YARN_PER_JOB, value: RUN_MODE.YARN_PER_JOB,
key: RUN_MODE.YARN_PER_JOB, key: RUN_MODE.YARN_PER_JOB
}, },
{ {
label: 'Yarn Application', label: 'Yarn Application',
value: RUN_MODE.YARN_APPLICATION, value: RUN_MODE.YARN_APPLICATION,
key: RUN_MODE.YARN_APPLICATION, key: RUN_MODE.YARN_APPLICATION
}, },
{ {
label: 'Kubernetes Session', label: 'Kubernetes Session',
value: RUN_MODE.KUBERNETES_SESSION, value: RUN_MODE.KUBERNETES_SESSION,
key: RUN_MODE.KUBERNETES_SESSION, key: RUN_MODE.KUBERNETES_SESSION
}, },
{ {
label: 'Kubernetes Application', label: 'Kubernetes Application',
value: RUN_MODE.KUBERNETES_APPLICATION, value: RUN_MODE.KUBERNETES_APPLICATION,
key: RUN_MODE.KUBERNETES_APPLICATION, key: RUN_MODE.KUBERNETES_APPLICATION
}, },
{ {
label: 'Kubernetes Operator Application', label: 'Kubernetes Operator Application',
value: RUN_MODE.KUBERNETES_APPLICATION_OPERATOR, value: RUN_MODE.KUBERNETES_APPLICATION_OPERATOR,
key: RUN_MODE.KUBERNETES_APPLICATION_OPERATOR, key: RUN_MODE.KUBERNETES_APPLICATION_OPERATOR
} }
); );
...@@ -165,7 +165,7 @@ export const buildEnvOptions = (env: TaskInfo[] = []) => { ...@@ -165,7 +165,7 @@ export const buildEnvOptions = (env: TaskInfo[] = []) => {
{l('button.disable')} {l('button.disable')}
</Space> </Space>
), ),
title: l('button.disable'), title: l('button.disable'),
value: -1, value: -1,
key: -1 key: -1
} }
......
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