Skip to content

Commit

Permalink
[INLONG-10816][SDK] Transform support Replace function.(apache#10816)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ybszzzziz committed Aug 20, 2024
1 parent f16215d commit 0d857c4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,35 @@
*/
public class ReplaceFunction implements ValueParser {

private ValueParser stringParser1;
private ValueParser stringParser2;
private ValueParser stringParser3;
private ValueParser stringParser;
private ValueParser targetParser;
private ValueParser replacementParser;

/**
* Constructor
* @param expr
*/
public ReplaceFunction(Function expr) {
List<Expression> expressions = expr.getParameters().getExpressions();
stringParser1 = OperatorTools.buildParser(expressions.get(0));
stringParser2 = OperatorTools.buildParser(expressions.get(1));
stringParser3 = OperatorTools.buildParser(expressions.get(2));
stringParser = OperatorTools.buildParser(expressions.get(0));
targetParser = OperatorTools.buildParser(expressions.get(1));
replacementParser = OperatorTools.buildParser(expressions.get(2));
}

/**
* parse
* @param sourceData
* @param rowIndex
* @return
*/
@Override
public Object parse(SourceData sourceData, int rowIndex, Context context) {
Object stringObj1 = stringParser1.parse(sourceData, rowIndex, context);
Object stringObj2 = stringParser2.parse(sourceData, rowIndex, context);
Object stringObj3 = stringParser3.parse(sourceData, rowIndex, context);
String str1 = OperatorTools.parseString(stringObj1);
String str2 = OperatorTools.parseString(stringObj2);
String str3 = OperatorTools.parseString(stringObj3);
return str1.replace(str2.charAt(0), str3.charAt(0));
Object stringObj1 = stringParser.parse(sourceData, rowIndex, context);
Object stringObj2 = targetParser.parse(sourceData, rowIndex, context);
Object stringObj3 = replacementParser.parse(sourceData, rowIndex, context);
String str = OperatorTools.parseString(stringObj1);
String target = OperatorTools.parseString(stringObj2);
String replacement = OperatorTools.parseString(stringObj3);
return str.replace(target, replacement);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ public void testReplaceFunction() throws Exception {
TransformProcessor<String, String> processor = TransformProcessor
.create(config, SourceDecoderFactory.createCsvDecoder(csvSource),
SinkEncoderFactory.createKvEncoder(kvSink));
// case1: replace('hooray', 'o', 'a')
List<String> output = processor.transform("hooray|o|a", new HashMap<>());
// case1: replace('hooray', 'oray', 'lly')
List<String> output = processor.transform("hooray|oray|lly", new HashMap<>());
Assert.assertEquals(1, output.size());
Assert.assertEquals(output.get(0), "result=haaray");
Assert.assertEquals(output.get(0), "result=holly");

}

Expand Down

0 comments on commit 0d857c4

Please sign in to comment.