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-11303][SDK] Transform SQL supports "JSON_REMOVE" function
- Loading branch information
ZKpLo
committed
Oct 12, 2024
1 parent
46d6881
commit 2b8ede6
Showing
6 changed files
with
196 additions
and
8 deletions.
There are no files selected for viewing
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
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
96 changes: 96 additions & 0 deletions
96
...c/main/java/org/apache/inlong/sdk/transform/process/function/json/JsonRemoveFunction.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,96 @@ | ||
/* | ||
* 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.json; | ||
|
||
import org.apache.inlong.sdk.transform.decode.SourceData; | ||
import org.apache.inlong.sdk.transform.process.Context; | ||
import org.apache.inlong.sdk.transform.process.function.TransformFunction; | ||
import org.apache.inlong.sdk.transform.process.operator.OperatorTools; | ||
import org.apache.inlong.sdk.transform.process.parser.ValueParser; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
import com.alibaba.fastjson.JSONPath; | ||
import net.sf.jsqlparser.expression.Expression; | ||
import net.sf.jsqlparser.expression.Function; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* JsonRemoveFunction -> JSON_REMOVE(json_doc, path[, path] ...) | ||
* description: | ||
* - return NULL if any argument is NULL or the 'json_doc' argument is not a valid JSON document or any path argument | ||
* is not a valid path expression or is $ or contains a * or ** wildcard; | ||
* - return the result of removing data from 'json_doc'. | ||
*/ | ||
@TransformFunction(names = { | ||
"json_remove"}, parameter = "(String json_doc, String path1[, String path2, ...])", descriptions = { | ||
"- Return \"\" if any argument is NULL or the 'json_doc' argument is not a valid JSON document or any " | ||
+ | ||
"path argument is not a valid path expression or is $ or contains a * or ** wildcard;", | ||
"- Return the result of removing data from 'json_doc'." | ||
}, examples = { | ||
"json_remove(\"{\\\"name\\\":\\\"Charlie\\\",\\\"hobbies\\\":[[\\\"swimming1\\\",\\\"swimming2\\\"]," + | ||
"\\\"reading\\\",\\\"coding\\\"]}\",\"$.age\") = {\"hobbies\":[[\"swimming2\"],\"coding\"]," + | ||
"\"name\":\"Charlie\"}" | ||
}) | ||
public class JsonRemoveFunction implements ValueParser { | ||
|
||
private ValueParser jsonDocParser; | ||
private List<ValueParser> pathsParser; | ||
|
||
public JsonRemoveFunction(Function expr) { | ||
List<Expression> expressions = expr.getParameters().getExpressions(); | ||
jsonDocParser = OperatorTools.buildParser(expressions.get(0)); | ||
pathsParser = new ArrayList<>(); | ||
for (int i = 1; i < expressions.size(); i++) { | ||
pathsParser.add(OperatorTools.buildParser(expressions.get(i))); | ||
} | ||
} | ||
|
||
@Override | ||
public Object parse(SourceData sourceData, int rowIndex, Context context) { | ||
Object jsonDocObj = jsonDocParser.parse(sourceData, rowIndex, context); | ||
if (jsonDocObj == null) { | ||
return null; | ||
} | ||
ArrayList<String> pathValuePairs = new ArrayList<>(); | ||
for (ValueParser valueParser : pathsParser) { | ||
pathValuePairs.add(valueParser.parse(sourceData, rowIndex, context).toString()); | ||
} | ||
return jsonRemove(jsonDocObj.toString(), pathValuePairs); | ||
} | ||
|
||
private String jsonRemove(String jsonDoc, ArrayList<String> paths) { | ||
if (jsonDoc == null || paths == null) { | ||
return null; | ||
} | ||
|
||
Object json = JSON.parse(jsonDoc); | ||
|
||
for (String path : paths) { | ||
if (path.equals("$") || path.contains("*") || path.contains("**")) { | ||
throw new IllegalArgumentException("Invalid path expression: " + path); | ||
} | ||
|
||
JSONPath.remove(json, path); | ||
} | ||
|
||
return json.toString(); | ||
} | ||
} |
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
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
77 changes: 77 additions & 0 deletions
77
...st/java/org/apache/inlong/sdk/transform/process/function/json/TestJsonRemoveFunction.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,77 @@ | ||
/* | ||
* 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.json; | ||
|
||
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 TestJsonRemoveFunction extends AbstractFunctionJsonTestBase { | ||
|
||
@Test | ||
public void testJsonArrayAppendFunction() throws Exception { | ||
String transformSql = null, data = null; | ||
TransformConfig config = null; | ||
TransformProcessor<String, String> processor = null; | ||
List<String> output = null; | ||
|
||
String json_doc = | ||
"{\\\"name\\\":\\\"Alice\\\",\\\"age\\\":30,\\\"address\\\":{\\\"city\\\":\\\"Wonderland\\\",\\\"zip\\\":\\\"12345\\\"}}"; | ||
transformSql = "select json_remove(string1,string2) from source"; | ||
config = new TransformConfig(transformSql); | ||
processor = TransformProcessor | ||
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
// case1: json_remove("{\"name\":\"Alice\",\"age\":30,\"address\":{\"city\":\"Wonderland\",\"zip\":\"12345\"}}", | ||
// "$.age") | ||
data = json_doc + "|$.age|"; | ||
output = processor.transform(data, new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result={\"address\":{\"zip\":\"12345\",\"city\":\"Wonderland\"},\"name\":\"Alice\"}", | ||
output.get(0)); | ||
|
||
// case2: json_remove("{\"name\":\"Alice\",\"age\":30,\"address\":{\"city\":\"Wonderland\",\"zip\":\"12345\"}}", | ||
// "$.address.zip") | ||
data = json_doc + "|$.address.zip|"; | ||
output = processor.transform(data, new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result={\"address\":{\"city\":\"Wonderland\"},\"name\":\"Alice\",\"age\":30}", | ||
output.get(0)); | ||
|
||
json_doc = | ||
"{\\\"name\\\":\\\"Charlie\\\",\\\"hobbies\\\":[[\\\"swimming1\\\",\\\"swimming2\\\"],\\\"reading\\\",\\\"coding\\\"]}"; | ||
transformSql = "select json_remove(string1,string2,string3) from source"; | ||
config = new TransformConfig(transformSql); | ||
processor = TransformProcessor | ||
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
// case3: | ||
// json_remove("{\"name\":\"Charlie\",\"hobbies\":[[\"swimming1\",\"swimming2\"],\"reading\",\"coding\"]}","$.age") | ||
data = json_doc + "|$.hobbies[1]|$.hobbies[0][0]|"; | ||
output = processor.transform(data, new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result={\"hobbies\":[[\"swimming2\"],\"coding\"],\"name\":\"Charlie\"}", output.get(0)); | ||
} | ||
} |