TheNeuralBit commented on a change in pull request #12838:
URL: https://github.com/apache/beam/pull/12838#discussion_r516389575



##########
File path: 
sdks/java/extensions/protobuf/src/main/java/org/apache/beam/sdk/extensions/protobuf/ProtoMessageSchema.java
##########
@@ -115,6 +122,76 @@ public SchemaUserTypeCreator schemaTypeCreator(Class<?> 
targetClass, Schema sche
     return creator;
   }
 
+  public static <T extends Message> SimpleFunction<byte[], Row> 
getProtoBytesToRowFn(
+      Class<T> clazz) {
+    return new ProtoBytesToRowFn<>(clazz);
+  }
+
+  public static class ProtoBytesToRowFn<T extends Message> extends 
SimpleFunction<byte[], Row> {
+    private final ProtoCoder<T> protoCoder;
+    private final SerializableFunction<T, Row> toRowFunction;
+
+    public ProtoBytesToRowFn(Class<T> clazz) {
+      this.protoCoder = ProtoCoder.of(clazz);
+      this.toRowFunction = new 
ProtoMessageSchema().toRowFunction(TypeDescriptor.of(clazz));
+    }
+
+    @Override
+    public Row apply(byte[] bytes) {
+      try {
+        InputStream inputStream = new ByteArrayInputStream(bytes);
+        T message = protoCoder.decode(inputStream);
+        return toRowFunction.apply(message);
+      } catch (IOException e) {
+        throw new IllegalArgumentException("Could not decode row from proto 
payload.", e);
+      }
+    }
+  }
+
+  public static <T extends Message> SimpleFunction<Row, byte[]> 
getRowToProtoBytesFn(
+      Class<T> clazz) {
+    return new RowToProtoBytesFn<>(clazz);
+  }
+
+  public static class RowToProtoBytesFn<T extends Message> extends 
SimpleFunction<Row, byte[]> {

Review comment:
       Thanks! I think this approach is best for now. In the future I think we 
should try to isolate all the format specific logic in a module for just that 
format, and just have general purpose logic in SQL. This is closer to that 
vision imo.




----------------------------------------------------------------
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]


Reply via email to