Skip to content

Commit f24ebd3

Browse files
authored
Merge pull request #13083: [BEAM-7034] Add example snippet to read fromQuery using BQ Storage API.
2 parents f0a3f6d + 3c15bf3 commit f24ebd3

2 files changed

Lines changed: 75 additions & 1 deletion

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.beam.examples.snippets.transforms.io.gcp.bigquery;
19+
20+
// [START bigquery_read_from_query_with_bigquery_storage_api]
21+
22+
import org.apache.beam.examples.snippets.transforms.io.gcp.bigquery.BigQueryMyData.MyData;
23+
import org.apache.beam.sdk.Pipeline;
24+
import org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO;
25+
import org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO.TypedRead.Method;
26+
import org.apache.beam.sdk.transforms.MapElements;
27+
import org.apache.beam.sdk.values.PCollection;
28+
import org.apache.beam.sdk.values.TypeDescriptor;
29+
30+
class BigQueryReadFromQueryWithBigQueryStorageAPI {
31+
public static PCollection<MyData> readFromQueryWithBigQueryStorageAPI(
32+
String project, String dataset, String table, String query, Pipeline pipeline) {
33+
34+
// String project = "my-project-id";
35+
// String dataset = "my_bigquery_dataset_id";
36+
// String table = "my_bigquery_table_id";
37+
38+
// Pipeline pipeline = Pipeline.create();
39+
40+
/*
41+
String query = String.format("SELECT\n" +
42+
" string_field,\n" +
43+
" int64_field,\n" +
44+
" float64_field,\n" +
45+
" numeric_field,\n" +
46+
" bool_field,\n" +
47+
" bytes_field,\n" +
48+
" date_field,\n" +
49+
" datetime_field,\n" +
50+
" time_field,\n" +
51+
" timestamp_field,\n" +
52+
" geography_field,\n" +
53+
" array_field,\n" +
54+
" struct_field\n" +
55+
"FROM\n" +
56+
" `%s:%s.%s`", project, dataset, table)
57+
*/
58+
59+
PCollection<MyData> rows =
60+
pipeline
61+
.apply(
62+
"Read from BigQuery table",
63+
BigQueryIO.readTableRows()
64+
.fromQuery(query)
65+
.usingStandardSql()
66+
.withMethod(Method.DIRECT_READ))
67+
.apply(
68+
"TableRows to MyData",
69+
MapElements.into(TypeDescriptor.of(MyData.class)).via(MyData::fromTableRow));
70+
71+
return rows;
72+
}
73+
}
74+
// [END bigquery_read_from_query_with_bigquery_storage_api]

website/www/site/content/en/documentation/io/built-in/google-bigquery.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ GitHub](https://github.com/apache/beam/blob/master/examples/java/src/main/java/o
365365
The following code snippet reads with a query string.
366366

367367
{{< highlight java >}}
368-
// Snippet not yet available (BEAM-7034).
368+
{{< code_sample "examples/java/src/main/java/org/apache/beam/examples/snippets/transforms/io/gcp/bigquery/BigQueryReadFromQueryWithBigQueryStorageAPI.java" bigquery_read_from_query_with_bigquery_storage_api >}}
369369
{{< /highlight >}}
370370

371371
{{< highlight py >}}

0 commit comments

Comments
 (0)