|
19 | 19 | import org.opensearch.dataprepper.model.processor.AbstractProcessor; |
20 | 20 | import org.opensearch.dataprepper.model.processor.Processor; |
21 | 21 | import org.opensearch.dataprepper.model.record.Record; |
| 22 | +import org.slf4j.Logger; |
| 23 | +import org.slf4j.LoggerFactory; |
22 | 24 |
|
23 | 25 | import java.util.ArrayList; |
24 | 26 | import java.util.Collection; |
| 27 | +import java.util.List; |
25 | 28 | import java.util.function.Function; |
26 | 29 | import java.util.regex.Pattern; |
27 | 30 |
|
28 | | - |
29 | 31 | @DataPrepperPlugin(name = "split_event", pluginType = Processor.class, pluginConfigurationType = SplitEventProcessorConfig.class) |
30 | | -public class SplitEventProcessor extends AbstractProcessor<Record<Event>, Record<Event>>{ |
31 | | - final String delimiter; |
32 | | - final String delimiterRegex; |
33 | | - final String field; |
34 | | - final Pattern pattern; |
| 32 | +public class SplitEventProcessor extends AbstractProcessor<Record<Event>, Record<Event>> { |
| 33 | + private static final Logger LOG = LoggerFactory.getLogger(SplitEventProcessor.class); |
| 34 | + |
| 35 | + private final String field; |
35 | 36 | private final Function<String, String[]> splitter; |
36 | 37 |
|
37 | 38 | @DataPrepperPluginConstructor |
38 | 39 | public SplitEventProcessor(final PluginMetrics pluginMetrics, final SplitEventProcessorConfig config) { |
39 | 40 | super(pluginMetrics); |
40 | | - this.delimiter = config.getDelimiter(); |
41 | | - this.delimiterRegex = config.getDelimiterRegex(); |
42 | 41 | this.field = config.getField(); |
43 | 42 |
|
44 | | - if(delimiterRegex != null && !delimiterRegex.isEmpty() |
| 43 | + final String delimiter = config.getDelimiter(); |
| 44 | + final String delimiterRegex = config.getDelimiterRegex(); |
| 45 | + |
| 46 | + if (delimiterRegex != null && !delimiterRegex.isEmpty() |
45 | 47 | && delimiter != null && !delimiter.isEmpty()) { |
46 | 48 | throw new IllegalArgumentException("delimiter and delimiter_regex cannot be defined at the same time"); |
47 | | - } else if((delimiterRegex == null || delimiterRegex.isEmpty()) && |
48 | | - (delimiter == null || delimiter.isEmpty())) { |
49 | | - throw new IllegalArgumentException("delimiter or delimiter_regex needs to be defined"); |
50 | 49 | } |
51 | 50 |
|
52 | | - if(delimiterRegex != null && !delimiterRegex.isEmpty()) { |
53 | | - pattern = Pattern.compile(delimiterRegex); |
| 51 | + final boolean hasRegex = (delimiterRegex != null && !delimiterRegex.isEmpty()); |
| 52 | + |
| 53 | + if (hasRegex) { |
| 54 | + final Pattern pattern = Pattern.compile(delimiterRegex); |
54 | 55 | splitter = pattern::split; |
| 56 | + } else if (delimiter != null && !delimiter.isEmpty()) { |
| 57 | + final Pattern literalPattern = Pattern.compile(Pattern.quote(delimiter)); |
| 58 | + splitter = literalPattern::split; |
55 | 59 | } else { |
56 | | - splitter = inputString -> inputString.split(delimiter); |
57 | | - pattern = null; |
| 60 | + splitter = null; |
58 | 61 | } |
59 | 62 | } |
60 | 63 |
|
61 | 64 | @Override |
62 | 65 | public Collection<Record<Event>> doExecute(final Collection<Record<Event>> records) { |
63 | | - Collection<Record<Event>> newRecords = new ArrayList<>(); |
64 | | - for(final Record<Event> record : records) { |
| 66 | + final Collection<Record<Event>> newRecords = new ArrayList<>(); |
| 67 | + for (final Record<Event> record : records) { |
65 | 68 | final Event recordEvent = record.getData(); |
66 | 69 |
|
67 | 70 | if (!recordEvent.containsKey(field)) { |
68 | 71 | newRecords.add(record); |
69 | 72 | continue; |
70 | 73 | } |
71 | | - |
| 74 | + |
72 | 75 | final Object value = recordEvent.get(field, Object.class); |
73 | 76 |
|
74 | | - //split record according to delimiter |
75 | | - final String[] splitValues = splitter.apply((String) value); |
| 77 | + if (value == null) { |
| 78 | + newRecords.add(record); |
| 79 | + continue; |
| 80 | + } |
| 81 | + |
| 82 | + if (value instanceof List<?>) { |
| 83 | + splitArrayField(record, recordEvent, (List<?>) value, newRecords); |
| 84 | + continue; |
| 85 | + } |
76 | 86 |
|
77 | | - // when no splits or empty value use the original record |
78 | | - if(splitValues.length <= 1) { |
| 87 | + if (splitter == null) { |
| 88 | + LOG.debug("Field '{}' is not an array and no delimiter is configured, passing through unchanged", field); |
79 | 89 | newRecords.add(record); |
80 | 90 | continue; |
81 | | - } |
| 91 | + } |
82 | 92 |
|
83 | | - //create new events for the splits |
84 | | - for (int i = 0; i < splitValues.length-1 ; i++) { |
85 | | - Record newRecord = createNewRecordFromEvent(recordEvent, splitValues[i]); |
86 | | - addToAcknowledgementSetFromOriginEvent((Event) newRecord.getData(), recordEvent); |
87 | | - newRecords.add(newRecord); |
| 93 | + if (!(value instanceof String)) { |
| 94 | + LOG.debug("Field '{}' has non-string, non-array value of type {}, passing through unchanged", field, value.getClass().getSimpleName()); |
| 95 | + newRecords.add(record); |
| 96 | + continue; |
88 | 97 | } |
89 | 98 |
|
90 | | - // Modify original event to hold the last split |
91 | | - recordEvent.put(field, splitValues[splitValues.length-1]); |
92 | | - newRecords.add(record); |
| 99 | + final String[] splitValues = splitter.apply((String) value); |
| 100 | + |
| 101 | + if (splitValues.length <= 1) { |
| 102 | + newRecords.add(record); |
| 103 | + continue; |
| 104 | + } |
| 105 | + |
| 106 | + splitIntoRecords(record, recordEvent, splitValues, newRecords); |
93 | 107 | } |
94 | 108 | return newRecords; |
95 | 109 | } |
96 | 110 |
|
97 | | - protected Record createNewRecordFromEvent(final Event recordEvent, String splitValue) { |
98 | | - Record newRecord; |
99 | | - JacksonEvent newRecordEvent; |
| 111 | + private void splitArrayField(final Record<Event> record, final Event recordEvent, |
| 112 | + final List<?> arrayValue, final Collection<Record<Event>> newRecords) { |
| 113 | + if (arrayValue.size() <= 1) { |
| 114 | + if (arrayValue.size() == 1) { |
| 115 | + recordEvent.put(field, arrayValue.get(0)); |
| 116 | + } |
| 117 | + newRecords.add(record); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + splitIntoRecords(record, recordEvent, arrayValue.toArray(), newRecords); |
| 122 | + } |
| 123 | + |
| 124 | + private void splitIntoRecords(final Record<Event> record, final Event recordEvent, |
| 125 | + final Object[] values, final Collection<Record<Event>> newRecords) { |
| 126 | + for (int i = 0; i < values.length - 1; i++) { |
| 127 | + final Record<Event> newRecord = createNewRecordFromEvent(recordEvent, values[i]); |
| 128 | + addToAcknowledgementSetFromOriginEvent(newRecord.getData(), recordEvent); |
| 129 | + newRecords.add(newRecord); |
| 130 | + } |
| 131 | + |
| 132 | + recordEvent.put(field, values[values.length - 1]); |
| 133 | + newRecords.add(record); |
| 134 | + } |
100 | 135 |
|
101 | | - newRecordEvent = JacksonEvent.fromEvent(recordEvent); |
102 | | - newRecordEvent.put(field,(Object) splitValue); |
103 | | - newRecord = new Record<>(newRecordEvent); |
104 | | - return newRecord; |
| 136 | + private Record<Event> createNewRecordFromEvent(final Event recordEvent, final Object splitValue) { |
| 137 | + final JacksonEvent newRecordEvent = JacksonEvent.fromEvent(recordEvent); |
| 138 | + newRecordEvent.put(field, splitValue); |
| 139 | + return new Record<>(newRecordEvent); |
105 | 140 | } |
106 | 141 |
|
107 | | - protected void addToAcknowledgementSetFromOriginEvent(Event recordEvent, Event originRecordEvent) { |
108 | | - DefaultEventHandle eventHandle = (DefaultEventHandle) originRecordEvent.getEventHandle(); |
| 142 | + private void addToAcknowledgementSetFromOriginEvent(final Event recordEvent, final Event originRecordEvent) { |
| 143 | + final DefaultEventHandle eventHandle = (DefaultEventHandle) originRecordEvent.getEventHandle(); |
109 | 144 | if (eventHandle != null) { |
110 | 145 | eventHandle.addEventHandle(recordEvent.getEventHandle()); |
111 | 146 | } |
|
0 commit comments