Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds an auto_schema flag to Avro/Parquet codecs #3206

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package org.opensearch.dataprepper.plugins.codec.avro;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.AssertTrue;

/**
* Configuration class for {@link AvroOutputCodec}.
Expand All @@ -13,11 +14,27 @@ public class AvroOutputCodecConfig {
@JsonProperty("schema")
private String schema;

@JsonProperty("auto_schema")
private boolean autoSchema;

@AssertTrue(message = "The Avro codec requires either defining a schema or setting auto_schema to true to automatically generate a schema.")
boolean isSchemaOrAutoSchemaDefined() {
return schema != null ^ autoSchema;
}

public String getSchema() {
return schema;
}

public void setSchema(String schema) {
void setSchema(final String schema) {
this.schema = schema;
}

public boolean isAutoSchema() {
return autoSchema;
}

void setAutoSchema(final boolean autoSchema) {
this.autoSchema = autoSchema;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.opensearch.dataprepper.plugins.codec.avro;

import org.junit.jupiter.api.Test;

import java.util.UUID;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

class AvroOutputCodecConfigTest {

private AvroOutputCodecConfig createObjectUnderTest() {
return new AvroOutputCodecConfig();
}

@Test
void isSchemaOrAutoSchemaDefined_returns_true_if_schema_defined() {
AvroOutputCodecConfig objectUnderTest = createObjectUnderTest();
objectUnderTest.setSchema(UUID.randomUUID().toString());

assertThat(objectUnderTest.isSchemaOrAutoSchemaDefined(), equalTo(true));
}

@Test
void isSchemaOrAutoSchemaDefined_returns_true_if_schema_null_and_autoSchema_true() {
AvroOutputCodecConfig objectUnderTest = createObjectUnderTest();
objectUnderTest.setAutoSchema(true);

assertThat(objectUnderTest.isSchemaOrAutoSchemaDefined(), equalTo(true));
}

@Test
void isSchemaOrAutoSchemaDefined_returns_false_if_schema_null() {
AvroOutputCodecConfig objectUnderTest = createObjectUnderTest();

assertThat(objectUnderTest.isSchemaOrAutoSchemaDefined(), equalTo(false));
}

@Test
void isSchemaOrAutoSchemaDefined_returns_false_if_schema_and_autoSchema() {
AvroOutputCodecConfig objectUnderTest = createObjectUnderTest();
objectUnderTest.setSchema(UUID.randomUUID().toString());
objectUnderTest.setAutoSchema(true);

assertThat(objectUnderTest.isSchemaOrAutoSchemaDefined(), equalTo(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,34 @@
package org.opensearch.dataprepper.plugins.codec.parquet;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.AssertTrue;

public class ParquetOutputCodecConfig {
@JsonProperty("schema")
private String schema;

@JsonProperty("auto_schema")
private boolean autoSchema;

@AssertTrue(message = "The Parquet codec requires either defining a schema or setting auto_schema to true to automatically generate a schema.")
boolean isSchemaOrAutoSchemaDefined() {
return schema != null ^ autoSchema;
}

public String getSchema() {
return schema;
}

public void setSchema(String schema) {
public void setSchema(final String schema) {
this.schema = schema;
}

public boolean isAutoSchema() {
return autoSchema;
}

void setAutoSchema(final boolean autoSchema) {
this.autoSchema = autoSchema;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.opensearch.dataprepper.plugins.codec.parquet;

import org.junit.jupiter.api.Test;

import java.util.UUID;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

class ParquetOutputCodecConfigTest {

private ParquetOutputCodecConfig createObjectUnderTest() {
return new ParquetOutputCodecConfig();
}

@Test
void isSchemaOrAutoSchemaDefined_returns_true_if_schema_defined() {
ParquetOutputCodecConfig objectUnderTest = createObjectUnderTest();
objectUnderTest.setSchema(UUID.randomUUID().toString());

assertThat(objectUnderTest.isSchemaOrAutoSchemaDefined(), equalTo(true));
}

@Test
void isSchemaOrAutoSchemaDefined_returns_true_if_schema_null_and_autoSchema_true() {
ParquetOutputCodecConfig objectUnderTest = createObjectUnderTest();
objectUnderTest.setAutoSchema(true);

assertThat(objectUnderTest.isSchemaOrAutoSchemaDefined(), equalTo(true));
}

@Test
void isSchemaOrAutoSchemaDefined_returns_false_if_schema_null() {
ParquetOutputCodecConfig objectUnderTest = createObjectUnderTest();

assertThat(objectUnderTest.isSchemaOrAutoSchemaDefined(), equalTo(false));
}

@Test
void isSchemaOrAutoSchemaDefined_returns_false_if_schema_and_autoSchema() {
ParquetOutputCodecConfig objectUnderTest = createObjectUnderTest();
objectUnderTest.setSchema(UUID.randomUUID().toString());
objectUnderTest.setAutoSchema(true);

assertThat(objectUnderTest.isSchemaOrAutoSchemaDefined(), equalTo(false));
}

}
Loading