-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveQueryToTable.java
More file actions
30 lines (26 loc) · 1.33 KB
/
Copy pathSaveQueryToTable.java
File metadata and controls
30 lines (26 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import com.google.cloud.bigquery.*;
public class SaveQueryToTable {
public static void main(String[] args) {
String query = "SELECT corpus FROM `bigquery-public-data.samples.shakespeare` Group By corpus";
String destinationTable = "MY_TABLE_NAME2";
String destinationDataSet = "MY_DATASET_NAME";
saveQueryToTable(destinationDataSet, destinationTable, query);
}
public static void saveQueryToTable(String destinationDataSet, String destinationTableId, String query) {
try {
//initialize bigquery client
BigQuery bigQuery = BigQueryOptions.getDefaultInstance().getService();
// identify the destination table
TableId destinationTable = TableId.of(destinationDataSet, destinationTableId);
//build the query job
QueryJobConfiguration queryJobConfiguration =
QueryJobConfiguration.newBuilder(query).setDestinationTable(destinationTable).build();
//execute the query
bigQuery.query(queryJobConfiguration);
//the results are now save in the destination table
System.out.println("Saved query ran successfully");
} catch (BigQueryException | InterruptedException e) {
System.out.println("Saved Query did not run.\n" + e.toString());
}
}
}