-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add map_to_list processor #3945
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d766a73
Add map to list processor basic functionality and unit tests
oeyh 8876ee7
Add exclude_keys and remove_processed_fields options
oeyh eceab20
Add unit test for when condition
oeyh 11ddfd4
Address review comments
oeyh ee8e020
Address more comments
oeyh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
101 changes: 101 additions & 0 deletions
101
...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,101 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.processor.mutateevent; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
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 ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
private static final TypeReference<Map<String, Object>> MAP_TYPE_REFERENCE = new TypeReference<Map<String, Object>>() {}; | ||
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<>(); | ||
|
||
for (final Map.Entry<String, Object> entry : sourceMap.entrySet()) { | ||
if (excludeKeySet.contains(entry.getKey())) { | ||
continue; | ||
} | ||
targetList.add(Map.of( | ||
config.getKeyName(), entry.getKey(), | ||
config.getValueName(), entry.getValue() | ||
)); | ||
} | ||
|
||
if (config.getRemoveProcessedFields()) { | ||
Map<String, Object> modifiedSourceMap = new HashMap<>(); | ||
for (final Map.Entry<String, Object> entry : sourceMap.entrySet()) { | ||
if (excludeKeySet.contains(entry.getKey())) { | ||
modifiedSourceMap.put(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code can be merged with the code in lines 62-70 like below, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merged the two for loops.