-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for spilt event processor
Signed-off-by: srigovs <[email protected]>
- Loading branch information
1 parent
680ad7a
commit 458b5de
Showing
5 changed files
with
595 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
plugins { | ||
id 'java' | ||
} | ||
|
||
jacocoTestCoverageVerification { | ||
dependsOn jacocoTestReport | ||
violationRules { | ||
rule { | ||
limit { | ||
minimum = 1.0 | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
dependencies { | ||
implementation project(':data-prepper-api') | ||
implementation project(':data-prepper-plugins:common') | ||
implementation 'com.fasterxml.jackson.core:jackson-databind' | ||
} | ||
|
||
test { | ||
useJUnitPlatform() | ||
} |
127 changes: 127 additions & 0 deletions
127
...ain/java/org/opensearch/dataprepper/plugins/processor/splitevent/SplitEventProcessor.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,127 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.processor.splitevent; | ||
|
||
import org.opensearch.dataprepper.metrics.PluginMetrics; | ||
import org.opensearch.dataprepper.model.annotations.DataPrepperPlugin; | ||
import org.opensearch.dataprepper.model.annotations.DataPrepperPluginConstructor; | ||
import org.opensearch.dataprepper.model.event.DefaultEventHandle; | ||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.event.JacksonEvent; | ||
import org.opensearch.dataprepper.model.processor.AbstractProcessor; | ||
import org.opensearch.dataprepper.model.processor.Processor; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.regex.Pattern; | ||
|
||
|
||
@DataPrepperPlugin(name = "split_event", pluginType = Processor.class, pluginConfigurationType = SplitEventProcessorConfig.class) | ||
public class SplitEventProcessor extends AbstractProcessor<Record<Event>, Record<Event>>{ | ||
final Pattern pattern; | ||
final String delimiter; | ||
final String delimiterRegex; | ||
final String field; | ||
|
||
@DataPrepperPluginConstructor | ||
public SplitEventProcessor(final PluginMetrics pluginMetrics, final SplitEventProcessorConfig config) { | ||
super(pluginMetrics); | ||
this.delimiter = config.getDelimiter(); | ||
this.delimiterRegex = config.getDelimiterRegex(); | ||
this.field = config.getField(); | ||
|
||
if(delimiterRegex != null && !delimiterRegex.isEmpty() | ||
&& delimiter != null && !delimiter.isEmpty()) { | ||
throw new IllegalArgumentException("delimiter and delimiter_regex cannot be defined at the same time"); | ||
} else if((delimiterRegex == null || delimiterRegex.isEmpty()) && | ||
(delimiter == null || delimiter.isEmpty())) { | ||
throw new IllegalArgumentException("delimiter or delimiter_regex needs to be defined"); | ||
} | ||
|
||
if(delimiterRegex != null && !delimiterRegex.isEmpty()) { | ||
pattern = Pattern.compile(delimiterRegex); | ||
} else { | ||
pattern = Pattern.compile(Pattern.quote(delimiter)); | ||
} | ||
} | ||
|
||
@Override | ||
public Collection<Record<Event>> doExecute(final Collection<Record<Event>> records) { | ||
Collection<Record<Event>> newRecords = new ArrayList<>(); | ||
for(final Record<Event> record : records) { | ||
final Event recordEvent = record.getData(); | ||
|
||
if (!recordEvent.containsKey(field)) { | ||
Record newRecord = new Record<>(recordEvent); | ||
newRecords.add(newRecord); | ||
continue; | ||
} | ||
|
||
final Object value = recordEvent.get(field, Object.class); | ||
|
||
//split record according to delimiter | ||
final String[] splitValues = pattern.split((String) value); | ||
|
||
// when no splits or empty value modify the original event | ||
if(splitValues.length <= 1) { | ||
Record newRecord = new Record<>(recordEvent); | ||
newRecords.add(newRecord); | ||
continue; | ||
} | ||
|
||
//create new events for the splits | ||
for (int i = 0; i < splitValues.length-1 ; i++) { | ||
Record newRecord = createNewRecordFromEvent(recordEvent, splitValues[i]); | ||
addToAcknowledgementSetFromOriginEvent((Event) newRecord.getData(), recordEvent); | ||
newRecords.add(newRecord); | ||
} | ||
|
||
// Modify original event to hold the last split | ||
recordEvent.put(field, splitValues[splitValues.length-1]); | ||
Record newRecord = new Record<>(recordEvent); | ||
newRecords.add(newRecord); | ||
} | ||
return newRecords; | ||
} | ||
|
||
protected Record createNewRecordFromEvent(final Event recordEvent, String splitValue) { | ||
Record newRecord; | ||
JacksonEvent newRecordEvent; | ||
|
||
newRecordEvent = JacksonEvent.fromEvent(recordEvent); | ||
newRecordEvent.put(field,(Object) splitValue); | ||
newRecord = new Record<>(newRecordEvent); | ||
return newRecord; | ||
} | ||
|
||
protected void addToAcknowledgementSetFromOriginEvent(Event recordEvent, Event originRecordEvent) { | ||
DefaultEventHandle eventHandle = (DefaultEventHandle) originRecordEvent.getEventHandle(); | ||
if (eventHandle != null && eventHandle.getAcknowledgementSet() != null) { | ||
eventHandle.getAcknowledgementSet().add(recordEvent); | ||
} | ||
} | ||
|
||
@Override | ||
public void prepareForShutdown() { | ||
} | ||
|
||
@Override | ||
public boolean isReadyForShutdown() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...va/org/opensearch/dataprepper/plugins/processor/splitevent/SplitEventProcessorConfig.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,42 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.processor.splitevent; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Size; | ||
|
||
|
||
public class SplitEventProcessorConfig { | ||
@NotEmpty | ||
@NotNull | ||
@JsonProperty("field") | ||
private String field; | ||
|
||
@JsonProperty("delimiter_regex") | ||
private String delimiterRegex; | ||
|
||
@Size(min = 1, max = 1) | ||
private String delimiter; | ||
|
||
public String getField() { | ||
return field; | ||
} | ||
|
||
public String getDelimiterRegex() { | ||
return delimiterRegex; | ||
} | ||
|
||
public String getDelimiter() { | ||
return delimiter; | ||
} | ||
} |
Oops, something went wrong.