forked from opensearch-project/data-prepper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support for storing raw bytes in Kafka Buffer (opensearch-proj…
…ect#3519) * Adding support for storing raw bytes in Kafka Buffer Signed-off-by: Krishna Kondaka <[email protected]> * Modified to wait for the send() to finish before returning Signed-off-by: Krishna Kondaka <[email protected]> * Addressed review comments Signed-off-by: Krishna Kondaka <[email protected]> * Removed unused imports Signed-off-by: Krishna Kondaka <[email protected]> * Fixed Kafka integration test Signed-off-by: Krishna Kondaka <[email protected]> * Fixed json processor check style errors Signed-off-by: Krishna Kondaka <[email protected]> * Addressed review comments and added a new test case Signed-off-by: Krishna Kondaka <[email protected]> * Addressed review comments and added a new tests 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
38 changed files
with
531 additions
and
106 deletions.
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
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
26 changes: 26 additions & 0 deletions
26
data-prepper-api/src/main/java/org/opensearch/dataprepper/model/codec/ByteDecoder.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,26 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.model.codec; | ||
|
||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Serializable; | ||
import java.util.function.Consumer; | ||
|
||
public interface ByteDecoder extends Serializable { | ||
/** | ||
* Parses an {@link InputStream}. Implementors should call the {@link Consumer} for each | ||
* {@link Record} loaded from the {@link InputStream}. | ||
* | ||
* @param inputStream The input stream for code to process | ||
* @param eventConsumer The consumer which handles each event from the stream | ||
* @throws IOException throws IOException when invalid input is received or incorrect codec name is provided | ||
*/ | ||
void parse(InputStream inputStream, Consumer<Record<Event>> eventConsumer) throws IOException; | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
data-prepper-api/src/main/java/org/opensearch/dataprepper/model/codec/HasByteDecoder.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,12 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.model.codec; | ||
|
||
public interface HasByteDecoder { | ||
default ByteDecoder getDecoder() { | ||
return null; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
data-prepper-api/src/main/java/org/opensearch/dataprepper/model/codec/JsonDecoder.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,58 @@ | ||
/* | ||
* 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.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Consumer; | ||
|
||
public class JsonDecoder implements ByteDecoder { | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
private final JsonFactory jsonFactory = new JsonFactory(); | ||
|
||
public void parse(InputStream inputStream, 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_ARRAY) { | ||
parseRecordsArray(jsonParser, eventConsumer); | ||
} | ||
} | ||
} | ||
|
||
private void parseRecordsArray(final JsonParser jsonParser, final Consumer<Record<Event>> eventConsumer) throws IOException { | ||
while (jsonParser.nextToken() != JsonToken.END_ARRAY) { | ||
final Map<String, Object> innerJson = objectMapper.readValue(jsonParser, Map.class); | ||
|
||
final Record<Event> record = createRecord(innerJson); | ||
eventConsumer.accept(record); | ||
} | ||
} | ||
|
||
private Record<Event> createRecord(final Map<String, Object> json) { | ||
final JacksonEvent event = (JacksonEvent)JacksonLog.builder() | ||
.withData(json) | ||
.getThis() | ||
.build(); | ||
|
||
return new Record<>(event); | ||
} | ||
|
||
} |
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
23 changes: 23 additions & 0 deletions
23
...-prepper-api/src/test/java/org/opensearch/dataprepper/model/codec/HasByteDecoderTest.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,23 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.model.codec; | ||
|
||
import org.junit.Assert; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.mockito.Mockito.spy; | ||
|
||
public class HasByteDecoderTest { | ||
|
||
@Test | ||
public void testGetDecoder() { | ||
final HasByteDecoder hasByteDecoder = spy(HasByteDecoder.class); | ||
|
||
Assert.assertEquals(null, hasByteDecoder.getDecoder()); | ||
} | ||
|
||
} | ||
|
50 changes: 50 additions & 0 deletions
50
data-prepper-api/src/test/java/org/opensearch/dataprepper/model/codec/JsonDecoderTest.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,50 @@ | ||
package org.opensearch.dataprepper.model.codec; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.opensearch.dataprepper.model.event.Event; | ||
import org.opensearch.dataprepper.model.record.Record; | ||
|
||
import java.io.ByteArrayInputStream; | ||
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.Assert.assertNotEquals; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
public class JsonDecoderTest { | ||
private JsonDecoder jsonDecoder; | ||
private Record<Event> receivedRecord; | ||
|
||
private JsonDecoder createObjectUnderTest() { | ||
return new JsonDecoder(); | ||
} | ||
|
||
@BeforeEach | ||
void setup() { | ||
jsonDecoder = createObjectUnderTest(); | ||
receivedRecord = null; | ||
} | ||
|
||
@Test | ||
void test_basicJsonDecoder() { | ||
String stringValue = UUID.randomUUID().toString(); | ||
Random r = new Random(); | ||
int intValue = r.nextInt(); | ||
String inputString = "[{\"key1\":\""+stringValue+"\", \"key2\":"+intValue+"}]"; | ||
try { | ||
jsonDecoder.parse(new ByteArrayInputStream(inputString.getBytes()), (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)); | ||
} | ||
|
||
} |
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
Oops, something went wrong.