[
https://issues.apache.org/jira/browse/BEAM-8376?focusedWorklogId=352133&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-352133
]
ASF GitHub Bot logged work on BEAM-8376:
----------------------------------------
Author: ASF GitHub Bot
Created on: 02/Dec/19 19:40
Start Date: 02/Dec/19 19:40
Worklog Time Spent: 10m
Work Description: chamikaramj commented on pull request #10187:
[BEAM-8376] Initial version of firestore connector JavaSDK
URL: https://github.com/apache/beam/pull/10187#discussion_r352765791
##########
File path:
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/firestore/FirestoreWriterFn.java
##########
@@ -0,0 +1,203 @@
+/*
+ * 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 com.google.api.core.ApiFuture;
+import com.google.cloud.firestore.Firestore;
+import com.google.cloud.firestore.FirestoreException;
+import com.google.cloud.firestore.FirestoreOptions;
+import com.google.cloud.firestore.WriteResult;
+import com.google.rpc.Code;
+import org.apache.beam.sdk.metrics.Counter;
+import org.apache.beam.sdk.metrics.Metrics;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.display.DisplayData;
+import org.apache.beam.sdk.util.BackOff;
+import org.apache.beam.sdk.util.BackOffUtils;
+import org.apache.beam.sdk.util.FluentBackoff;
+import org.apache.beam.sdk.util.Sleeper;
+import
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableSet;
+import org.joda.time.Duration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.ExecutionException;
+
+public class FirestoreWriterFn<T> extends DoFn<T, Void> {
+
+ private static final Set<Integer> NON_RETRYABLE_ERRORS =
+ ImmutableSet.of(
+ Code.FAILED_PRECONDITION.getNumber(),
+ Code.INVALID_ARGUMENT.getNumber(),
+ Code.PERMISSION_DENIED.getNumber(),
+ Code.UNAUTHENTICATED.getNumber());
+ private static final Logger LOG =
LoggerFactory.getLogger(FirestoreWriterFn.class);
+
+ // Current batch of mutations to be written.
+ private final List<T> mutations = new ArrayList<T>();
+ private int mutationsSize = 0; // Accumulated size of protos in mutations.
+ private WriteBatcher writeBatcher;
+ private transient AdaptiveThrottler throttler;
+ private final Counter throttledSeconds =
+ Metrics.counter(FirestoreWriterFn.class,
"cumulativeThrottlingSeconds");
+ private final Counter rpcErrors =
+ Metrics.counter(FirestoreWriterFn.class, "firestoreRpcErrors");
+ private final Counter rpcSuccesses =
+ Metrics.counter(FirestoreWriterFn.class, "firestoreRpcSuccesses");
+
+ private static final int MAX_RETRIES = 5;
+ private static final FluentBackoff BUNDLE_WRITE_BACKOFF =
+ FluentBackoff.DEFAULT
+ .withMaxRetries(MAX_RETRIES)
+ .withInitialBackoff(Duration.standardSeconds(5));
+ private String outputTable;
+ private Firestore firestore;
+ private String firestoreKey;
+
+ public FirestoreWriterFn(String outputTable, String firestoreKey) {
+ this.outputTable = outputTable;
+ this.writeBatcher = new WriteBatcherImpl();
+ this.firestoreKey = firestoreKey;
+ }
+
+ @StartBundle
+ public void startBundle(StartBundleContext c) {
+ firestore = FirestoreOptions.getDefaultInstance().getService();
+ writeBatcher.start();
+ if (throttler == null) {
+ // Initialize throttler at first use, because it is not
serializable.
+ throttler = new AdaptiveThrottler(120000, 10000, 1.25);
+ }
+ }
+
+ @ProcessElement
+ public void processElement(ProcessContext c) throws Exception {
+ mutations.add(c.element());
+ if (mutations.size() >=
writeBatcher.nextBatchSize(System.currentTimeMillis())) {
+ flushBatch();
+ }
+ }
+
+ @FinishBundle
+ public void finishBundle() throws Exception {
+ if (!mutations.isEmpty()) {
+ flushBatch();
+ }
+ }
+
+ /**
+ * Writes a batch of mutations to Cloud Firestore.
+ *
+ * <p>If a commit fails, it will be retried up to {@link #MAX_RETRIES}
times. All mutations in
Review comment:
Can this result in duplicate data for records that were written in the
previous try ?
----------------------------------------------------------------
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]
Issue Time Tracking
-------------------
Worklog Id: (was: 352133)
> Add FirestoreIO connector to Java SDK
> -------------------------------------
>
> Key: BEAM-8376
> URL: https://issues.apache.org/jira/browse/BEAM-8376
> Project: Beam
> Issue Type: New Feature
> Components: io-java-gcp
> Reporter: Stefan Djelekar
> Priority: Major
> Time Spent: 40m
> Remaining Estimate: 0h
>
> Motivation:
> There is no Firestore connector for Java SDK at the moment.
> Having it will enhance the integrations with database options on the Google
> Cloud Platform.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)