-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[INLONG-11305][SDK] Transform SQL supports "JSON_SET" function (#11346)
- Loading branch information
Showing
2 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
.../src/main/java/org/apache/inlong/sdk/transform/process/function/json/JsonSetFunction.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,105 @@ | ||
/* | ||
* 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; | ||
|
||
/** | ||
* JsonSetFunction -> JSON_SET(json_doc, path, val[, path, val, ...] ) | ||
* 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 contains a * or ** wildcard; | ||
* - return the result of inserting or updating data in 'json_doc'. | ||
*/ | ||
@TransformFunction(names = { | ||
"json_set"}, parameter = "(String json_doc, String path1, String val1[, String path2, String val2, ...] )", 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 contains a * or ** wildcard;", | ||
"- Return the result of inserting or updating data in 'json_doc'." | ||
}, examples = { | ||
"json_set({\\\"name\\\":\\\"Alice\\\"},\"$.name\",\"inlong\") = {\"name\":\"inlong\"}" | ||
}) | ||
public class JsonSetFunction implements ValueParser { | ||
|
||
private ValueParser jsonDocParser; | ||
private List<ValueParser> pathValuePairsParser; | ||
|
||
public JsonSetFunction(Function expr) { | ||
List<Expression> expressions = expr.getParameters().getExpressions(); | ||
jsonDocParser = OperatorTools.buildParser(expressions.get(0)); | ||
pathValuePairsParser = new ArrayList<>(); | ||
for (int i = 1; i < expressions.size(); i++) { | ||
pathValuePairsParser.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<Object> pathValuePairs = new ArrayList<>(); | ||
for (ValueParser valueParser : pathValuePairsParser) { | ||
pathValuePairs.add(valueParser.parse(sourceData, rowIndex, context)); | ||
} | ||
return jsonSet(jsonDocObj.toString(), pathValuePairs); | ||
} | ||
|
||
private String jsonSet(String jsonDoc, ArrayList<Object> pathValuePairs) { | ||
if (jsonDoc == null || pathValuePairs == null || pathValuePairs.size() % 2 != 0) { | ||
return null; | ||
} | ||
|
||
Object json = JSON.parse(jsonDoc); | ||
|
||
for (int i = 0; i < pathValuePairs.size(); i += 2) { | ||
String path = (String) pathValuePairs.get(i); | ||
Object value = pathValuePairs.get(i + 1); | ||
|
||
if (path == null || path.contains("*") || path.contains("**")) { | ||
throw new IllegalArgumentException("Invalid path expression: " + path); | ||
} | ||
if ("$".equals(path)) { | ||
json = JSON.parse(value.toString()); | ||
continue; | ||
} | ||
// Insert or update data, if the path does not exist, insert new data | ||
try { | ||
JSONPath.set(json, path, JSON.parse(value.toString())); | ||
} catch (Exception ignored) { | ||
JSONPath.set(json, path, value); | ||
} | ||
} | ||
|
||
return json.toString(); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
.../test/java/org/apache/inlong/sdk/transform/process/function/json/TestJsonSetFunction.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,73 @@ | ||
/* | ||
* 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 TestJsonSetFunction extends AbstractFunctionJsonTestBase { | ||
|
||
@Test | ||
public void testJsonSetFunction() throws Exception { | ||
String transformSql = null, data = null; | ||
TransformConfig config = null; | ||
TransformProcessor<String, String> processor = null; | ||
List<String> output = null; | ||
|
||
transformSql = "select json_set(string1,string2,string3) from source"; | ||
config = new TransformConfig(transformSql); | ||
processor = TransformProcessor | ||
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
|
||
// case1: json_set(json_doc,$.address,Wonderland) | ||
String json_doc = "{\\\"name\\\":\\\"Alice\\\"}"; | ||
data = json_doc + "|$.address|Wonderland|"; | ||
output = processor.transform(data, new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result={\"address\":\"Wonderland\",\"name\":\"Alice\"}", output.get(0)); | ||
|
||
// case2: json_set(json_doc,$.name,inlong) | ||
json_doc = "{\\\"name\\\":\\\"Alice\\\"}"; | ||
data = json_doc + "|$.name|inlong|"; | ||
output = processor.transform(data, new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result={\"name\":\"inlong\"}", output.get(0)); | ||
|
||
transformSql = "select json_set(string1,string2,string3,numeric1,numeric2) from source"; | ||
config = new TransformConfig(transformSql); | ||
processor = TransformProcessor | ||
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource), | ||
SinkEncoderFactory.createKvEncoder(kvSink)); | ||
|
||
// case3: json_set(json_doc,$,{"newKey":"newValue"},$.newKey,"new1") | ||
json_doc = "{\\\"name\\\":\\\"Alice\\\"}"; | ||
data = json_doc + "|$|{\\\"newKey\\\":\\\"newValue\\\"}|\"$.newKey\"|\"new1\""; | ||
output = processor.transform(data, new HashMap<>()); | ||
Assert.assertEquals(1, output.size()); | ||
Assert.assertEquals("result={\"newKey\":\"new1\"}", output.get(0)); | ||
} | ||
} |