-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Shivani Shukla <[email protected]>
- Loading branch information
Showing
6 changed files
with
210 additions
and
0 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
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
46 changes: 46 additions & 0 deletions
46
...main/java/com/amazon/dataprepper/plugins/processor/mutatestring/SplitStringProcessor.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,46 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.processor.mutatestring; | ||
|
||
import com.amazon.dataprepper.metrics.PluginMetrics; | ||
import com.amazon.dataprepper.model.annotations.DataPrepperPlugin; | ||
import com.amazon.dataprepper.model.annotations.DataPrepperPluginConstructor; | ||
import com.amazon.dataprepper.model.event.Event; | ||
import com.amazon.dataprepper.model.processor.Processor; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
|
||
@DataPrepperPlugin(name = "split_string", pluginType = Processor.class, pluginConfigurationType = SplitStringProcessorConfig.class) | ||
public class SplitStringProcessor extends AbstractStringProcessor<SplitStringProcessorConfig.Entry> { | ||
|
||
private final Map<String, Pattern> patternMap; | ||
|
||
@DataPrepperPluginConstructor | ||
public SplitStringProcessor(final PluginMetrics pluginMetrics, final StringProcessorConfig<SplitStringProcessorConfig.Entry> config) { | ||
super(pluginMetrics, config); | ||
|
||
patternMap = new HashMap<>(); | ||
for (SplitStringProcessorConfig.Entry entry: config.getIterativeConfig()) { | ||
patternMap.put(entry.getDelimiter(), Pattern.compile(entry.getDelimiter())); | ||
} | ||
} | ||
|
||
@Override | ||
protected void performKeyAction(final Event recordEvent, final SplitStringProcessorConfig.Entry entry, final String value) { | ||
|
||
final Pattern pattern = patternMap.get(entry.getDelimiter()); | ||
final String[] splitValue = pattern.split(value); | ||
recordEvent.put(entry.getSource(), splitValue); | ||
} | ||
|
||
@Override | ||
protected String getKey(final SplitStringProcessorConfig.Entry entry) { | ||
return entry.getSource(); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
...ava/com/amazon/dataprepper/plugins/processor/mutatestring/SplitStringProcessorConfig.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 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.processor.mutatestring; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
import java.util.List; | ||
|
||
public class SplitStringProcessorConfig { | ||
public static class Entry { | ||
|
||
@NotEmpty | ||
@NotNull | ||
private String source; | ||
|
||
@NotEmpty | ||
@NotNull | ||
private String delimiter; | ||
|
||
public String getSource() { | ||
return source; | ||
} | ||
|
||
public String getDelimiter() { | ||
return delimiter; | ||
} | ||
|
||
public Entry(final String source, final String delimiter) { | ||
this.source = source; | ||
this.delimiter = delimiter; | ||
} | ||
|
||
public Entry() {}; | ||
|
||
private List<Entry> entries; | ||
|
||
public List<Entry> getEntries() { | ||
return entries; | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...java/com/amazon/dataprepper/plugins/processor/mutatestring/SplitStringProcessorTests.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,89 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.processor.mutatestring; | ||
|
||
import com.amazon.dataprepper.metrics.PluginMetrics; | ||
import com.amazon.dataprepper.model.event.Event; | ||
import com.amazon.dataprepper.model.event.JacksonEvent; | ||
import com.amazon.dataprepper.model.record.Record; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.ArgumentsProvider; | ||
import org.junit.jupiter.params.provider.ArgumentsSource; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.CoreMatchers.notNullValue; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class SplitStringProcessorTests { | ||
|
||
@Mock | ||
private PluginMetrics pluginMetrics; | ||
|
||
@Mock | ||
private StringProcessorConfig<SplitStringProcessorConfig.Entry> config; | ||
|
||
private SplitStringProcessor createObjectUnderTest() { | ||
return new SplitStringProcessor(pluginMetrics, config); | ||
} | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(SplitStringArgumentsProvider.class) | ||
void testSingleSplitProcessor(String message, List<String> splitMessage) { | ||
|
||
when(config.getIterativeConfig()).thenReturn(Collections.singletonList(createEntry("message", ","))); | ||
|
||
final SplitStringProcessor splitStringProcessor = createObjectUnderTest(); | ||
final Record<Event> record = createEvent(message); | ||
final List<Record<Event>> splitRecords = (List<Record<Event>>) splitStringProcessor.doExecute(Collections.singletonList(record)); | ||
|
||
assertThat(splitRecords.get(0).getData().get("message", Object.class), notNullValue()); | ||
assertThat(splitRecords.get(0).getData().get("message", Object.class), equalTo(splitMessage)); | ||
} | ||
|
||
private SplitStringProcessorConfig.Entry createEntry(final String source, final String delimiter) { | ||
return new SplitStringProcessorConfig.Entry(source, delimiter); | ||
} | ||
|
||
private Record<Event> createEvent(String message) { | ||
final Map<String, Object> eventData = new HashMap<>(); | ||
eventData.put("message", message); | ||
return new Record<>(JacksonEvent.builder() | ||
.withEventType("event") | ||
.withData(eventData) | ||
.build()); | ||
} | ||
|
||
static class SplitStringArgumentsProvider implements ArgumentsProvider { | ||
|
||
@Override | ||
public Stream<? extends Arguments> provideArguments(ExtensionContext context) { | ||
return Stream.of( | ||
Arguments.arguments("hello,world,no-split", Arrays.asList("hello","world","no-split")), | ||
Arguments.arguments("hello,world", Arrays.asList("hello", "world")), | ||
Arguments.arguments("hello,,world", Arrays.asList("hello","","world")), | ||
Arguments.arguments("hello,", Arrays.asList("hello")), | ||
Arguments.arguments("hello,,", Arrays.asList("hello")), | ||
Arguments.arguments(",hello", Arrays.asList("","hello")), | ||
Arguments.arguments(",,hello", Arrays.asList("","","hello")) | ||
); | ||
} | ||
} | ||
|
||
} |