-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
S3 Event Consistency via metadata and JSON changes (#1803)
Support a configurable key for S3 metadata and make this base key s3/ by default. Moved the output of JSON from S3 objects into the root of the Event from the message key. Resolves #1687 Signed-off-by: David Venable <[email protected]>
- Loading branch information
Showing
11 changed files
with
160 additions
and
24 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
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
39 changes: 39 additions & 0 deletions
39
.../s3-source/src/main/java/com/amazon/dataprepper/plugins/source/EventMetadataModifier.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,39 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.source; | ||
|
||
import com.amazon.dataprepper.model.event.Event; | ||
|
||
import java.util.Objects; | ||
import java.util.function.BiConsumer; | ||
|
||
class EventMetadataModifier implements BiConsumer<Event, S3ObjectReference> { | ||
private static final String BUCKET_FIELD_NAME = "bucket"; | ||
private static final String KEY_FIELD_NAME = "key"; | ||
private final String baseKey; | ||
|
||
EventMetadataModifier(final String metadataRootKey) { | ||
baseKey = generateBaseKey(metadataRootKey); | ||
} | ||
|
||
@Override | ||
public void accept(final Event event, final S3ObjectReference s3ObjectReference) { | ||
event.put(baseKey + BUCKET_FIELD_NAME, s3ObjectReference.getBucketName()); | ||
event.put(baseKey + KEY_FIELD_NAME, s3ObjectReference.getKey()); | ||
} | ||
|
||
private static String generateBaseKey(String metadataRootKey) { | ||
Objects.requireNonNull(metadataRootKey); | ||
|
||
if(metadataRootKey.startsWith("/")) | ||
metadataRootKey = metadataRootKey.substring(1); | ||
|
||
if(metadataRootKey.isEmpty() || metadataRootKey.endsWith("/")) | ||
return metadataRootKey; | ||
|
||
return metadataRootKey + "/"; | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
...source/src/test/java/com/amazon/dataprepper/plugins/source/EventMetadataModifierTest.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,82 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.amazon.dataprepper.plugins.source; | ||
|
||
import com.amazon.dataprepper.model.event.Event; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.ArgumentsProvider; | ||
import org.junit.jupiter.params.provider.ArgumentsSource; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.UUID; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class EventMetadataModifierTest { | ||
@Mock | ||
private Event event; | ||
|
||
@Mock | ||
private S3ObjectReference s3ObjectReference; | ||
private String bucketName; | ||
private String key; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
bucketName = UUID.randomUUID().toString(); | ||
key = UUID.randomUUID().toString(); | ||
} | ||
|
||
private EventMetadataModifier createObjectUnderTest(final String metadataRootKey) { | ||
return new EventMetadataModifier(metadataRootKey); | ||
} | ||
|
||
@Test | ||
void constructor_throws_if_metadataRootKey_is_null() { | ||
assertThrows(NullPointerException.class, () -> createObjectUnderTest(null)); | ||
} | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(KeysArgumentsProvider.class) | ||
void accept_sets_correct_S3_bucket_and_key(final String metadataKey, final String expectedRootKey) { | ||
when(s3ObjectReference.getBucketName()).thenReturn(bucketName); | ||
when(s3ObjectReference.getKey()).thenReturn(key); | ||
|
||
createObjectUnderTest(metadataKey).accept(event, s3ObjectReference); | ||
|
||
verify(event).put(expectedRootKey + "bucket", bucketName); | ||
verify(event).put(expectedRootKey + "key", key); | ||
} | ||
|
||
static class KeysArgumentsProvider implements ArgumentsProvider { | ||
@Override | ||
public Stream<? extends Arguments> provideArguments(final ExtensionContext context) { | ||
return Stream.of( | ||
arguments("", ""), | ||
arguments("/", ""), | ||
arguments("s3", "s3/"), | ||
arguments("s3/", "s3/"), | ||
arguments("/s3", "s3/"), | ||
arguments("/s3/", "s3/"), | ||
arguments("s3/inner", "s3/inner/"), | ||
arguments("s3/inner/", "s3/inner/"), | ||
arguments("/s3/inner", "s3/inner/"), | ||
arguments("/s3/inner/", "s3/inner/") | ||
); | ||
} | ||
} | ||
} |
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