-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split HTTP source data to multiple chunks before writing to byte buff…
…er (#4266) * Split HTTP source data to multiple chunks before writing to byte buffer Signed-off-by: Krishna Kondaka <[email protected]> * Modified JsonDecoder to parse objects in addition to array of objects Signed-off-by: Krishna Kondaka <[email protected]> * Added a test case Signed-off-by: Krishna Kondaka <[email protected]> * Removed json to array conversion Signed-off-by: Krishna Kondaka <[email protected]> * Modified to add new JsonObjDecoder for decoding single json objects Signed-off-by: Krishna Kondaka <[email protected]> * Removed changes to JsonDecoder Signed-off-by: Krishna Kondaka <[email protected]> * Renamed JsonObjDecoder to JsonObjectDecoder Signed-off-by: Krishna Kondaka <[email protected]> --------- Signed-off-by: Krishna Kondaka <[email protected]> Co-authored-by: Krishna Kondaka <[email protected]>
- Loading branch information
Showing
6 changed files
with
207 additions
and
6 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
data-prepper-api/src/main/java/org/opensearch/dataprepper/model/codec/JsonObjectDecoder.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,57 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.model.codec; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.event.JacksonEvent; | ||
import org.opensearch.dataprepper.model.log.JacksonLog; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.time.Instant; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
public class JsonObjectDecoder implements ByteDecoder { | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
private final JsonFactory jsonFactory = new JsonFactory(); | ||
|
||
public void parse(InputStream inputStream, Instant timeReceived, Consumer<Record<Event>> eventConsumer) throws IOException { | ||
Objects.requireNonNull(inputStream); | ||
Objects.requireNonNull(eventConsumer); | ||
|
||
final JsonParser jsonParser = jsonFactory.createParser(inputStream); | ||
|
||
while (!jsonParser.isClosed() && jsonParser.nextToken() != JsonToken.END_OBJECT) { | ||
if (jsonParser.getCurrentToken() == JsonToken.START_OBJECT) { | ||
final Map<String, Object> innerJson = objectMapper.readValue(jsonParser, Map.class); | ||
|
||
final Record<Event> record = createRecord(innerJson, timeReceived); | ||
eventConsumer.accept(record); | ||
} | ||
} | ||
} | ||
|
||
private Record<Event> createRecord(final Map<String, Object> json, final Instant timeReceived) { | ||
final JacksonLog.Builder logBuilder = JacksonLog.builder() | ||
.withData(json) | ||
.getThis(); | ||
if (timeReceived != null) { | ||
logBuilder.withTimeReceived(timeReceived); | ||
} | ||
final JacksonEvent event = (JacksonEvent)logBuilder.build(); | ||
|
||
return new Record<>(event); | ||
} | ||
|
||
} | ||
|
75 changes: 75 additions & 0 deletions
75
...epper-api/src/test/java/org/opensearch/dataprepper/model/codec/JsonObjectDecoderTest.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,75 @@ | ||
package org.opensearch.dataprepper.model.codec; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.event.DefaultEventHandle; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.time.Instant; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
public class JsonObjectDecoderTest { | ||
private JsonObjectDecoder jsonObjectDecoder; | ||
private Record<Event> receivedRecord; | ||
private Instant receivedTime; | ||
|
||
private JsonObjectDecoder createObjectUnderTest() { | ||
return new JsonObjectDecoder(); | ||
} | ||
|
||
@BeforeEach | ||
void setup() { | ||
jsonObjectDecoder = createObjectUnderTest(); | ||
receivedRecord = null; | ||
} | ||
|
||
@Test | ||
void test_basicJsonObjectDecoder() { | ||
String stringValue = UUID.randomUUID().toString(); | ||
Random r = new Random(); | ||
int intValue = r.nextInt(); | ||
String inputString = "{\"key1\":\""+stringValue+"\", \"key2\":"+intValue+"}"; | ||
try { | ||
jsonObjectDecoder.parse(new ByteArrayInputStream(inputString.getBytes()), null, (record) -> { | ||
receivedRecord = record; | ||
}); | ||
} catch (Exception e){} | ||
|
||
assertNotEquals(receivedRecord, null); | ||
Map<String, Object> map = receivedRecord.getData().toMap(); | ||
assertThat(map.get("key1"), equalTo(stringValue)); | ||
assertThat(map.get("key2"), equalTo(intValue)); | ||
} | ||
|
||
@Test | ||
void test_basicJsonObjectDecoder_withTimeReceived() { | ||
String stringValue = UUID.randomUUID().toString(); | ||
Random r = new Random(); | ||
int intValue = r.nextInt(); | ||
|
||
String inputString = "{\"key1\":\""+stringValue+"\", \"key2\":"+intValue+"}"; | ||
final Instant now = Instant.now(); | ||
try { | ||
jsonObjectDecoder.parse(new ByteArrayInputStream(inputString.getBytes()), now, (record) -> { | ||
receivedRecord = record; | ||
receivedTime = ((DefaultEventHandle)(((Event)record.getData()).getEventHandle())).getInternalOriginationTime(); | ||
}); | ||
} catch (Exception e){} | ||
|
||
assertNotEquals(receivedRecord, null); | ||
Map<String, Object> map = receivedRecord.getData().toMap(); | ||
assertThat(map.get("key1"), equalTo(stringValue)); | ||
assertThat(map.get("key2"), equalTo(intValue)); | ||
assertThat(receivedTime, equalTo(now)); | ||
} | ||
|
||
} |
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
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
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
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