This is an automated email from the ASF dual-hosted git repository. aloyszhang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/inlong.git
The following commit(s) were added to refs/heads/master by this push: new fbde3da763 [INLONG-10841][SDK] Transform SQL supports toBase64 function (#10845) fbde3da763 is described below commit fbde3da7630f7fb703f37e17370fa0c8ae7f0169 Author: zoy0 <105140381+z...@users.noreply.github.com> AuthorDate: Thu Aug 22 16:59:37 2024 +0800 [INLONG-10841][SDK] Transform SQL supports toBase64 function (#10845) --- .../process/function/ToBase64Function.java | 48 ++++++++++++++++++++++ .../transform/process/operator/OperatorTools.java | 2 + .../TestTransformStringFunctionsProcessor.java | 16 ++++++++ 3 files changed, 66 insertions(+) diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ToBase64Function.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ToBase64Function.java new file mode 100644 index 0000000000..a82f36d9c2 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/ToBase64Function.java @@ -0,0 +1,48 @@ +/* + * 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.Function; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +/** + * ToBase64Function + * description: to_base64(string1)--returns the base64-encoded result from string1 + */ +public class ToBase64Function implements ValueParser { + + private final ValueParser stringParser; + + public ToBase64Function(Function expr) { + stringParser = OperatorTools.buildParser(expr.getParameters().getExpressions().get(0)); + } + + @Override + public Object parse(SourceData sourceData, int rowIndex, Context context) { + Object stringObj = stringParser.parse(sourceData, rowIndex, context); + String string = OperatorTools.parseString(stringObj); + return Base64.getEncoder().encodeToString(string.getBytes(StandardCharsets.UTF_8)); + } +} 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 5ed42095da..c1cffbf88a 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 @@ -42,6 +42,7 @@ import org.apache.inlong.sdk.transform.process.function.SqrtFunction; import org.apache.inlong.sdk.transform.process.function.SubstringFunction; import org.apache.inlong.sdk.transform.process.function.TanFunction; import org.apache.inlong.sdk.transform.process.function.TimestampExtractFunction; +import org.apache.inlong.sdk.transform.process.function.ToBase64Function; import org.apache.inlong.sdk.transform.process.function.ToDateFunction; import org.apache.inlong.sdk.transform.process.function.ToTimestampFunction; import org.apache.inlong.sdk.transform.process.function.TrimFunction; @@ -140,6 +141,7 @@ public class OperatorTools { functionMap.put("from_unixtime", FromUnixTimeFunction::new); functionMap.put("unix_timestamp", UnixTimestampFunction::new); functionMap.put("to_timestamp", ToTimestampFunction::new); + functionMap.put("to_base64", ToBase64Function::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 a56fc520b3..5479b4485a 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 @@ -184,4 +184,20 @@ public class TestTransformStringFunctionsProcessor { Assert.assertEquals(output3.get(0), "result=in long"); } + @Test + public void testToBase64Function() throws Exception { + String transformSql = "select to_base64(string1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor<String, String> processor1 = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // to_base64('app-fun') + List<String> output1 = processor1.transform("app-fun", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=YXBwLWZ1bg=="); + // to_base64('hello world') + List<String> output2 = processor1.transform("hello world", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=aGVsbG8gd29ybGQ="); + } }