From 17522d9f613f50a2a5ef2b0f6c0c0e56a5d943b1 Mon Sep 17 00:00:00 2001 From: Unqyan <2369492695@qq.com> Date: Thu, 22 Aug 2024 19:26:36 +0800 Subject: [PATCH] [INLONG-10816][SDK] Transform support Replace function.(#10816) --- .../process/function/ReplaceFunction.java | 57 +++++++++++++++++++ .../process/operator/OperatorTools.java | 2 + ...TestTransformStringFunctionsProcessor.java | 36 ++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ReplaceFunction.java diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ReplaceFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ReplaceFunction.java new file mode 100644 index 00000000000..d9d1d26a210 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ReplaceFunction.java @@ -0,0 +1,57 @@ +/* + * 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.apache.inlong.sdk.transform.process.function; + +import org.apache.inlong.sdk.transform.decode.SourceData; +import org.apache.inlong.sdk.transform.process.Context; +import org.apache.inlong.sdk.transform.process.operator.OperatorTools; +import org.apache.inlong.sdk.transform.process.parser.ValueParser; + +import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.expression.Function; + +import java.util.List; + +/** + * ReplaceFunction + * description: replace(s, s1, s2)--replace string s1 in string s with string s2. + */ +public class ReplaceFunction implements ValueParser { + + private ValueParser stringParser; + private ValueParser targetParser; + private ValueParser replacementParser; + + public ReplaceFunction(Function expr) { + List expressions = expr.getParameters().getExpressions(); + stringParser = OperatorTools.buildParser(expressions.get(0)); + targetParser = OperatorTools.buildParser(expressions.get(1)); + replacementParser = OperatorTools.buildParser(expressions.get(2)); + } + + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + Object strObj = stringParser.parse(sourceData, rowIndex, context); + Object targetObj = targetParser.parse(sourceData, rowIndex, context); + Object replacementObj = replacementParser.parse(sourceData, rowIndex, context); + String str = OperatorTools.parseString(strObj); + String target = OperatorTools.parseString(targetObj); + String replacement = OperatorTools.parseString(replacementObj); + return str.replace(target, replacement); + } +} \ No newline at end of file diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java index c1cffbf88ae..c867ca7c211 100644 --- a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/operator/OperatorTools.java @@ -34,6 +34,7 @@ import org.apache.inlong.sdk.transform.process.function.LogFunction; import org.apache.inlong.sdk.transform.process.function.NowFunction; import org.apache.inlong.sdk.transform.process.function.PowerFunction; +import org.apache.inlong.sdk.transform.process.function.ReplaceFunction; import org.apache.inlong.sdk.transform.process.function.ReplicateFunction; import org.apache.inlong.sdk.transform.process.function.RoundFunction; import org.apache.inlong.sdk.transform.process.function.SinFunction; @@ -142,6 +143,7 @@ public class OperatorTools { functionMap.put("unix_timestamp", UnixTimestampFunction::new); functionMap.put("to_timestamp", ToTimestampFunction::new); functionMap.put("to_base64", ToBase64Function::new); + functionMap.put("replace", ReplaceFunction::new); } public static ExpressionOperator buildOperator(Expression expr) { diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformStringFunctionsProcessor.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformStringFunctionsProcessor.java index 5479b4485aa..8b8e940863b 100644 --- a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformStringFunctionsProcessor.java +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/TestTransformStringFunctionsProcessor.java @@ -200,4 +200,40 @@ public void testToBase64Function() throws Exception { Assert.assertEquals(1, output2.size()); Assert.assertEquals(output2.get(0), "result=aGVsbG8gd29ybGQ="); } + @Test + public void testReplaceFunction() throws Exception { + String transformSql = "select replace(string1, string2, string3) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case1: replace('hooray', 'oray', 'lly') + List output1 = processor.transform("hooray|oray|lly", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=holly"); + // case2: replace('hooray', 'hook', 'hoor') + List output2 = processor.transform("hooray|hook|hoor", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=hooray"); + // case3: replace('Hello World', 'World', '') + List output3 = processor.transform("Hello World|World|", new HashMap<>()); + Assert.assertEquals(1, output3.size()); + Assert.assertEquals(output3.get(0), "result=Hello "); + // case4: replace('Hello World', '', 'J') + List output4 = processor.transform("Hello World||J", new HashMap<>()); + Assert.assertEquals(1, output4.size()); + Assert.assertEquals(output4.get(0), "result=JHJeJlJlJoJ JWJoJrJlJdJ"); + // case5: replace('', '', '') + List output5 = processor.transform("||", new HashMap<>()); + Assert.assertEquals(1, output5.size()); + Assert.assertEquals(output5.get(0), "result="); + // case6: replace('abababab', 'ab', 'cd') + List output6 = processor.transform("abababab|ab|cd", new HashMap<>()); + Assert.assertEquals(1, output6.size()); + Assert.assertEquals(output6.get(0), "result=cdcdcdcd"); + // case7: replace('aaa', 'aa', 'd') + List output7 = processor.transform("aaa|aa|d", new HashMap<>()); + Assert.assertEquals(1, output7.size()); + Assert.assertEquals(output7.get(0), "result=da"); + } }