diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/UrlDecodeFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/UrlDecodeFunction.java new file mode 100644 index 00000000000..6802e0d7280 --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/UrlDecodeFunction.java @@ -0,0 +1,45 @@ +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.net.URLDecoder; +import java.nio.charset.StandardCharsets; + +/** + * UrlDecodeFunction + * description: Decodes a given string in ‘application/x-www-form-urlencoded’ format using the UTF-8 encoding scheme. + * If the input is NULL, or there is an issue with the decoding process(such as encountering an illegal escape pattern), + * or the encoding scheme is not supported, the function returns NULL. + */ +@TransformFunction(names = {"url_decode"}) +public class UrlDecodeFunction implements ValueParser { + + private final ValueParser stringParser; + + public UrlDecodeFunction(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); + if (stringObj == null) { + return null; + } + String string = OperatorTools.parseString(stringObj); + if (string == null) { + return null; + } + + try { + return URLDecoder.decode(string, StandardCharsets.UTF_8.toString()); + } catch (Exception e) { + return null; + } + } +} diff --git a/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/UrlEncodeFunction.java b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/UrlEncodeFunction.java new file mode 100644 index 00000000000..4dabaee8d4a --- /dev/null +++ b/inlong-sdk/transform-sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/UrlEncodeFunction.java @@ -0,0 +1,53 @@ +package org.apache.inlong.sdk.transform.process.function; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +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; + +/** + * UrlEncodeFunction + * description: Translates a string into ‘application/x-www-form-urlencoded’ format using the UTF-8 encoding scheme. + * If the input is NULL, or there is an issue with the encoding process, + * or the encoding scheme is not supported, will return NULL. + */ +@TransformFunction(names = {"url_encode"}) +public class UrlEncodeFunction implements ValueParser { + + private final ValueParser stringParser; + + public UrlEncodeFunction(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); + if (stringObj == null) { + return null; + } + + String string = OperatorTools.parseString(stringObj); + if (string == null) { + return null; + } + + try { + return URLEncoder.encode(string, StandardCharsets.UTF_8.toString()); + } catch (Exception e) { + return null; + } + } + + public static void main(String[] args) throws UnsupportedEncodingException { + String string = "https://www.google.com/search?q=java url encode"; + String s = "bat apache"; + System.out.println(URLEncoder.encode(string, StandardCharsets.UTF_8.toString())); + System.out.println(URLEncoder.encode(s, StandardCharsets.UTF_8.toString())); + } +} + diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestUrlDecodeFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestUrlDecodeFunction.java new file mode 100644 index 00000000000..1bec2fdc48a --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestUrlDecodeFunction.java @@ -0,0 +1,38 @@ +package org.apache.inlong.sdk.transform.process.function.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestUrlDecodeFunction extends AbstractFunctionStringTestBase { + + @Test + public void testUrlDecodeFunction() throws Exception { + String transformSql = "select url_decode(string1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + // case1: url_decode('https%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Djava+url+encode') + List output1 = processor.transform("https%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Djava+url+encode|banana|cloud|1", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=https://www.google.com/search?q=java url encode"); + + String transformSql2 = "select url_decode(stringX) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: url_decode(null) -> null + List output2 = processor2.transform("|apple|banana|cloud|1", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=null"); + } +} diff --git a/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestUrlEncodeFunction.java b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestUrlEncodeFunction.java new file mode 100644 index 00000000000..236b0975d2d --- /dev/null +++ b/inlong-sdk/transform-sdk/src/test/java/org/apache/inlong/sdk/transform/process/function/string/TestUrlEncodeFunction.java @@ -0,0 +1,38 @@ +package org.apache.inlong.sdk.transform.process.function.string; + +import org.apache.inlong.sdk.transform.decode.SourceDecoderFactory; +import org.apache.inlong.sdk.transform.encode.SinkEncoderFactory; +import org.apache.inlong.sdk.transform.pojo.TransformConfig; +import org.apache.inlong.sdk.transform.process.TransformProcessor; +import org.junit.Assert; +import org.junit.Test; + +import java.util.HashMap; +import java.util.List; + +public class TestUrlEncodeFunction extends AbstractFunctionStringTestBase { + + @Test + public void testUrlEncodeFunction() throws Exception { + String transformSql = "select url_encode(string1) from source"; + TransformConfig config = new TransformConfig(transformSql); + TransformProcessor processor = TransformProcessor + .create(config, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + + // case1: url_encode('https://www.google.com/search?q=java url encode') + List output1 = processor.transform("https://www.google.com/search?q=java url encode|banana|cloud|1", new HashMap<>()); + Assert.assertEquals(1, output1.size()); + Assert.assertEquals(output1.get(0), "result=https%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Djava+url+encode"); + + String transformSql2 = "select url_encode(stringX) from source"; + TransformConfig config2 = new TransformConfig(transformSql2); + TransformProcessor processor2 = TransformProcessor + .create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), + SinkEncoderFactory.createKvEncoder(kvSink)); + // case2: url_encode(null) -> null + List output2 = processor2.transform("apple|banana|cloud|1", new HashMap<>()); + Assert.assertEquals(1, output2.size()); + Assert.assertEquals(output2.get(0), "result=null"); + } +}