piotr-szuberski commented on a change in pull request #13319:
URL: https://github.com/apache/beam/pull/13319#discussion_r529385828



##########
File path: 
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigtable/BigtableTable.java
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.extensions.sql.meta.provider.bigtable;
+
+import static java.util.stream.Collectors.toSet;
+import static org.apache.beam.sdk.io.gcp.bigtable.RowUtils.COLUMNS_MAPPING;
+import static org.apache.beam.sdk.io.gcp.bigtable.RowUtils.KEY;
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists.newArrayList;
+
+import com.alibaba.fastjson.JSONObject;
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.extensions.sql.impl.BeamTableStatistics;
+import org.apache.beam.sdk.extensions.sql.meta.SchemaBaseBeamTable;
+import org.apache.beam.sdk.extensions.sql.meta.Table;
+import org.apache.beam.sdk.extensions.sql.meta.provider.InvalidTableException;
+import org.apache.beam.sdk.io.gcp.bigtable.BigtableIO;
+import org.apache.beam.sdk.io.gcp.bigtable.BigtableRowToBeamRow;
+import org.apache.beam.sdk.io.gcp.bigtable.BigtableRowToBeamRowFlat;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.values.PBegin;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.POutput;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Splitter;
+
+@Experimental
+public class BigtableTable extends SchemaBaseBeamTable implements Serializable 
{
+  // Should match:
+  // 
googleapis.com/bigtable/projects/projectId/instances/instanceId/tables/tableId"
+  private static final Pattern locationPattern =
+      Pattern.compile(
+          
"(?<host>.+)/bigtable/projects/(?<projectId>.+)/instances/(?<instanceId>.+)/tables/(?<tableId>.+)");
+
+  private final String projectId;
+  private final String instanceId;
+  private final String tableId;
+  private String emulatorHost = "";
+
+  private boolean useFlatSchema = false;
+
+  private Map<String, List<String>> columnsMapping = new HashMap<>();
+
+  BigtableTable(Table table) {
+    super(table.getSchema());
+    validateSchema(schema);
+
+    String location = table.getLocation();
+    if (location == null) {
+      throw new IllegalStateException("LOCATION is required");
+    }
+    Matcher matcher = locationPattern.matcher(location);
+    validateMatcher(matcher, location);
+
+    this.projectId = getMatcherValue(matcher, "projectId");
+    this.instanceId = getMatcherValue(matcher, "instanceId");
+    this.tableId = getMatcherValue(matcher, "tableId");
+    String host = getMatcherValue(matcher, "host"); // googleapis.com or 
localhost:<PORT>
+    if (!"googleapis.com".equals(host)) {
+      this.emulatorHost = host;
+    }
+
+    JSONObject properties = table.getProperties();
+    if (properties.containsKey(COLUMNS_MAPPING)) {
+      columnsMapping = 
parseColumnsMapping(properties.getString(COLUMNS_MAPPING));
+      validateColumnsMapping(columnsMapping, schema);
+      useFlatSchema = true;
+    }
+  }
+
+  @Override
+  public PCollection<Row> buildIOReader(PBegin begin) {
+    BigtableIO.Read readTransform =
+        
BigtableIO.read().withProjectId(projectId).withInstanceId(instanceId).withTableId(tableId);
+    if (!emulatorHost.isEmpty()) {
+      readTransform = readTransform.withEmulator(emulatorHost);
+    }
+    return readTransform
+        .expand(begin)
+        .apply(
+            "BigtableRowToBeamRow",
+            useFlatSchema
+                ? new BigtableRowToBeamRowFlat(schema, columnsMapping)
+                : new BigtableRowToBeamRow(schema))
+        .setRowSchema(schema);
+  }
+
+  @Override
+  public POutput buildIOWriter(PCollection<Row> input) {
+    throw new UnsupportedOperationException("Write to Cloud Bigtable is not 
yet supported");
+  }
+
+  @Override
+  public PCollection.IsBounded isBounded() {
+    return PCollection.IsBounded.BOUNDED;
+  }
+
+  @Override
+  public BeamTableStatistics getTableStatistics(PipelineOptions options) {
+    return BeamTableStatistics.BOUNDED_UNKNOWN;
+  }
+
+  private static Map<String, List<String>> parseColumnsMapping(String 
commaSeparatedMapping) {

Review comment:
       Done.

##########
File path: 
sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigtable/BigtableTable.java
##########
@@ -0,0 +1,200 @@
+/*
+ * 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.extensions.sql.meta.provider.bigtable;
+
+import static java.util.stream.Collectors.toSet;
+import static org.apache.beam.sdk.io.gcp.bigtable.RowUtils.COLUMNS_MAPPING;
+import static org.apache.beam.sdk.io.gcp.bigtable.RowUtils.KEY;
+import static 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists.newArrayList;
+
+import com.alibaba.fastjson.JSONObject;
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.beam.sdk.annotations.Experimental;
+import org.apache.beam.sdk.extensions.sql.impl.BeamTableStatistics;
+import org.apache.beam.sdk.extensions.sql.meta.SchemaBaseBeamTable;
+import org.apache.beam.sdk.extensions.sql.meta.Table;
+import org.apache.beam.sdk.extensions.sql.meta.provider.InvalidTableException;
+import org.apache.beam.sdk.io.gcp.bigtable.BigtableIO;
+import org.apache.beam.sdk.io.gcp.bigtable.BigtableRowToBeamRow;
+import org.apache.beam.sdk.io.gcp.bigtable.BigtableRowToBeamRowFlat;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.values.PBegin;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.POutput;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Splitter;
+
+@Experimental
+public class BigtableTable extends SchemaBaseBeamTable implements Serializable 
{
+  // Should match:
+  // 
googleapis.com/bigtable/projects/projectId/instances/instanceId/tables/tableId"
+  private static final Pattern locationPattern =
+      Pattern.compile(
+          
"(?<host>.+)/bigtable/projects/(?<projectId>.+)/instances/(?<instanceId>.+)/tables/(?<tableId>.+)");
+
+  private final String projectId;
+  private final String instanceId;
+  private final String tableId;
+  private String emulatorHost = "";
+
+  private boolean useFlatSchema = false;
+
+  private Map<String, List<String>> columnsMapping = new HashMap<>();
+
+  BigtableTable(Table table) {
+    super(table.getSchema());
+    validateSchema(schema);
+
+    String location = table.getLocation();
+    if (location == null) {
+      throw new IllegalStateException("LOCATION is required");
+    }
+    Matcher matcher = locationPattern.matcher(location);
+    validateMatcher(matcher, location);
+
+    this.projectId = getMatcherValue(matcher, "projectId");
+    this.instanceId = getMatcherValue(matcher, "instanceId");
+    this.tableId = getMatcherValue(matcher, "tableId");
+    String host = getMatcherValue(matcher, "host"); // googleapis.com or 
localhost:<PORT>
+    if (!"googleapis.com".equals(host)) {
+      this.emulatorHost = host;
+    }
+
+    JSONObject properties = table.getProperties();
+    if (properties.containsKey(COLUMNS_MAPPING)) {
+      columnsMapping = 
parseColumnsMapping(properties.getString(COLUMNS_MAPPING));
+      validateColumnsMapping(columnsMapping, schema);
+      useFlatSchema = true;
+    }
+  }
+
+  @Override
+  public PCollection<Row> buildIOReader(PBegin begin) {
+    BigtableIO.Read readTransform =
+        
BigtableIO.read().withProjectId(projectId).withInstanceId(instanceId).withTableId(tableId);
+    if (!emulatorHost.isEmpty()) {
+      readTransform = readTransform.withEmulator(emulatorHost);
+    }
+    return readTransform
+        .expand(begin)
+        .apply(
+            "BigtableRowToBeamRow",
+            useFlatSchema
+                ? new BigtableRowToBeamRowFlat(schema, columnsMapping)
+                : new BigtableRowToBeamRow(schema))
+        .setRowSchema(schema);
+  }
+
+  @Override
+  public POutput buildIOWriter(PCollection<Row> input) {
+    throw new UnsupportedOperationException("Write to Cloud Bigtable is not 
yet supported");
+  }
+
+  @Override
+  public PCollection.IsBounded isBounded() {
+    return PCollection.IsBounded.BOUNDED;
+  }
+
+  @Override
+  public BeamTableStatistics getTableStatistics(PipelineOptions options) {
+    return BeamTableStatistics.BOUNDED_UNKNOWN;
+  }
+
+  private static Map<String, List<String>> parseColumnsMapping(String 
commaSeparatedMapping) {

Review comment:
       I've remembered why they were static. That's because of null checker - 
it refuses to use non-static functions in the constructor (got required 
@Initialized and @NonNull BigtableTable)




----------------------------------------------------------------
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:
us...@infra.apache.org


Reply via email to