[ 
https://issues.apache.org/jira/browse/BEAM-5191?focusedWorklogId=270476&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-270476
 ]

ASF GitHub Bot logged work on BEAM-5191:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 01/Jul/19 21:13
            Start Date: 01/Jul/19 21:13
    Worklog Time Spent: 10m 
      Work Description: jklukas commented on pull request #8945: [BEAM-5191] 
Support for BigQuery clustering
URL: https://github.com/apache/beam/pull/8945#discussion_r299220024
 
 

 ##########
 File path: 
sdks/java/io/google-cloud-platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryTimePartitioningClusteringIT.java
 ##########
 @@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.io.gcp.bigquery;
+
+import com.google.api.services.bigquery.Bigquery;
+import com.google.api.services.bigquery.model.Clustering;
+import com.google.api.services.bigquery.model.Table;
+import com.google.api.services.bigquery.model.TableFieldSchema;
+import com.google.api.services.bigquery.model.TableRow;
+import com.google.api.services.bigquery.model.TableSchema;
+import com.google.api.services.bigquery.model.TimePartitioning;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.beam.sdk.Pipeline;
+import org.apache.beam.sdk.io.gcp.testing.BigqueryClient;
+import org.apache.beam.sdk.options.Default;
+import org.apache.beam.sdk.options.Description;
+import org.apache.beam.sdk.options.ExperimentalOptions;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.sdk.testing.TestPipeline;
+import org.apache.beam.sdk.testing.TestPipelineOptions;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.apache.beam.sdk.transforms.ParDo;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Integration test that partitions and clusters sample data in BigQuery. */
+@RunWith(JUnit4.class)
+public class BigQueryTimePartitioningClusteringIT {
+  private static final String WEATHER_SAMPLES_TABLE =
+      "clouddataflow-readonly:samples.weather_stations";
+
+  private Bigquery bqClient;
+  private String datasetName = "BigQueryTimePartitioningIT";
+  private BigQueryClusteringITOptions options;
+
+  @Before
+  public void setUp() {
+    PipelineOptionsFactory.register(BigQueryClusteringITOptions.class);
+    options = 
TestPipeline.testingPipelineOptions().as(BigQueryClusteringITOptions.class);
+    options.setTempLocation(options.getTempRoot() + "/temp-it/");
+    bqClient = BigqueryClient.getNewBigquerryClient(options.getAppName());
+  }
+
+  /** Customized PipelineOptions for BigQueryClustering Integration Test. */
+  public interface BigQueryClusteringITOptions
+      extends TestPipelineOptions, ExperimentalOptions, BigQueryOptions {
+    @Description("Table to read from, specified as " + 
"<project_id>:<dataset_id>.<table_id>")
+    @Default.String(WEATHER_SAMPLES_TABLE)
+    String getInput();
+
+    void setInput(String value);
+  }
+
+  static class KeepStationNumberAndConvertDate extends DoFn<TableRow, 
TableRow> {
+    @ProcessElement
+    public void processElement(ProcessContext c) {
+      String day = (String) c.element().get("day");
+      String month = (String) c.element().get("month");
+      String year = (String) c.element().get("year");
+
+      TableRow row = new TableRow();
+      row.set("station_number", c.element().get("station_number"));
+      row.set("date", String.format("%s-%s-%s", year, month, day));
+      c.output(row);
+    }
+  }
+
+  @Test
+  public void testE2EBigQueryTimePartitioning() throws Exception {
+    String tableName = "weather_stations_time_partitioned_" + 
System.currentTimeMillis();
+
+    Pipeline p = Pipeline.create(options);
+
+    p.apply(BigQueryIO.readTableRows().from(options.getInput()))
+        .apply(ParDo.of(new KeepStationNumberAndConvertDate()))
+        .apply(
+            BigQueryIO.writeTableRows()
+                .to(String.format("%s.%s", datasetName, tableName))
+                .withTimePartitioning(getTimepartitioning())
+                .withSchema(getSchema())
+                
.withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED)
+                
.withWriteDisposition(BigQueryIO.Write.WriteDisposition.WRITE_TRUNCATE));
+
+    p.run().waitUntilFinish();
 
 Review comment:
   I believe these ITs are the work of @wscheep in the previous PR, and I am 
going to borrow liberally from these examples to add the dynamic destinations 
ITs.
 
----------------------------------------------------------------
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


Issue Time Tracking
-------------------

    Worklog Id:     (was: 270476)
    Time Spent: 9.5h  (was: 9h 20m)

> Add support for writing to BigQuery clustered tables
> ----------------------------------------------------
>
>                 Key: BEAM-5191
>                 URL: https://issues.apache.org/jira/browse/BEAM-5191
>             Project: Beam
>          Issue Type: Improvement
>          Components: io-java-gcp
>    Affects Versions: 2.6.0
>            Reporter: Robert Sahlin
>            Assignee: Wout Scheepers
>            Priority: Minor
>              Labels: features, newbie
>          Time Spent: 9.5h
>  Remaining Estimate: 0h
>
> Google recently added support for clustered tables in BigQuery. It would be 
> useful to set clustering columns the same way as for partitioning. It should 
> support multiple fields (4) for clustering.
> For example:
> [BigQueryIO.Write|https://beam.apache.org/documentation/sdks/javadoc/2.6.0/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.Write.html]<[T|https://beam.apache.org/documentation/sdks/javadoc/2.6.0/org/apache/beam/sdk/io/gcp/bigquery/BigQueryIO.Write.html]>
>  .withClustering(new Clustering().setField("productId").setType("STRING"))



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to