44 */
55package org .opensearch .dataprepper .plugins .codec .json ;
66
7- import com .fasterxml .jackson .databind .ObjectMapper ;
87import org .opensearch .dataprepper .model .annotations .DataPrepperPlugin ;
98import org .opensearch .dataprepper .model .annotations .DataPrepperPluginConstructor ;
109import org .opensearch .dataprepper .model .codec .OutputCodec ;
2221@ DataPrepperPlugin (name = "ndjson" , pluginType = OutputCodec .class , pluginConfigurationType = NdjsonOutputConfig .class )
2322public class NdjsonOutputCodec implements OutputCodec {
2423 private static final String NDJSON = "ndjson" ;
25- private static final ObjectMapper objectMapper = new ObjectMapper ();
26- private final NdjsonOutputConfig config ;
27- private OutputCodecContext codecContext ;
24+ private OutputCodecContext deprecatedSupportCodecContext ;
2825
2926 @ DataPrepperPluginConstructor
3027 public NdjsonOutputCodec (final NdjsonOutputConfig config ) {
3128 Objects .requireNonNull (config );
32- this .config = config ;
29+ }
30+
31+ private static class NdjsonWriter implements Writer {
32+ private final OutputStream outputStream ;
33+ private final OutputCodecContext codecContext ;
34+
35+ private NdjsonWriter (final OutputStream outputStream , final OutputCodecContext codecContext ) {
36+ this .outputStream = outputStream ;
37+ this .codecContext = codecContext ;
38+ }
39+
40+ @ Override
41+ public void writeEvent (final Event event ) throws IOException {
42+ doWriteEvent (outputStream , event , codecContext );
43+ }
44+
45+ @ Override
46+ public void complete () throws IOException {
47+ outputStream .close ();
48+ }
49+ }
50+
51+ @ Override
52+ public Writer createWriter (final OutputStream outputStream , final Event sampleEvent , final OutputCodecContext codecContext ) {
53+ Objects .requireNonNull (outputStream );
54+ Objects .requireNonNull (codecContext );
55+
56+ return new NdjsonWriter (outputStream , codecContext );
3357 }
3458
3559 @ Override
3660 public void start (final OutputStream outputStream , Event event , final OutputCodecContext codecContext ) throws IOException {
3761 Objects .requireNonNull (outputStream );
3862 Objects .requireNonNull (codecContext );
39- this .codecContext = codecContext ;
63+ this .deprecatedSupportCodecContext = codecContext ;
4064 }
4165
4266 @ Override
4367 public void writeEvent (final Event event , final OutputStream outputStream ) throws IOException {
4468 Objects .requireNonNull (event );
4569
4670 String json = event .jsonBuilder ()
47- .includeKeys (codecContext .getIncludeKeys ())
48- .excludeKeys (codecContext .getExcludeKeys ())
49- .includeTags (codecContext .getTagsTargetKey ())
71+ .includeKeys (deprecatedSupportCodecContext .getIncludeKeys ())
72+ .excludeKeys (deprecatedSupportCodecContext .getExcludeKeys ())
73+ .includeTags (deprecatedSupportCodecContext .getTagsTargetKey ())
5074 .toJsonString ();
5175 outputStream .write (json .getBytes ());
5276 outputStream .write (System .lineSeparator ().getBytes ());
@@ -61,4 +85,16 @@ public void complete(final OutputStream outputStream) throws IOException {
6185 public String getExtension () {
6286 return NDJSON ;
6387 }
88+
89+ private static void doWriteEvent (final OutputStream outputStream , final Event event , final OutputCodecContext codecContext ) throws IOException {
90+ Objects .requireNonNull (event );
91+
92+ String json = event .jsonBuilder ()
93+ .includeKeys (codecContext .getIncludeKeys ())
94+ .excludeKeys (codecContext .getExcludeKeys ())
95+ .includeTags (codecContext .getTagsTargetKey ())
96+ .toJsonString ();
97+ outputStream .write (json .getBytes ());
98+ outputStream .write (System .lineSeparator ().getBytes ());
99+ }
64100}
0 commit comments