nbali commented on code in PR #24168:
URL: https://github.com/apache/beam/pull/24168#discussion_r1071643551


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/firestore/RowToDocument.java:
##########
@@ -0,0 +1,176 @@
+/*
+ * 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.io.gcp.firestore;
+
+import static 
org.apache.beam.sdk.io.gcp.firestore.FirestoreHelper.makeStringValue;
+import static org.apache.beam.sdk.io.gcp.firestore.FirestoreHelper.makeValue;
+
+import com.google.firestore.v1.Document;
+import com.google.firestore.v1.MapValue;
+import com.google.firestore.v1.Value;
+import com.google.protobuf.ByteString;
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.Row;
+import org.joda.time.Instant;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** A {@code PTransform} to perform a conversion of {@link Row} to {@link 
Document}. */
+@SuppressWarnings({
+  "nullness" // TODO(https://github.com/apache/beam/issues/20497)
+})
+public class RowToDocument extends PTransform<PCollection<Row>, 
PCollection<Document>> {
+  private final String keyField;
+  private static final Logger LOG = 
LoggerFactory.getLogger(RowToDocument.class);
+
+  private RowToDocument(String keyField) {
+    this.keyField = keyField;
+  }
+
+  public static void validateKeyFieldPresenceAndType(Schema schema, String 
keyField) {
+    if (!schema.getFieldNames().contains(keyField)
+        || 
!schema.getField(keyField).getType().getTypeName().equals(Schema.TypeName.STRING))
 {
+      throw new IllegalStateException(
+          "Field `"
+              + keyField
+              + "` should be present and of type `STRING`. Please change the 
type or specify a field to"
+              + " write the KEY value from.");
+    }
+  }
+
+  @Override
+  public PCollection<Document> expand(PCollection<Row> input) {
+    validateKeyFieldPresenceAndType(input.getSchema(), keyField);
+    LOG.info("Field to use as Document KEY is set to: `" + keyField + "`.");
+    return input.apply(ParDo.of(new RowToDocument.RowToDocumentConverter()));
+  }
+
+  /**
+   * Create a PTransform instance.
+   *
+   * @param keyField Row field containing a String key, must be set.
+   * @return {@code PTransform} instance for Row to Document conversion.
+   */
+  public static RowToDocument create(String keyField) {
+    return new RowToDocument(keyField);
+  }
+
+  class RowToDocumentConverter extends DoFn<Row, Document> {
+    RowToDocumentConverter() {
+      super();
+    }
+
+    @ProcessElement
+    public void processElement(ProcessContext context) {
+      Row row = context.element();
+
+      Schema schemaWithoutKeyField =
+          Schema.builder()
+              .addFields(
+                  row.getSchema().getFields().stream()
+                      .filter(field -> !field.getName().equals(keyField))
+                      .collect(Collectors.toList()))
+              .build();
+      Document.Builder documentBuilder = Document.newBuilder();
+      documentBuilder.putAllFields(constructMapFromRow(schemaWithoutKeyField, 
row).getFieldsMap());
+      documentBuilder.setName(constructKeyFromRow(row));
+
+      context.output(documentBuilder.build());
+    }
+
+    /**
+     * Converts an entire {@code Row} to an appropriate Firestore {@code 
MapValue}.
+     *
+     * @param row {@code Row} to convert.
+     * @return resulting {@code MapValue}.
+     */
+    private MapValue constructMapFromRow(Schema schema, Row row) {
+      MapValue.Builder mapValueBuilder = MapValue.newBuilder();
+      for (Schema.Field field : schema.getFields()) {
+        Value val = mapObjectToValue(row.getValue(field.getName()));
+        mapValueBuilder.putFields(field.getName(), val);
+      }
+      return mapValueBuilder.build();
+    }
+
+    /**
+     * Create a random key for a {@code Row} without a keyField or use a 
user-specified key by

Review Comment:
   outdated javadoc / missing feature TBD which one



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to