|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.dataprepper.plugins.codec.json; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.JsonFactory; |
| 9 | +import com.fasterxml.jackson.core.JsonParser; |
| 10 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 11 | +import com.fasterxml.jackson.databind.MappingIterator; |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | +import org.opensearch.dataprepper.model.annotations.DataPrepperPlugin; |
| 14 | +import org.opensearch.dataprepper.model.annotations.DataPrepperPluginConstructor; |
| 15 | +import org.opensearch.dataprepper.model.codec.InputCodec; |
| 16 | +import org.opensearch.dataprepper.model.event.Event; |
| 17 | +import org.opensearch.dataprepper.model.event.EventFactory; |
| 18 | +import org.opensearch.dataprepper.model.event.LogEventBuilder; |
| 19 | +import org.opensearch.dataprepper.model.log.Log; |
| 20 | +import org.opensearch.dataprepper.model.record.Record; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Objects; |
| 26 | +import java.util.function.Consumer; |
| 27 | + |
| 28 | +/** |
| 29 | + * A Data Prepper {@link InputCodec} which reads ND-JSON and other similar |
| 30 | + * formats which have JSON objects together. |
| 31 | + */ |
| 32 | +@DataPrepperPlugin(name = "ndjson", pluginType = InputCodec.class, pluginConfigurationType = NdjsonInputConfig.class) |
| 33 | +public class NdjsonInputCodec implements InputCodec { |
| 34 | + private static final TypeReference<Map<String, Object>> MAP_TYPE_REFERENCE = new TypeReference<>() {}; |
| 35 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 36 | + private final NdjsonInputConfig ndjsonInputConfig; |
| 37 | + private final EventFactory eventFactory; |
| 38 | + private final JsonFactory jsonFactory; |
| 39 | + |
| 40 | + @DataPrepperPluginConstructor |
| 41 | + public NdjsonInputCodec(final NdjsonInputConfig ndjsonInputConfig, final EventFactory eventFactory) { |
| 42 | + this.ndjsonInputConfig = ndjsonInputConfig; |
| 43 | + this.eventFactory = eventFactory; |
| 44 | + jsonFactory = new JsonFactory(); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void parse(final InputStream inputStream, final Consumer<Record<Event>> eventConsumer) throws IOException { |
| 49 | + Objects.requireNonNull(inputStream, "Parameter inputStream must not be null."); |
| 50 | + Objects.requireNonNull(eventConsumer, "Parameter eventConsumer must not be null."); |
| 51 | + |
| 52 | + final JsonParser parser = jsonFactory.createParser(inputStream); |
| 53 | + |
| 54 | + final MappingIterator<Map<String, Object>> mapMappingIterator = objectMapper.readValues(parser, MAP_TYPE_REFERENCE); |
| 55 | + while (mapMappingIterator.hasNext()) { |
| 56 | + final Map<String, Object> json = mapMappingIterator.next(); |
| 57 | + |
| 58 | + if(!ndjsonInputConfig.isIncludeEmptyObjects() && json.isEmpty()) |
| 59 | + continue; |
| 60 | + |
| 61 | + final Record<Event> record = createRecord(json); |
| 62 | + eventConsumer.accept(record); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private Record<Event> createRecord(final Map<String, Object> json) { |
| 67 | + final Log event = eventFactory.eventBuilder(LogEventBuilder.class) |
| 68 | + .withData(json) |
| 69 | + .build(); |
| 70 | + |
| 71 | + return new Record<>(event); |
| 72 | + } |
| 73 | +} |
0 commit comments