|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package com.dtstack.flink.sql.function; |
| 20 | + |
| 21 | +import org.apache.flink.api.common.typeinfo.TypeInformation; |
| 22 | +import org.apache.flink.table.api.TableEnvironment; |
| 23 | +import org.apache.flink.table.api.java.StreamTableEnvironment; |
| 24 | +import org.apache.flink.table.functions.ScalarFunction; |
| 25 | +import org.apache.flink.table.functions.TableFunction; |
| 26 | +import org.apache.flink.table.functions.AggregateFunction; |
| 27 | + |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | + |
| 32 | +/** |
| 33 | + * 自定义函数管理类 |
| 34 | + * Reason: |
| 35 | + * Date: 2017/2/21 |
| 36 | + * Company: www.dtstack.com |
| 37 | + * @author xuchao |
| 38 | + */ |
| 39 | + |
| 40 | +public class FunctionManager { |
| 41 | + private static final Logger logger = LoggerFactory.getLogger(FunctionManager.class); |
| 42 | + |
| 43 | + /** |
| 44 | + * TABLE|SCALA|AGGREGATE |
| 45 | + * 注册UDF到table env |
| 46 | + */ |
| 47 | + public static void registerUDF(String type, String classPath, String funcName, TableEnvironment tableEnv, ClassLoader classLoader) { |
| 48 | + if ("SCALA".equalsIgnoreCase(type)) { |
| 49 | + registerScalaUDF(classPath, funcName, tableEnv, classLoader); |
| 50 | + } else if ("TABLE".equalsIgnoreCase(type)) { |
| 51 | + registerTableUDF(classPath, funcName, tableEnv, classLoader); |
| 52 | + } else if ("AGGREGATE".equalsIgnoreCase(type)) { |
| 53 | + registerAggregateUDF(classPath, funcName, tableEnv, classLoader); |
| 54 | + } else { |
| 55 | + throw new RuntimeException("not support of UDF which is not in (TABLE, SCALA, AGGREGATE)"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * 注册自定义方法到env上 |
| 61 | + * @param classPath |
| 62 | + * @param funcName |
| 63 | + * @param tableEnv |
| 64 | + */ |
| 65 | + public static void registerScalaUDF(String classPath, String funcName, TableEnvironment tableEnv, ClassLoader classLoader) { |
| 66 | + try { |
| 67 | + ScalarFunction udfFunc = Class.forName(classPath, false, classLoader) |
| 68 | + .asSubclass(ScalarFunction.class).newInstance(); |
| 69 | + tableEnv.registerFunction(funcName, udfFunc); |
| 70 | + logger.info("register scala function:{} success.", funcName); |
| 71 | + } catch (Exception e) { |
| 72 | + logger.error("", e); |
| 73 | + throw new RuntimeException("register UDF exception:", e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * 注册自定义TABLEFFUNC方法到env上 |
| 79 | + * |
| 80 | + * @param classPath |
| 81 | + * @param funcName |
| 82 | + * @param tableEnv |
| 83 | + */ |
| 84 | + public static void registerTableUDF(String classPath, String funcName, TableEnvironment tableEnv, ClassLoader classLoader) { |
| 85 | + try { |
| 86 | + checkStreamTableEnv(tableEnv); |
| 87 | + TableFunction udtf = Class.forName(classPath, false, classLoader) |
| 88 | + .asSubclass(TableFunction.class).newInstance(); |
| 89 | + |
| 90 | + ((StreamTableEnvironment) tableEnv).registerFunction(funcName, udtf); |
| 91 | + logger.info("register table function:{} success.", funcName); |
| 92 | + } catch (Exception e) { |
| 93 | + logger.error("", e); |
| 94 | + throw new RuntimeException("register Table UDF exception:", e); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static void checkStreamTableEnv(TableEnvironment tableEnv) { |
| 99 | + if (!(tableEnv instanceof StreamTableEnvironment)) { |
| 100 | + throw new RuntimeException("no support tableEnvironment class for " + tableEnv.getClass().getName()); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * 注册自定义Aggregate FUNC方法到env上 |
| 106 | + * |
| 107 | + * @param classPath |
| 108 | + * @param funcName |
| 109 | + * @param tableEnv |
| 110 | + */ |
| 111 | + public static void registerAggregateUDF(String classPath, String funcName, TableEnvironment tableEnv, ClassLoader classLoader) { |
| 112 | + try { |
| 113 | + checkStreamTableEnv(tableEnv); |
| 114 | + |
| 115 | + AggregateFunction udaf = Class.forName(classPath, false, classLoader) |
| 116 | + .asSubclass(AggregateFunction.class).newInstance(); |
| 117 | + ((StreamTableEnvironment) tableEnv).registerFunction(funcName, udaf); |
| 118 | + logger.info("register Aggregate function:{} success.", funcName); |
| 119 | + } catch (Exception e) { |
| 120 | + logger.error("", e); |
| 121 | + throw new RuntimeException("register Aggregate UDF exception:", e); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + public static TypeInformation[] transformTypes(Class[] fieldTypes) { |
| 127 | + TypeInformation[] types = new TypeInformation[fieldTypes.length]; |
| 128 | + for (int i = 0; i < fieldTypes.length; i++) { |
| 129 | + types[i] = TypeInformation.of(fieldTypes[i]); |
| 130 | + } |
| 131 | + |
| 132 | + return types; |
| 133 | + } |
| 134 | +} |
0 commit comments