-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add map to list processor basic functionality and unit tests Signed-off-by: Hai Yan <[email protected]>
- Loading branch information
Showing
4 changed files
with
421 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
94 changes: 94 additions & 0 deletions
94
...ain/java/org/opensearch/dataprepper/plugins/processor/mutateevent/MapToListProcessor.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,94 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.processor.mutateevent; | ||
|
||
import org.opensearch.dataprepper.expression.ExpressionEvaluator; | ||
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.Event; | ||
import org.opensearch.dataprepper.model.processor.AbstractProcessor; | ||
import org.opensearch.dataprepper.model.processor.Processor; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
@DataPrepperPlugin(name = "map_to_list", pluginType = Processor.class, pluginConfigurationType = MapToListProcessorConfig.class) | ||
public class MapToListProcessor extends AbstractProcessor<Record<Event>, Record<Event>> { | ||
private static final Logger LOG = LoggerFactory.getLogger(MapToListProcessor.class); | ||
private final MapToListProcessorConfig config; | ||
private final ExpressionEvaluator expressionEvaluator; | ||
private final Set<String> excludeKeySet = new HashSet<>(); | ||
|
||
@DataPrepperPluginConstructor | ||
public MapToListProcessor(final PluginMetrics pluginMetrics, final MapToListProcessorConfig config, final ExpressionEvaluator expressionEvaluator) { | ||
super(pluginMetrics); | ||
this.config = config; | ||
this.expressionEvaluator = expressionEvaluator; | ||
excludeKeySet.addAll(config.getExcludeKeys()); | ||
} | ||
|
||
@Override | ||
public Collection<Record<Event>> doExecute(final Collection<Record<Event>> records) { | ||
for (final Record<Event> record : records) { | ||
final Event recordEvent = record.getData(); | ||
|
||
if (config.getMapToListWhen() != null && !expressionEvaluator.evaluateConditional(config.getMapToListWhen(), recordEvent)) { | ||
continue; | ||
} | ||
|
||
try { | ||
final Map<String, Object> sourceMap = recordEvent.get(config.getSource(), Map.class); | ||
final List<Map<String, Object>> targetList = new ArrayList<>(); | ||
|
||
Map<String, Object> modifiedSourceMap = new HashMap<>(); | ||
for (final Map.Entry<String, Object> entry : sourceMap.entrySet()) { | ||
if (excludeKeySet.contains(entry.getKey())) { | ||
if (config.getRemoveProcessedFields()) { | ||
modifiedSourceMap.put(entry.getKey(), entry.getValue()); | ||
} | ||
continue; | ||
} | ||
targetList.add(Map.of( | ||
config.getKeyName(), entry.getKey(), | ||
config.getValueName(), entry.getValue() | ||
)); | ||
} | ||
|
||
if (config.getRemoveProcessedFields()) { | ||
recordEvent.put(config.getSource(), modifiedSourceMap); | ||
} | ||
|
||
recordEvent.put(config.getTarget(), targetList); | ||
} catch (Exception e) { | ||
LOG.error("Fail to perform Map to List operation", e); | ||
//TODO: add tagging on failure | ||
} | ||
} | ||
return records; | ||
} | ||
|
||
@Override | ||
public void prepareForShutdown() { | ||
} | ||
|
||
@Override | ||
public boolean isReadyForShutdown() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...va/org/opensearch/dataprepper/plugins/processor/mutateevent/MapToListProcessorConfig.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 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.processor.mutateevent; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import jakarta.validation.constraints.NotEmpty; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class MapToListProcessorConfig { | ||
private static final String DEFAULT_KEY_NAME = "key"; | ||
private static final String DEFAULT_VALUE_NAME = "value"; | ||
private static final List<String> DEFAULT_EXCLUDE_KEYS = new ArrayList<>(); | ||
private static final boolean DEFAULT_REMOVE_PROCESSED_FIELDS = false; | ||
|
||
@NotEmpty | ||
@NotNull | ||
@JsonProperty("source") | ||
private String source; | ||
|
||
@NotEmpty | ||
@NotNull | ||
@JsonProperty("target") | ||
private String target; | ||
|
||
@JsonProperty("key_name") | ||
private String keyName = DEFAULT_KEY_NAME; | ||
|
||
@JsonProperty("value_name") | ||
private String valueName = DEFAULT_VALUE_NAME; | ||
|
||
@JsonProperty("map_to_list_when") | ||
private String mapToListWhen; | ||
|
||
@JsonProperty("exclude_keys") | ||
private List<String> excludeKeys = DEFAULT_EXCLUDE_KEYS; | ||
|
||
@JsonProperty("remove_processed_fields") | ||
private boolean removeProcessedFields = DEFAULT_REMOVE_PROCESSED_FIELDS; | ||
|
||
public String getSource() { | ||
return source; | ||
} | ||
|
||
public String getTarget() { | ||
return target; | ||
} | ||
|
||
public String getKeyName() { | ||
return keyName; | ||
} | ||
|
||
public String getValueName() { | ||
return valueName; | ||
} | ||
|
||
public String getMapToListWhen() { | ||
return mapToListWhen; | ||
} | ||
|
||
public List<String> getExcludeKeys() { | ||
return excludeKeys; | ||
} | ||
|
||
public boolean getRemoveProcessedFields() { | ||
return removeProcessedFields; | ||
} | ||
} |
Oops, something went wrong.