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

Add STRING format to ReadFromKafka schema transform. #34302

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -43,7 +43,7 @@ public abstract class KafkaReadSchemaTransformConfiguration {

public static final Set<String> VALID_START_OFFSET_VALUES = Sets.newHashSet("earliest", "latest");

public static final String VALID_FORMATS_STR = "RAW,AVRO,JSON,PROTO";
public static final String VALID_FORMATS_STR = "RAW,STRING,AVRO,JSON,PROTO";
public static final Set<String> VALID_DATA_FORMATS =
Sets.newHashSet(VALID_FORMATS_STR.split(","));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
Expand Down Expand Up @@ -103,6 +104,17 @@ public Row apply(byte[] input) {
};
}

public static SerializableFunction<byte[], Row> getRawStringToRowFunction(Schema stringSchema) {
return new SimpleFunction<byte[], Row>() {
@Override
public Row apply(byte[] input) {
return Row.withSchema(stringSchema)
.addValue(new String(input, StandardCharsets.UTF_8))
.build();
}
};
}

@Override
public String identifier() {
return getUrn(ExternalTransforms.ManagedTransforms.Urns.KAFKA_READ);
Expand Down Expand Up @@ -193,6 +205,9 @@ public PCollectionRowTuple expand(PCollectionRowTuple input) {
if ("RAW".equals(format)) {
beamSchema = Schema.builder().addField("payload", Schema.FieldType.BYTES).build();
valueMapper = getRawBytesToRowFunction(beamSchema);
} else if ("STRING".equals(format)) {
beamSchema = Schema.builder().addField("payload", Schema.FieldType.STRING).build();
valueMapper = getRawStringToRowFunction(beamSchema);
} else if ("PROTO".equals(format)) {
String fileDescriptorPath = configuration.getFileDescriptorPath();
String messageName = checkArgumentNotNull(configuration.getMessageName());
Expand Down
Loading