You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by GitBox <gi...@apache.org> on 2022/10/26 11:34:28 UTC

[GitHub] [incubator-seatunnel] TaoZex commented on a diff in pull request #3185: [Feature][Connector-V2][GoogleSheets] Support GoogleSheets Source

TaoZex commented on code in PR #3185:
URL: https://github.com/apache/incubator-seatunnel/pull/3185#discussion_r1005558408


##########
seatunnel-connectors-v2/connector-google-sheets/src/main/java/org/apache/seatunnel/connectors/seatunnel/google/sheets/source/SheetsSource.java:
##########
@@ -0,0 +1,78 @@
+/*
+ * 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.source;
+
+import org.apache.seatunnel.api.common.PrepareFailException;
+import org.apache.seatunnel.api.serialization.DeserializationSchema;
+import org.apache.seatunnel.api.source.Boundedness;
+import org.apache.seatunnel.api.source.SeaTunnelSource;
+import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.connectors.seatunnel.common.schema.SeaTunnelSchema;
+import org.apache.seatunnel.connectors.seatunnel.common.source.AbstractSingleSplitReader;
+import org.apache.seatunnel.connectors.seatunnel.common.source.AbstractSingleSplitSource;
+import org.apache.seatunnel.connectors.seatunnel.common.source.SingleSplitReaderContext;
+import org.apache.seatunnel.connectors.seatunnel.google.sheets.config.SheetsParameters;
+import org.apache.seatunnel.format.json.JsonDeserializationSchema;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import com.google.auto.service.AutoService;
+
+@AutoService(SeaTunnelSource.class)
+public class SheetsSource extends AbstractSingleSplitSource<SeaTunnelRow> {
+
+    private SeaTunnelRowType seaTunnelRowType;
+
+    private SheetsParameters sheetsParameters;
+
+    private DeserializationSchema<SeaTunnelRow> deserializationSchema;
+
+    @Override
+    public String getPluginName() {
+        return "GoogleSheets";
+    }
+
+    @Override
+    public void prepare(Config pluginConfig) throws PrepareFailException {
+        this.sheetsParameters = new SheetsParameters().buildWithConfig(pluginConfig);

Review Comment:
   need parameter check



##########
seatunnel-connectors-v2/connector-google-sheets/src/main/java/org/apache/seatunnel/connectors/seatunnel/google/sheets/source/SheetsSourceReader.java:
##########
@@ -0,0 +1,111 @@
+/*
+ * 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.source;
+
+import org.apache.seatunnel.api.serialization.DeserializationSchema;
+import org.apache.seatunnel.api.source.Collector;
+import org.apache.seatunnel.api.table.type.SeaTunnelRow;
+import org.apache.seatunnel.common.utils.JsonUtils;
+import org.apache.seatunnel.connectors.seatunnel.common.source.AbstractSingleSplitReader;
+import org.apache.seatunnel.connectors.seatunnel.common.source.SingleSplitReaderContext;
+import org.apache.seatunnel.connectors.seatunnel.google.sheets.config.SheetsParameters;
+
+import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
+import com.google.api.client.http.HttpRequestInitializer;
+import com.google.api.client.http.javanet.NetHttpTransport;
+import com.google.api.client.json.JsonFactory;
+import com.google.api.client.json.gson.GsonFactory;
+import com.google.api.services.sheets.v4.Sheets;
+import com.google.api.services.sheets.v4.SheetsScopes;
+import com.google.api.services.sheets.v4.model.ValueRange;
+import com.google.auth.http.HttpCredentialsAdapter;
+import com.google.auth.oauth2.ServiceAccountCredentials;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Base64;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class SheetsSourceReader extends AbstractSingleSplitReader<SeaTunnelRow> {
+
+    private SheetsParameters sheetsParameters;
+
+    private HttpRequestInitializer requestInitializer;
+
+    private static final String APPLICATION_NAME = "SeaTunnel Google Sheets";
+
+    private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
+
+    private final SingleSplitReaderContext context;
+
+    private DeserializationSchema<SeaTunnelRow> deserializationSchema;
+
+    public SheetsSourceReader(SheetsParameters sheetsParameters, SingleSplitReaderContext context, DeserializationSchema<SeaTunnelRow> deserializationSchema) throws IOException {
+        this.sheetsParameters = sheetsParameters;
+        this.context = context;
+        this.deserializationSchema = deserializationSchema;
+    }
+
+    @Override
+    public void open() throws Exception {
+        byte[] keyBytes = Base64.getDecoder().decode(sheetsParameters.getServiceAccountKey());
+        ServiceAccountCredentials sourceCredentials = ServiceAccountCredentials
+            .fromStream(new ByteArrayInputStream(keyBytes));
+        sourceCredentials = (ServiceAccountCredentials) sourceCredentials
+            .createScoped(Collections.singletonList(SheetsScopes.SPREADSHEETS));
+        requestInitializer = new HttpCredentialsAdapter(sourceCredentials);
+
+    }
+
+    @Override
+    public void close() throws IOException {
+

Review Comment:
   Do we need to close service?



##########
docs/en/connector-v2/source/GoogleSheets.md:
##########
@@ -0,0 +1,61 @@
+# GoogleSheets
+
+> GoogleSheets source connector
+
+## Description
+
+Used to read data from GoogleSheets.
+
+## Options
+
+| name                | type         | required | default value |
+|-------------------  |--------------|----------|---------------|
+| service_account_key | string       | yes      | -             |
+| sheet_id            | string       | yes      | -             |
+| sheet_name          | string       | yes      | -             |
+| headers             | List<String> | yes      | -             |
+| schema              | config       | No       | -             |
+
+

Review Comment:
   delete this line



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