Skip to content

Commit 7fedacd

Browse files
feat: sample - run a legacy SQL query (#97)
* feat: sample - run a legacy SQL query * nit: update * nit: update * update base on comments * update base on comments
1 parent 743bc0a commit 7fedacd

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
// [START bigquery_query_legacy]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.QueryJobConfiguration;
24+
import com.google.cloud.bigquery.TableResult;
25+
26+
public class RunLegacyQuery {
27+
28+
public static void runLegacyQuery() {
29+
try {
30+
// Initialize client that will be used to send requests. This client only needs to be created
31+
// once, and can be reused for multiple requests.
32+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
33+
34+
// To use legacy SQL syntax, set useLegacySql to true.
35+
String query =
36+
"SELECT corpus FROM [bigquery-public-data:samples.shakespeare] GROUP BY corpus;";
37+
QueryJobConfiguration queryConfig =
38+
QueryJobConfiguration.newBuilder(query).setUseLegacySql(true).build();
39+
40+
// Execute the query.
41+
TableResult result = bigquery.query(queryConfig);
42+
43+
// Print the results.
44+
result.iterateAll().forEach(rows -> rows.forEach(row -> System.out.println(row.getValue())));
45+
46+
System.out.println("Legacy query ran successfully");
47+
} catch (BigQueryException | InterruptedException e) {
48+
System.out.println("Legacy query did not run \n" + e.toString());
49+
}
50+
}
51+
}
52+
// [END bigquery_query_legacy]

samples/src/test/java/com/example/bigquery/CopyMultipleTablesIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public void testCopyMultipleTables() {
7070
CopyMultipleTables.copyMultipleTables(BIGQUERY_DATASET_NAME, generatedTableName);
7171
assertThat(bout.toString()).contains("Table copied successfully.");
7272

73-
//Clean up
73+
// Clean up
7474
DeleteTable.deleteTable(BIGQUERY_DATASET_NAME, generatedTableName);
7575
}
7676
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.PrintStream;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
public class RunLegacyQueryIT {
28+
private ByteArrayOutputStream bout;
29+
private PrintStream out;
30+
31+
@Before
32+
public void setUp() {
33+
bout = new ByteArrayOutputStream();
34+
out = new PrintStream(bout);
35+
System.setOut(out);
36+
}
37+
38+
@After
39+
public void tearDown() {
40+
System.setOut(null);
41+
}
42+
43+
@Test
44+
public void testRunLegacyQuery() {
45+
RunLegacyQuery.runLegacyQuery();
46+
assertThat(bout.toString()).contains("Legacy query ran successfully");
47+
}
48+
}

0 commit comments

Comments
 (0)