You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by "EricJoy2048 (via GitHub)" <gi...@apache.org> on 2023/01/31 11:38:25 UTC

[GitHub] [incubator-seatunnel] EricJoy2048 commented on a diff in pull request #3848: [Feature][Connector V2] Add GoogleSheets Sink

EricJoy2048 commented on code in PR #3848:
URL: https://github.com/apache/incubator-seatunnel/pull/3848#discussion_r1091801795


##########
docs/en/connector-v2/sink/GoogleSheets.md:
##########
@@ -0,0 +1,55 @@
+# GoogleSheets
+
+> GoogleSheets sink connector
+## Description
+
+Used to write data to GoogleSheets.
+
+## Key features
+
+- [ ] [exactly-once](../../concept/connector-v2-features.md)
+- [ ] [schema projection](../../concept/connector-v2-features.md)

Review Comment:
   `schema projection` is not sink key features. Please reference https://seatunnel.apache.org/docs/concept/connector-v2-features.



##########
seatunnel-connectors-v2/connector-google-sheets/src/main/java/org/apache/seatunnel/connectors/seatunnel/google/sheets/sink/SheetsSinkWriter.java:
##########
@@ -0,0 +1,95 @@
+/*
+ * 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.seatunnel.connectors.seatunnel.google.sheets.sink;
+
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSinkWriter;
+import org.apache.seatunnel.connectors.seatunnel.google.sheets.config.RangePosition;
+import org.apache.seatunnel.connectors.seatunnel.google.sheets.config.SheetsParameters;
+import org.apache.seatunnel.connectors.seatunnel.google.sheets.serialize.GoogleSheetsSerializer;
+import org.apache.seatunnel.connectors.seatunnel.google.sheets.serialize.SeaTunnelRowSerializer;
+
+import com.google.api.services.sheets.v4.Sheets;
+import com.google.api.services.sheets.v4.model.BatchUpdateValuesRequest;
+import com.google.api.services.sheets.v4.model.ValueRange;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+public class SheetsSinkWriter extends AbstractSinkWriter<SeaTunnelRow, Void> {
+
+    private final SheetsParameters sheetsParameters;
+    private final SeaTunnelRowSerializer seaTunnelRowSerializer;
+    private final Sheets service;
+    private final List<SeaTunnelRow> seaTunnelRowList;
+    private final RangePosition rangePosition;
+    private final Integer targetRowCount;
+    private final Integer batchSize = 100;
+    private Integer totalCount = 0;
+
+    public SheetsSinkWriter(SheetsParameters sheetsParameters, RangePosition rangePosition) throws IOException {
+        this.sheetsParameters = sheetsParameters;
+        this.seaTunnelRowSerializer = new GoogleSheetsSerializer();
+        this.service = sheetsParameters.buildSheets();
+        this.rangePosition = rangePosition;
+        this.targetRowCount = rangePosition.getEndY() - rangePosition.getStartY() + 1;
+        this.seaTunnelRowList = new ArrayList<>(this.targetRowCount);
+    }
+
+    @Override
+    public void write(SeaTunnelRow element) throws IOException {
+        seaTunnelRowList.add(element);
+        totalCount++;
+        if (totalCount % batchSize == 0 || totalCount >= targetRowCount) {
+            flush();
+        }
+    }
+
+    public void flush() throws IOException {
+        List<List<Object>> values = seaTunnelRowSerializer.deserializeRow(seaTunnelRowList);
+        List<ValueRange> data = new ArrayList<>();
+
+        String start = rangePosition.getStartX();
+        String end = rangePosition.getEndX() + (rangePosition.getStartY() + totalCount - 1);
+        if (targetRowCount >= batchSize) {
+            // If it is the last batch
+            if (totalCount >= targetRowCount) {
+                start += rangePosition.getEndY() - (totalCount % batchSize) + 1;
+            } else {
+                start += rangePosition.getStartY() + totalCount - batchSize;
+            }
+        } else {
+            start += rangePosition.getStartY();
+        }
+
+        data.add(new ValueRange()
+                .setRange(start + ":" + end)
+                .setValues(values));
+        BatchUpdateValuesRequest body = new BatchUpdateValuesRequest()
+                .setValueInputOption("RAW")
+                .setData(data);
+        service.spreadsheets().values().batchUpdate(sheetsParameters.getSheetId(), body).execute();
+        seaTunnelRowList.clear();
+    }
+
+    @Override
+    public void close() throws IOException {
+        // not need close

Review Comment:
   Need `flush()` ?



-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org