[ 
https://issues.apache.org/jira/browse/BEAM-4453?focusedWorklogId=120039&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-120039
 ]

ASF GitHub Bot logged work on BEAM-4453:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 06/Jul/18 21:19
            Start Date: 06/Jul/18 21:19
    Worklog Time Spent: 10m 
      Work Description: apilloud commented on a change in pull request #5873: 
[BEAM-4453] Add schema support for Java POJOs and Java Beans
URL: https://github.com/apache/beam/pull/5873#discussion_r199974567
 
 

 ##########
 File path: 
sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/GetterBasedSchemaProvider.java
 ##########
 @@ -0,0 +1,147 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.beam.sdk.schemas;
+
+import static com.google.common.base.Preconditions.checkState;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Type;
+import java.util.List;
+import java.util.Map;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.annotations.Experimental.Kind;
+import org.apache.beam.sdk.schemas.Schema.FieldType;
+import org.apache.beam.sdk.schemas.Schema.TypeName;
+import org.apache.beam.sdk.transforms.SerializableFunction;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.RowWithGetters;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.reflect.FieldValueGetterFactory;
+import org.apache.beam.sdk.values.reflect.FieldValueSetter;
+import org.apache.beam.sdk.values.reflect.FieldValueSetterFactory;
+
+/**
+ * A {@link SchemaProvider} base class that vends schemas and rows based on 
{@link
+ * org.apache.beam.sdk.values.reflect.FieldValueGetter}s.
+ */
+@Experimental(Kind.SCHEMAS)
+public abstract class GetterBasedSchemaProvider implements SchemaProvider {
+  /** Implementing class should override to return a getter factory. */
+  abstract FieldValueGetterFactory fieldValueGetterFactory();
+
+  /** Implementing class should override to return a setter factory. */
+  abstract FieldValueSetterFactory fieldValueSetterFactory();
+
+  @Override
+  public <T> SerializableFunction<T, Row> toRowFunction(TypeDescriptor<T> 
typeDescriptor) {
+    return o ->
+        Row.withSchema(schemaFor(typeDescriptor))
+            .withFieldValueGetters(fieldValueGetterFactory(), o)
+            .build();
+  }
+
+  @Override
+  @SuppressWarnings("unchecked")
+  public <T> SerializableFunction<Row, T> fromRowFunction(TypeDescriptor<T> 
typeDescriptor) {
+    return r -> {
+      if (r instanceof RowWithGetters) {
+        // Efficient path: simply extract the underlying POJO instead of 
creating a new one.
+        return (T) ((RowWithGetters) r).getGetterTarget();
+      } else {
+        // Use the setters to copy values from the Row to a new instance of 
the class.
+        return fromRow(r, (Class<T>) typeDescriptor.getType());
+      }
+    };
+  }
+
+  private <T> T fromRow(Row row, Class<T> clazz) {
+    T object;
+    try {
+      object = clazz.getDeclaredConstructor().newInstance();
+    } catch (NoSuchMethodException
+        | IllegalAccessException
+        | InvocationTargetException
+        | InstantiationException e) {
+      throw new RuntimeException("Failed to instantiate object ", e);
+    }
+
+    List<FieldValueSetter> setters =
+        fieldValueSetterFactory().createSetters(clazz, row.getSchema());
+    checkState(
+        setters.size() == row.getFieldCount(),
+        "Did not have a matching number of setters and fields.");
+
+    // Iterate over the row, and set (possibly recursively) each field in the 
underlying object
+    // using the setter.
+    Schema schema = row.getSchema();
 
 Review comment:
   You are also calling `row.getSchema()` a few lines up. If getSchema is 
expensive it makes sense to move this before `createSetters()` and use it there.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 120039)
    Time Spent: 0.5h  (was: 20m)

> Provide automatic schema registration for POJOs
> -----------------------------------------------
>
>                 Key: BEAM-4453
>                 URL: https://issues.apache.org/jira/browse/BEAM-4453
>             Project: Beam
>          Issue Type: Sub-task
>          Components: sdk-java-core
>            Reporter: Reuven Lax
>            Assignee: Reuven Lax
>            Priority: Major
>          Time Spent: 0.5h
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to