You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2020/11/12 18:11:26 UTC
[GitHub] [beam] piotr-szuberski opened a new pull request #13319: [BEAM-11173] Add Bigtable table with read operation
piotr-szuberski opened a new pull request #13319:
URL: https://github.com/apache/beam/pull/13319
Added Bigtable table provider with simple read operation - filters and write will be done in another one in the near future.
Problems:
BeamSQL doesn't support `ARRAY<ROW>`. If you use
```
CREATE TABLE test(
key VARCHAR,
family ROW<q1 ARRAY<ROW<val VARCHAR, timestampMicros BIGINT>>>
)
```
and then
`SELECT t.key, t.family.q1 FROM test t`
Calcite shouts that it couldn't parse the generated code.
What is possible is of little value:
`SELECT t.key, t.family.q1[0] FROM test t`
So it's blocked to select all cells with timestamp and labels for now, but as I read about Bigtable it's usually not the desired case (the schema advice is to have single value per cell and usually only the latest value is needed).
I used `val` instead of `value` because value is a keyword in SQL and hence would need to be used with backslashes.
------------------------
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
- [ ] [**Choose reviewer(s)**](https://beam.apache.org/contribute/#make-your-change) and mention them in a comment (`R: @username`).
- [ ] Format the pull request title like `[BEAM-XXX] Fixes bug in ApproximateQuantiles`, where you replace `BEAM-XXX` with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.
- [ ] Update `CHANGES.md` with noteworthy changes.
- [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/#make-reviewers-job-easier).
Post-Commit Tests Status (on master branch)
------------------------------------------------------------------------------------------------
Lang | SDK | Dataflow | Flink | Samza | Spark | Twister2
--- | --- | --- | --- | --- | --- | ---
Go | [](https://ci-beam.apache.org/job/beam_PostCommit_Go/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Go_VR_Spark/lastCompletedBuild/) | ---
Java | [](https://ci-beam.apache.org/job/beam_PostCommit_Java/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Dataflow_Java11/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Flink_Java11/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Batch/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Flink_Streaming/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Samza/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Spark/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_PVR_Spark_Batch/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_SparkStructuredStreaming/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Java_ValidatesRunner_Twister2/lastCompletedBuild/)
Python | [](https://ci-beam.apache.org/job/beam_PostCommit_Python36/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python37/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python38/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Py_VR_Dataflow_V2/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Py_ValCont/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Python_PVR_Flink_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PostCommit_Python_VR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_Python_VR_Spark/lastCompletedBuild/) | ---
XLang | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Direct/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Flink/lastCompletedBuild/) | --- | [](https://ci-beam.apache.org/job/beam_PostCommit_XVR_Spark/lastCompletedBuild/) | ---
Pre-Commit Tests Status (on master branch)
------------------------------------------------------------------------------------------------
--- |Java | Python | Go | Website | Whitespace | Typescript
--- | --- | --- | --- | --- | --- | ---
Non-portable | [](https://ci-beam.apache.org/job/beam_PreCommit_Java_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Python_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonLint_Cron/lastCompletedBuild/)<br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonDocker_Cron/lastCompletedBuild/) <br>[](https://ci-beam.apache.org/job/beam_PreCommit_PythonDocs_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Go_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Website_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Whitespace_Cron/lastCompletedBuild/) | [](https://ci-beam.apache.org/job/beam_PreCommit_Typescript_Cron/lastCompletedBuild/)
Portable | --- | [](https://ci-beam.apache.org/job/beam_PreCommit_Portable_Python_Cron/lastCompletedBuild/) | --- | --- | --- | ---
See [.test-infra/jenkins/README](https://github.com/apache/beam/blob/master/.test-infra/jenkins/README.md) for trigger phrase, status and link of all Jenkins jobs.
GitHub Actions Tests Status (on master branch)
------------------------------------------------------------------------------------------------
[](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
[](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on a change in pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #13319:
URL: https://github.com/apache/beam/pull/13319#discussion_r525026696
##########
File path: sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigtable/BigtableRowToBeamRow.java
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.toList;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.KEY;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.LABELS;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.TIMESTAMP_MICROS;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.VALUE;
+import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import com.google.bigtable.v2.Cell;
+import com.google.bigtable.v2.Column;
+import com.google.bigtable.v2.Family;
+import java.io.Serializable;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.primitives.Longs;
+
+@SuppressWarnings({
+ "nullness" // TODO(https://issues.apache.org/jira/browse/BEAM-10402)
+})
+public class BigtableRowToBeamRow
Review comment:
Done.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on a change in pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
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.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-732197328
@amaliujia
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on a change in pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #13319:
URL: https://github.com/apache/beam/pull/13319#discussion_r530213192
##########
File path: website/www/site/content/en/documentation/dsls/sql/extensions/create-external-table.md
##########
@@ -204,6 +205,131 @@ TYPE bigquery
LOCATION 'testing-integration:apache.users'
```
+## Cloud Bigtable
+
+### Syntax
+
+```
+CREATE EXTERNAL TABLE [ IF NOT EXISTS ] tableName (
+ key VARCHAR NOT NULL,
+ family ROW<qualifier cells [, qualifier cells ]* >
+ [, family ROW< qualifier cells [, qualifier cells ]* > ]*
+)
+TYPE bigtable
+LOCATION 'googleapis.com/bigtable/projects/[PROJECT_ID]/instances/[INSTANCE_ID]/tables/[TABLE]'
+```
+
+* `key`: key of the Bigtable row
+* `family`: name of the column family
+* `qualifier`: the column qualifier
+* `cells`: Either of each value:
+ * `TYPE`
+ * `ARRAY<SIMPLE_TYPE>`
+* `LOCATION`:
+ * `PROJECT_ID`: ID of the Google Cloud Project.
+ * `INSTANCE_ID`: Bigtable instance ID.
+ * `TABLE`: Bigtable Table ID.
+* `TYPE`: `SIMPLE_TYPE` or `CELL_ROW`
+* `CELL_ROW`: `ROW<val SIMPLE_TYPE [, timestampMicros BIGINT [NOT NULL]] [, labels ARRAY<VARCHAR> [NOT NULL]]`
+* `SIMPLE_TYPE`: on of the following:
+ * `BINARY`
+ * `VARCHAR`
+ * `BIGINT`
+ * `DOUBLE`
+ * `BOOLEAN`
Review comment:
Ok, will do!
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski edited a comment on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski edited a comment on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-728957557
@amaliujia I've added some javadoc but I'm not sure about it.
I've also added flat schema (like Spark does [1])so it should be possible to execute write operations in the near future.
I also tried to implement it so it would be easy to add the support of ARRAY\<ROW\> when Calcite is updated
[1] https://stackoverflow.com/questions/39530938/sparksql-on-hbase-tables
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] amaliujia merged pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
amaliujia merged pull request #13319:
URL: https://github.com/apache/beam/pull/13319
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on a change in pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #13319:
URL: https://github.com/apache/beam/pull/13319#discussion_r529413106
##########
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-733754684
Run Java_Examples_Dataflow PreCommit
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] amaliujia commented on a change in pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
amaliujia commented on a change in pull request #13319:
URL: https://github.com/apache/beam/pull/13319#discussion_r530100435
##########
File path: website/www/site/content/en/documentation/dsls/sql/extensions/create-external-table.md
##########
@@ -204,6 +205,131 @@ TYPE bigquery
LOCATION 'testing-integration:apache.users'
```
+## Cloud Bigtable
+
+### Syntax
+
+```
+CREATE EXTERNAL TABLE [ IF NOT EXISTS ] tableName (
+ key VARCHAR NOT NULL,
+ family ROW<qualifier cells [, qualifier cells ]* >
+ [, family ROW< qualifier cells [, qualifier cells ]* > ]*
+)
+TYPE bigtable
+LOCATION 'googleapis.com/bigtable/projects/[PROJECT_ID]/instances/[INSTANCE_ID]/tables/[TABLE]'
+```
+
+* `key`: key of the Bigtable row
+* `family`: name of the column family
+* `qualifier`: the column qualifier
+* `cells`: Either of each value:
+ * `TYPE`
+ * `ARRAY<SIMPLE_TYPE>`
+* `LOCATION`:
+ * `PROJECT_ID`: ID of the Google Cloud Project.
+ * `INSTANCE_ID`: Bigtable instance ID.
+ * `TABLE`: Bigtable Table ID.
+* `TYPE`: `SIMPLE_TYPE` or `CELL_ROW`
+* `CELL_ROW`: `ROW<val SIMPLE_TYPE [, timestampMicros BIGINT [NOT NULL]] [, labels ARRAY<VARCHAR> [NOT NULL]]`
+* `SIMPLE_TYPE`: on of the following:
+ * `BINARY`
+ * `VARCHAR`
+ * `BIGINT`
+ * `DOUBLE`
+ * `BOOLEAN`
Review comment:
Maybe just do that in this PR to make this feature complete?
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] amaliujia commented on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
amaliujia commented on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-736247818
LGTM
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] amaliujia commented on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
amaliujia commented on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-729441462
Thank you! I will take a another look
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-726853283
I'm having a quite hard time with raw protobuf problem. It's forbidden to use raw protobuf in `sdks:java:extensions:sql` but Bigtable requires it if I want to write tests.
I can't use the pubsub's approach and move Bigtable table provide to `:sdks:java:io:gcp` because later I'll need BeamSqlTableFilter which is in beam sql extensions.
A solution would be to create some testing utility that wraps the BigtableEmulator and bigtable Row creation. But it would have to be public to be able to use it from the SQL module.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski removed a comment on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski removed a comment on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-726853283
I'm having a quite hard time with raw protobuf problem. It's forbidden to use raw protobuf in `sdks:java:extensions:sql` but Bigtable requires it if I want to write tests.
I can't use the pubsub's approach and move Bigtable table provide to `:sdks:java:io:gcp` because later I'll need BeamSqlTableFilter which is in beam sql extensions.
A solution would be to create some testing utility that wraps the BigtableEmulator and bigtable Row creation. But it would have to be public to be able to use it from the SQL module.
EDIT: I've found that io:gcp has a testing package. I'll emplace the emulator wrapper in there.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on a change in pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski edited a comment on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski edited a comment on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-726853283
I'm having a quite hard time with raw protobuf problem. It's forbidden to use raw protobuf in `sdks:java:extensions:sql` but Bigtable requires it if I want to write tests.
I can't use the pubsub's approach and move Bigtable table provide to `:sdks:java:io:gcp` because later I'll need BeamSqlTableFilter which is in beam sql extensions.
A solution would be to create some testing utility that wraps the BigtableEmulator and bigtable Row creation. But it would have to be public to be able to use it from the SQL module.
EDIT: I've found that io:gcp has a testing package. I'll emplace the emulator wrapper in there.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-728957557
@amaliujia I've added some javadoc but I'm not sure about it.
I've also added flat schema (like Spark does [1])so it should be possible to execute write operations in the near future.
[1] https://stackoverflow.com/questions/39530938/sparksql-on-hbase-tables
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on a change in pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #13319:
URL: https://github.com/apache/beam/pull/13319#discussion_r525031544
##########
File path: sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigtable/BigtableRowToBeamRow.java
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.toList;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.KEY;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.LABELS;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.TIMESTAMP_MICROS;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.VALUE;
+import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import com.google.bigtable.v2.Cell;
+import com.google.bigtable.v2.Column;
+import com.google.bigtable.v2.Family;
+import java.io.Serializable;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.primitives.Longs;
+
+@SuppressWarnings({
+ "nullness" // TODO(https://issues.apache.org/jira/browse/BEAM-10402)
+})
+public class BigtableRowToBeamRow
Review comment:
I've written some javadoc, but I'm not sure whether it's understable
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-727823590
> My current question is, for the problem you find that ARRAY is not well supported, what part of design have it affected?
It affects the possibility of reading multiple cells with more fields than `value`, e.g.:
```
key VARCHAR,
family1 ROW<
column1 ARRAY<ROW<val BOOLEAN, timestampMicros BIGINT>>
>
```
For now it's possible only to get the array of cell's values.
In general Flink and Spark don't support the array of values. Instead they provide only recent values without the metadata like `timestampMicros` or `labels`.
The Flink's approach is already implemented.
Spark's approach is to skip families and columns in the schema definition, provide just values and then add mapping to each value in the TBLPROPERTIES in format
```
schemaField1 BIGINT,
schemaField2 VARCHAR
TBLPROPERTIES { "hbase.columns.mapping": "familyName:columnName:schemaField1,familyName:columnName:schemaField2"`
```
On the other hand BigQuery does support it.
Maybe for the simplicity's sake should we just stick to the Flink and Spark version?
I can see that in Pubsub table for some reason it's possible to use only flattened schema for the Write operation. If it's not possible to do so with ROW then Spark's approach would fit well for it.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on a change in pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #13319:
URL: https://github.com/apache/beam/pull/13319#discussion_r529413106
##########
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:
Oh, 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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on a change in pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #13319:
URL: https://github.com/apache/beam/pull/13319#discussion_r529421367
##########
File path: website/www/site/content/en/documentation/dsls/sql/extensions/create-external-table.md
##########
@@ -204,6 +205,131 @@ TYPE bigquery
LOCATION 'testing-integration:apache.users'
```
+## Cloud Bigtable
+
+### Syntax
+
+```
+CREATE EXTERNAL TABLE [ IF NOT EXISTS ] tableName (
+ key VARCHAR NOT NULL,
+ family ROW<qualifier cells [, qualifier cells ]* >
+ [, family ROW< qualifier cells [, qualifier cells ]* > ]*
+)
+TYPE bigtable
+LOCATION 'googleapis.com/bigtable/projects/[PROJECT_ID]/instances/[INSTANCE_ID]/tables/[TABLE]'
+```
+
+* `key`: key of the Bigtable row
+* `family`: name of the column family
+* `qualifier`: the column qualifier
+* `cells`: Either of each value:
+ * `TYPE`
+ * `ARRAY<SIMPLE_TYPE>`
+* `LOCATION`:
+ * `PROJECT_ID`: ID of the Google Cloud Project.
+ * `INSTANCE_ID`: Bigtable instance ID.
+ * `TABLE`: Bigtable Table ID.
+* `TYPE`: `SIMPLE_TYPE` or `CELL_ROW`
+* `CELL_ROW`: `ROW<val SIMPLE_TYPE [, timestampMicros BIGINT [NOT NULL]] [, labels ARRAY<VARCHAR> [NOT NULL]]`
+* `SIMPLE_TYPE`: on of the following:
+ * `BINARY`
+ * `VARCHAR`
+ * `BIGINT`
+ * `DOUBLE`
+ * `BOOLEAN`
Review comment:
BigQuery supports only those 5 types. But I agree that there is no reason why not to support others as well. Should I do that in this PR or in the next 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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-728956089
Run SQL PostCommit
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] amaliujia commented on a change in pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
amaliujia commented on a change in pull request #13319:
URL: https://github.com/apache/beam/pull/13319#discussion_r529004528
##########
File path: website/www/site/content/en/documentation/dsls/sql/extensions/create-external-table.md
##########
@@ -204,6 +205,131 @@ TYPE bigquery
LOCATION 'testing-integration:apache.users'
```
+## Cloud Bigtable
+
+### Syntax
+
+```
+CREATE EXTERNAL TABLE [ IF NOT EXISTS ] tableName (
+ key VARCHAR NOT NULL,
+ family ROW<qualifier cells [, qualifier cells ]* >
+ [, family ROW< qualifier cells [, qualifier cells ]* > ]*
+)
+TYPE bigtable
+LOCATION 'googleapis.com/bigtable/projects/[PROJECT_ID]/instances/[INSTANCE_ID]/tables/[TABLE]'
+```
+
+* `key`: key of the Bigtable row
+* `family`: name of the column family
+* `qualifier`: the column qualifier
+* `cells`: Either of each value:
+ * `TYPE`
+ * `ARRAY<SIMPLE_TYPE>`
+* `LOCATION`:
+ * `PROJECT_ID`: ID of the Google Cloud Project.
+ * `INSTANCE_ID`: Bigtable instance ID.
+ * `TABLE`: Bigtable Table ID.
+* `TYPE`: `SIMPLE_TYPE` or `CELL_ROW`
+* `CELL_ROW`: `ROW<val SIMPLE_TYPE [, timestampMicros BIGINT [NOT NULL]] [, labels ARRAY<VARCHAR> [NOT NULL]]`
+* `SIMPLE_TYPE`: on of the following:
+ * `BINARY`
+ * `VARCHAR`
+ * `BIGINT`
+ * `DOUBLE`
+ * `BOOLEAN`
Review comment:
So what is the reason that single type only supports these 5 types (i.e. not include other SQL types like TIMESTAMP)?
##########
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:
for this functions and other functions below, as they are already private functions, maybe should remove the `static`?
##########
File path: sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigtable/BigtableTableCreationFailuresTest.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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 org.apache.beam.sdk.io.gcp.testing.BigtableTestUtils.checkMessage;
+import static org.junit.Assert.assertThrows;
+
+import org.apache.beam.sdk.extensions.sql.BeamSqlCli;
+import org.apache.beam.sdk.extensions.sql.impl.ParseException;
+import org.apache.beam.sdk.extensions.sql.meta.Table;
+import org.apache.beam.sdk.extensions.sql.meta.provider.TableProvider;
+import org.apache.beam.sdk.extensions.sql.meta.store.InMemoryMetaStore;
+import org.junit.Before;
+import org.junit.Test;
+
+@SuppressWarnings({
+ "nullness" // TODO(https://issues.apache.org/jira/browse/BEAM-10402)
+})
+public class BigtableTableCreationFailuresTest {
+
+ private final InMemoryMetaStore metaStore = new InMemoryMetaStore();
+ private final TableProvider tableProvider = new BigtableTableProvider();
+ private BeamSqlCli cli;
+
+ @Before
+ public void setUp() {
+ metaStore.registerProvider(tableProvider);
+ cli = new BeamSqlCli().metaStore(metaStore);
+ }
+
+ @Test
+ public void testCreateWithoutTypeFails() {
+ String createTable = "CREATE EXTERNAL TABLE failure(something VARCHAR)";
+ ParseException e = assertThrows(ParseException.class, () -> cli.execute(createTable));
+ checkMessage(e.getMessage(), "Unable to parse query");
+ }
+
+ @Test
+ public void testCreateWithoutLocationFails() {
+ String createTable =
+ "CREATE EXTERNAL TABLE fail(key VARCHAR, something VARCHAR) \n" + "TYPE bigtable \n";
+ cli.execute(createTable);
+ Table table = metaStore.getTables().get("fail");
+
+ IllegalStateException e =
+ assertThrows(IllegalStateException.class, () -> tableProvider.buildBeamSqlTable(table));
+ checkMessage(e.getMessage(), "LOCATION");
+ }
+
+ @Test
+ public void testCreateWithoutKeyFails() {
+ String createTable =
+ "CREATE EXTERNAL TABLE fail(something VARCHAR) \n"
+ + "TYPE bigtable \n"
+ + "LOCATION '"
+ + location()
+ + "'";
+ cli.execute(createTable);
+ Table table = metaStore.getTables().get("fail");
+ IllegalStateException e =
+ assertThrows(IllegalStateException.class, () -> tableProvider.buildBeamSqlTable(table));
+ checkMessage(e.getMessage(), "Schema has to contain 'key' field");
+ }
+
+ @Test
+ public void testCreateWrongKeyTypeFails() {
+ String createTable =
+ "CREATE EXTERNAL TABLE fail(key FLOAT) \n"
+ + "TYPE bigtable \n"
+ + "LOCATION '"
+ + location()
+ + "'";
+ cli.execute(createTable);
+ Table table = metaStore.getTables().get("fail");
+ IllegalArgumentException e =
+ assertThrows(IllegalArgumentException.class, () -> tableProvider.buildBeamSqlTable(table));
+ checkMessage(e.getMessage(), "key field type should be STRING but was FLOAT");
+ }
+
+ @Test
+ public void testCreatePropertiesDontMatchSchema() {
+ String createTable =
+ "CREATE EXTERNAL TABLE fail(key VARCHAR, q BIGINT, qq BINARY) \n"
+ + "TYPE bigtable \n"
+ + "LOCATION '"
+ + location()
+ + "' \n"
+ + "TBLPROPERTIES '{\"columnsMapping\": \"f:b,f:c\"}'";
+ cli.execute(createTable);
+ Table table = metaStore.getTables().get("fail");
+ IllegalStateException e =
+ assertThrows(IllegalStateException.class, () -> tableProvider.buildBeamSqlTable(table));
+ checkMessage(e.getMessage(), "does not fit to schema field names");
+ }
+
+ @Test
+ public void testCreatePropertiesCountNotEqualSchemaFields() {
+ String createTable =
+ "CREATE EXTERNAL TABLE fail(key VARCHAR, q BIGINT, qq BINARY) \n"
+ + "TYPE bigtable \n"
+ + "LOCATION '"
+ + location()
+ + "' \n"
+ + "TBLPROPERTIES '{\"columnsMapping\": \"f:q\"}'";
+ cli.execute(createTable);
+ Table table = metaStore.getTables().get("fail");
+ IllegalStateException e =
+ assertThrows(IllegalStateException.class, () -> tableProvider.buildBeamSqlTable(table));
+ checkMessage(e.getMessage(), "Schema fields count: '2' does not fit columnsMapping count: '1'");
+ }
+
+ private static String location() {
+ return "googleapis.com/bigtable/projects/fakeProject/instances/fakeInstance/tables/beamTable";
+ }
+}
Review comment:
Add a test to hit
```
private static void validateMatcher(Matcher matcher, String location) {
if (!matcher.matches()) {
throw new InvalidTableException(
"Bigtable location must be in the following format:"
+ " 'googleapis.com/bigtable/projects/projectId/instances/instanceId/tables/tableId'"
+ " but was: "
+ location);
}
}
```
?
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on a change in pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #13319:
URL: https://github.com/apache/beam/pull/13319#discussion_r529413106
##########
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 error)
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski removed a comment on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski removed a comment on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-728956089
Run SQL PostCommit
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-733725578
@amaliujia I've moved the logic of parsing cell value to CellValueParser and added unit tests for parsing other BeamSQL types.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on a change in pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on a change in pull request #13319:
URL: https://github.com/apache/beam/pull/13319#discussion_r529388818
##########
File path: sdks/java/extensions/sql/src/test/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigtable/BigtableTableCreationFailuresTest.java
##########
@@ -0,0 +1,130 @@
+/*
+ * 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 org.apache.beam.sdk.io.gcp.testing.BigtableTestUtils.checkMessage;
+import static org.junit.Assert.assertThrows;
+
+import org.apache.beam.sdk.extensions.sql.BeamSqlCli;
+import org.apache.beam.sdk.extensions.sql.impl.ParseException;
+import org.apache.beam.sdk.extensions.sql.meta.Table;
+import org.apache.beam.sdk.extensions.sql.meta.provider.TableProvider;
+import org.apache.beam.sdk.extensions.sql.meta.store.InMemoryMetaStore;
+import org.junit.Before;
+import org.junit.Test;
+
+@SuppressWarnings({
+ "nullness" // TODO(https://issues.apache.org/jira/browse/BEAM-10402)
+})
+public class BigtableTableCreationFailuresTest {
+
+ private final InMemoryMetaStore metaStore = new InMemoryMetaStore();
+ private final TableProvider tableProvider = new BigtableTableProvider();
+ private BeamSqlCli cli;
+
+ @Before
+ public void setUp() {
+ metaStore.registerProvider(tableProvider);
+ cli = new BeamSqlCli().metaStore(metaStore);
+ }
+
+ @Test
+ public void testCreateWithoutTypeFails() {
+ String createTable = "CREATE EXTERNAL TABLE failure(something VARCHAR)";
+ ParseException e = assertThrows(ParseException.class, () -> cli.execute(createTable));
+ checkMessage(e.getMessage(), "Unable to parse query");
+ }
+
+ @Test
+ public void testCreateWithoutLocationFails() {
+ String createTable =
+ "CREATE EXTERNAL TABLE fail(key VARCHAR, something VARCHAR) \n" + "TYPE bigtable \n";
+ cli.execute(createTable);
+ Table table = metaStore.getTables().get("fail");
+
+ IllegalStateException e =
+ assertThrows(IllegalStateException.class, () -> tableProvider.buildBeamSqlTable(table));
+ checkMessage(e.getMessage(), "LOCATION");
+ }
+
+ @Test
+ public void testCreateWithoutKeyFails() {
+ String createTable =
+ "CREATE EXTERNAL TABLE fail(something VARCHAR) \n"
+ + "TYPE bigtable \n"
+ + "LOCATION '"
+ + location()
+ + "'";
+ cli.execute(createTable);
+ Table table = metaStore.getTables().get("fail");
+ IllegalStateException e =
+ assertThrows(IllegalStateException.class, () -> tableProvider.buildBeamSqlTable(table));
+ checkMessage(e.getMessage(), "Schema has to contain 'key' field");
+ }
+
+ @Test
+ public void testCreateWrongKeyTypeFails() {
+ String createTable =
+ "CREATE EXTERNAL TABLE fail(key FLOAT) \n"
+ + "TYPE bigtable \n"
+ + "LOCATION '"
+ + location()
+ + "'";
+ cli.execute(createTable);
+ Table table = metaStore.getTables().get("fail");
+ IllegalArgumentException e =
+ assertThrows(IllegalArgumentException.class, () -> tableProvider.buildBeamSqlTable(table));
+ checkMessage(e.getMessage(), "key field type should be STRING but was FLOAT");
+ }
+
+ @Test
+ public void testCreatePropertiesDontMatchSchema() {
+ String createTable =
+ "CREATE EXTERNAL TABLE fail(key VARCHAR, q BIGINT, qq BINARY) \n"
+ + "TYPE bigtable \n"
+ + "LOCATION '"
+ + location()
+ + "' \n"
+ + "TBLPROPERTIES '{\"columnsMapping\": \"f:b,f:c\"}'";
+ cli.execute(createTable);
+ Table table = metaStore.getTables().get("fail");
+ IllegalStateException e =
+ assertThrows(IllegalStateException.class, () -> tableProvider.buildBeamSqlTable(table));
+ checkMessage(e.getMessage(), "does not fit to schema field names");
+ }
+
+ @Test
+ public void testCreatePropertiesCountNotEqualSchemaFields() {
+ String createTable =
+ "CREATE EXTERNAL TABLE fail(key VARCHAR, q BIGINT, qq BINARY) \n"
+ + "TYPE bigtable \n"
+ + "LOCATION '"
+ + location()
+ + "' \n"
+ + "TBLPROPERTIES '{\"columnsMapping\": \"f:q\"}'";
+ cli.execute(createTable);
+ Table table = metaStore.getTables().get("fail");
+ IllegalStateException e =
+ assertThrows(IllegalStateException.class, () -> tableProvider.buildBeamSqlTable(table));
+ checkMessage(e.getMessage(), "Schema fields count: '2' does not fit columnsMapping count: '1'");
+ }
+
+ private static String location() {
+ return "googleapis.com/bigtable/projects/fakeProject/instances/fakeInstance/tables/beamTable";
+ }
+}
Review comment:
Done.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski edited a comment on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski edited a comment on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-728957557
@amaliujia I've added some javadoc but I'm not sure about it.
I've also added flat schema (like Spark does [1])so it should be possible to execute write operations in the near future.
I also tried to implement it so it would be easy to add the support of ARRAY<ROW> when Calcite is updated
[1] https://stackoverflow.com/questions/39530938/sparksql-on-hbase-tables
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-729706764
I've moved the transforms to io/bigtable because Write transforms need to import `com.google.protobuf.ByteString` which is not allowed in extensions/sql
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] amaliujia commented on a change in pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
amaliujia commented on a change in pull request #13319:
URL: https://github.com/apache/beam/pull/13319#discussion_r523246921
##########
File path: sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigtable/BigtableRowToBeamRow.java
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.toList;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.KEY;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.LABELS;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.TIMESTAMP_MICROS;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.VALUE;
+import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import com.google.bigtable.v2.Cell;
+import com.google.bigtable.v2.Column;
+import com.google.bigtable.v2.Family;
+import java.io.Serializable;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.primitives.Longs;
+
+@SuppressWarnings({
+ "nullness" // TODO(https://issues.apache.org/jira/browse/BEAM-10402)
+})
+public class BigtableRowToBeamRow
Review comment:
Nit: it might be better to list the BigTable reference: https://cloud.google.com/bigtable/docs/reference/data/rpc/google.bigtable.v2
##########
File path: sdks/java/extensions/sql/src/main/java/org/apache/beam/sdk/extensions/sql/meta/provider/bigtable/BigtableRowToBeamRow.java
##########
@@ -0,0 +1,160 @@
+/*
+ * 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.toList;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.KEY;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.LABELS;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.TIMESTAMP_MICROS;
+import static org.apache.beam.sdk.extensions.sql.meta.provider.bigtable.BigtableTable.VALUE;
+import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkArgument;
+
+import com.google.bigtable.v2.Cell;
+import com.google.bigtable.v2.Column;
+import com.google.bigtable.v2.Family;
+import java.io.Serializable;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.transforms.MapElements;
+import org.apache.beam.sdk.transforms.PTransform;
+import org.apache.beam.sdk.transforms.SimpleFunction;
+import org.apache.beam.sdk.values.KV;
+import org.apache.beam.sdk.values.PCollection;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.primitives.Longs;
+
+@SuppressWarnings({
+ "nullness" // TODO(https://issues.apache.org/jira/browse/BEAM-10402)
+})
+public class BigtableRowToBeamRow
Review comment:
Can you add java doc here to give briefly design decision for mapping from bigtable row to Beam row.
I see that bigtable row contains
Row: Key, Family[]
Family: ... Column[]
Column: ... Cell[]
Cell: ...Value
document current mapping seems will be straightforward?
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [beam] piotr-szuberski commented on pull request #13319: [BEAM-11173] Add Bigtable table with read operation
Posted by GitBox <gi...@apache.org>.
piotr-szuberski commented on pull request #13319:
URL: https://github.com/apache/beam/pull/13319#issuecomment-735647862
@amaliujia
----------------------------------------------------------------
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:
users@infra.apache.org