forked from apache/inlong
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INLONG-11028][SDK] Transform SQL support UrlEncode & UrlDecode funct…
…ions
- Loading branch information
1 parent
179e478
commit 310a9a2
Showing
4 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/UrlDecodeFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...sdk/src/main/java/org/apache/inlong/sdk/transform/process/function/UrlEncodeFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
} | ||
|
38 changes: 38 additions & 0 deletions
38
...t/java/org/apache/inlong/sdk/transform/process/function/string/TestUrlDecodeFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String> 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<String> 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<String, String> processor2 = TransformProcessor | ||
.create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
// case2: url_decode(null) -> null | ||
List<String> output2 = processor2.transform("|apple|banana|cloud|1", new HashMap<>()); | ||
Assert.assertEquals(1, output2.size()); | ||
Assert.assertEquals(output2.get(0), "result=null"); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...t/java/org/apache/inlong/sdk/transform/process/function/string/TestUrlEncodeFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, String> processor = TransformProcessor | ||
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
|
||
// case1: url_encode('https://www.google.com/search?q=java url encode') | ||
List<String> 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<String, String> processor2 = TransformProcessor | ||
.create(config2, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
// case2: url_encode(null) -> null | ||
List<String> output2 = processor2.transform("apple|banana|cloud|1", new HashMap<>()); | ||
Assert.assertEquals(1, output2.size()); | ||
Assert.assertEquals(output2.get(0), "result=null"); | ||
} | ||
} |