|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.inlong.sdk.transform.process.function.string; |
| 19 | + |
| 20 | +import org.apache.inlong.sdk.transform.decode.SourceData; |
| 21 | +import org.apache.inlong.sdk.transform.process.Context; |
| 22 | +import org.apache.inlong.sdk.transform.process.function.FunctionConstant; |
| 23 | +import org.apache.inlong.sdk.transform.process.function.TransformFunction; |
| 24 | +import org.apache.inlong.sdk.transform.process.operator.OperatorTools; |
| 25 | +import org.apache.inlong.sdk.transform.process.parser.ValueParser; |
| 26 | + |
| 27 | +import com.google.gson.JsonObject; |
| 28 | +import net.sf.jsqlparser.expression.Expression; |
| 29 | +import net.sf.jsqlparser.expression.Function; |
| 30 | + |
| 31 | +import java.util.List; |
| 32 | +/** |
| 33 | + * StrToJsonFunction -> str_to_json(str, pairDelimiter, keyValueDelimiter) |
| 34 | + * description: |
| 35 | + * - Return NULL if 'str' is NULL |
| 36 | + * - Return a json string after splitting the 'str' into key/value pairs using 'pairDelimiter'(default is ',') |
| 37 | + * and 'keyValueDelimiter'(default is '=') |
| 38 | + * Note: Both 'pairDelimiter' and 'keyValueDelimiter' are treated as regular expressions.So special characters |
| 39 | + * (e.g. <([{^-=$!|]})?*+.>) need to be properly escaped before using as a delimiter literally. |
| 40 | + */ |
| 41 | +@TransformFunction(type = FunctionConstant.STRING_TYPE, names = { |
| 42 | + "str_to_json"}, parameter = "(String s1, String pairDelimiter, String keyValueDelimiter)", descriptions = { |
| 43 | + "- Return \"\" if 'str' is NULL;", |
| 44 | + "- Return a json string after splitting the 'str' into key/value pairs using 'pairDelimiter'(default is ',') " |
| 45 | + + |
| 46 | + "and 'keyValueDelimiter'(default is '=');", |
| 47 | + "Note: Both 'pairDelimiter' and 'keyValueDelimiter' are treated as regular expressions.So special " + |
| 48 | + "characters(e.g. <([{^-=$!|]})?*+.>) need to be properly escaped before using as a delimiter literally." |
| 49 | + }, examples = { |
| 50 | + "str_to_json('key1=value1,key2=value2,key3=value3') = {\"key1\":\"value1\",\"key2\":\"value2\",\"key3\"=\"value3\"}", |
| 51 | + "str_to_json(\"name->John!age->30!city->China\" , \"!\" , \"->\") = {\"name\":\"John\",\"age\":\"30\",\"city\":\"China\"}" |
| 52 | + }) |
| 53 | +public class StrToJsonFunction implements ValueParser { |
| 54 | + |
| 55 | + private ValueParser inputParser; |
| 56 | + |
| 57 | + private ValueParser pairDelimiterParser; |
| 58 | + |
| 59 | + private ValueParser kvDelimiterParser; |
| 60 | + |
| 61 | + public StrToJsonFunction(Function expr) { |
| 62 | + List<Expression> expressions = expr.getParameters().getExpressions(); |
| 63 | + if (!expressions.isEmpty()) { |
| 64 | + inputParser = OperatorTools.buildParser(expressions.get(0)); |
| 65 | + if (expressions.size() >= 2) { |
| 66 | + pairDelimiterParser = OperatorTools.buildParser(expressions.get(1)); |
| 67 | + if (expressions.size() >= 3) { |
| 68 | + kvDelimiterParser = OperatorTools.buildParser(expressions.get(2)); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public Object parse(SourceData sourceData, int rowIndex, Context context) { |
| 76 | + Object inputStringObj = inputParser.parse(sourceData, rowIndex, context); |
| 77 | + Object pairDelimiterStringObj = null; |
| 78 | + String pairDelimiterString = null; |
| 79 | + if (pairDelimiterParser != null) { |
| 80 | + pairDelimiterStringObj = pairDelimiterParser.parse(sourceData, rowIndex, context); |
| 81 | + pairDelimiterString = OperatorTools.parseString(pairDelimiterStringObj); |
| 82 | + } |
| 83 | + Object kvDelimiterStringObj = null; |
| 84 | + String kvDelimiterString = null; |
| 85 | + if (kvDelimiterParser != null) { |
| 86 | + kvDelimiterStringObj = kvDelimiterParser.parse(sourceData, rowIndex, context); |
| 87 | + kvDelimiterString = OperatorTools.parseString(kvDelimiterStringObj); |
| 88 | + } |
| 89 | + String inputString = OperatorTools.parseString(inputStringObj); |
| 90 | + |
| 91 | + return parse2Json(pairDelimiterString, kvDelimiterString, inputString); |
| 92 | + } |
| 93 | + |
| 94 | + private JsonObject parse2Json(String pairDelimiterString, String kvDelimiterString, |
| 95 | + String inputString) { |
| 96 | + String pairDelimiter = |
| 97 | + (pairDelimiterString == null || pairDelimiterString.isEmpty()) ? "," : escapeRegex(pairDelimiterString); |
| 98 | + String keyValueDelimiter = |
| 99 | + (kvDelimiterString == null || kvDelimiterString.isEmpty()) ? "=" : escapeRegex(kvDelimiterString); |
| 100 | + |
| 101 | + JsonObject json = new JsonObject(); |
| 102 | + String[] pairs = inputString.split(pairDelimiter); |
| 103 | + |
| 104 | + for (String pair : pairs) { |
| 105 | + if (pair.contains(keyValueDelimiter)) { |
| 106 | + String[] keyValue = pair.split(keyValueDelimiter, 2); |
| 107 | + json.addProperty(keyValue[0], keyValue[1]); |
| 108 | + } |
| 109 | + } |
| 110 | + return json; |
| 111 | + } |
| 112 | + |
| 113 | + private String escapeRegex(String delimiter) { |
| 114 | + return delimiter.replaceAll("([\\\\^$|?*+\\[\\](){}])", "\\\\$1"); |
| 115 | + } |
| 116 | +} |
0 commit comments