piotr-szuberski commented on a change in pull request #12780:
URL: https://github.com/apache/beam/pull/12780#discussion_r514070547
##########
File path:
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/RowToPubsubMessage.java
##########
@@ -57,14 +73,43 @@ public static RowToPubsubMessage
withTimestampAttribute(boolean useTimestampAttr
? input.apply(WithTimestamps.of((row) ->
row.getDateTime(TIMESTAMP_FIELD).toInstant()))
: input;
- return withTimestamp
- .apply(DropFields.fields(TIMESTAMP_FIELD))
- .apply(ToJson.of())
- .apply(
- MapElements.into(TypeDescriptor.of(PubsubMessage.class))
- .via(
- (String json) ->
- new PubsubMessage(
- json.getBytes(StandardCharsets.ISO_8859_1),
ImmutableMap.of())));
+ withTimestamp = withTimestamp.apply(DropFields.fields(TIMESTAMP_FIELD));
+ switch (payloadFormat) {
+ case JSON:
+ return withTimestamp
+ .apply("MapRowToJsonString", ToJson.of())
+ .apply("MapToJsonBytes", MapElements.via(new StringToBytes()))
+ .apply("MapToPubsubMessage", MapElements.via(new
ToPubsubMessage()));
+ case AVRO:
+ return withTimestamp
+ .apply(
+ "MapRowToAvroBytes",
+
MapElements.via(AvroUtils.getRowToAvroBytesFunction(payloadSchema)))
+ .apply("MapToPubsubMessage", MapElements.via(new
ToPubsubMessage()));
+ default:
+ throw new IllegalArgumentException("Unsupported payload format: " +
payloadFormat);
+ }
+ }
+
+ private static class StringToBytes extends SimpleFunction<String, byte[]> {
+ @Override
+ public byte[] apply(String s) {
+ return s.getBytes(ISO_8859_1);
Review comment:
Sure! Done.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]