|
1 | 1 | /*
|
2 |
| - Copyright 2015, Google, Inc. |
3 |
| - Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
| - you may not use this file except in compliance with the License. |
5 |
| - You may obtain a copy of the License at |
6 |
| - |
7 |
| - http://www.apache.org/licenses/LICENSE-2.0 |
8 |
| - |
9 |
| - Unless required by applicable law or agreed to in writing, software |
10 |
| - distributed under the License is distributed on an "AS IS" BASIS, |
11 |
| - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
| - See the License for the specific language governing permissions and |
| 2 | + Copyright 2015, Google, Inc. |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
13 | 13 | limitations under the License.
|
14 | 14 | */
|
15 | 15 | package com.google.cloud.bigquery.samples;
|
|
24 | 24 | import java.util.Scanner;
|
25 | 25 |
|
26 | 26 | /**
|
27 |
| - * TODO: Insert description here. (generated by elibixby) |
| 27 | + * Sample of how to Export Cloud Data. |
28 | 28 | */
|
29 |
| -public class ExportDataCloudStorageSample extends BigqueryUtils { |
| 29 | +public class ExportDataCloudStorageSample { |
30 | 30 |
|
| 31 | + /** |
| 32 | + * Protected constructor since this is a collection of static functions. |
| 33 | + */ |
| 34 | + protected ExportDataCloudStorageSample() { |
| 35 | + super(); |
| 36 | + } |
31 | 37 |
|
32 |
| - |
| 38 | + /** |
| 39 | + * This program can be run to demonstrate running a Bigquery query from the |
| 40 | + * CLI. |
| 41 | + * @param args Command line args |
| 42 | + * @throws IOException If there is an error connceting to bigquery |
| 43 | + * @throws InterruptedException Should never be thrown. |
| 44 | + */ |
33 | 45 | // [START main]
|
34 |
| - public static void main(String[] args) throws IOException, InterruptedException{ |
| 46 | + public static void main(final String[] args) throws IOException, |
| 47 | + InterruptedException { |
35 | 48 | Scanner scanner = new Scanner(System.in);
|
36 | 49 | System.out.println("Enter your project id: ");
|
37 | 50 | String projectId = scanner.nextLine();
|
38 | 51 | System.out.println("Enter your dataset id: ");
|
39 | 52 | String datasetId = scanner.nextLine();
|
40 | 53 | System.out.println("Enter your table id: ");
|
41 | 54 | String tableId = scanner.nextLine();
|
42 |
| - System.out.println("Enter the Google Cloud Storage Path to which you'd like to export: "); |
| 55 | + System.out.println("Enter the Google Cloud Storage Path to which you'd " |
| 56 | + + "like to export: "); |
43 | 57 | String cloudStoragePath = scanner.nextLine();
|
44 |
| - System.out.println("Enter how often to check if your job is complete (milliseconds): "); |
| 58 | + System.out.println("Enter how often to check if your job is complete " |
| 59 | + + "(milliseconds): "); |
45 | 60 | long interval = scanner.nextLong();
|
46 | 61 | scanner.close();
|
47 |
| - |
| 62 | + |
48 | 63 | run(cloudStoragePath, projectId, datasetId, tableId, interval);
|
49 |
| - |
| 64 | + |
50 | 65 | }
|
51 | 66 | // [END main]
|
52 |
| - |
| 67 | + |
| 68 | + /** |
| 69 | + * Run the bigquery ClI. |
| 70 | + * @param cloudStoragePath The bucket we are using |
| 71 | + * @param projectId Project id |
| 72 | + * @param datasetId datasetid |
| 73 | + * @param tableId tableid |
| 74 | + * @param interval interval to wait between polling in milliseconds |
| 75 | + * @throws IOException Thrown if there is an error connecting to Bigquery. |
| 76 | + * @throws InterruptedException Should never be thrown |
| 77 | + */ |
53 | 78 | // [START run]
|
54 |
| - public static void run( |
55 |
| - String cloudStoragePath, |
56 |
| - String projectId, |
57 |
| - String datasetId, |
58 |
| - String tableId, |
59 |
| - long interval) throws IOException, InterruptedException{ |
| 79 | + public static void run( |
| 80 | + final String cloudStoragePath, |
| 81 | + final String projectId, |
| 82 | + final String datasetId, |
| 83 | + final String tableId, |
| 84 | + final long interval) throws IOException, InterruptedException { |
60 | 85 |
|
61 | 86 | Bigquery bigquery = BigqueryServiceFactory.getService();
|
62 |
| - |
| 87 | + |
63 | 88 | Job extractJob = extractJob(
|
64 | 89 | bigquery,
|
65 | 90 | cloudStoragePath,
|
66 | 91 | new TableReference()
|
67 | 92 | .setProjectId(projectId)
|
68 | 93 | .setDatasetId(datasetId)
|
69 | 94 | .setTableId(tableId));
|
70 |
| - |
71 |
| - Bigquery.Jobs.Get get_job = bigquery.jobs().get( |
72 |
| - extractJob.getJobReference().getProjectId(), |
| 95 | + |
| 96 | + Bigquery.Jobs.Get getJob = bigquery.jobs().get( |
| 97 | + extractJob.getJobReference().getProjectId(), |
73 | 98 | extractJob.getJobReference().getJobId());
|
74 |
| - |
75 |
| - pollJob(get_job, interval); |
76 |
| - |
| 99 | + |
| 100 | + BigqueryUtils.pollJob(getJob, interval); |
| 101 | + |
77 | 102 | System.out.println("Export is Done!");
|
78 |
| - |
| 103 | + |
79 | 104 | }
|
80 | 105 | // [END run]
|
81 |
| - |
82 |
| - |
| 106 | + |
| 107 | + |
| 108 | + /** |
| 109 | + * A job that extracts data from a table. |
| 110 | + * @param bigquery Bigquery service to use |
| 111 | + * @param cloudStoragePath Cloud storage bucket we are inserting into |
| 112 | + * @param table Table to extract from |
| 113 | + * @return The job to extract data from the table |
| 114 | + * @throws IOException Thrown if error connceting to Bigtable |
| 115 | + */ |
83 | 116 | // [START extract_job]
|
84 | 117 | public static Job extractJob(
|
85 |
| - Bigquery bigquery, |
86 |
| - String cloudStoragePath, |
87 |
| - TableReference table) throws IOException{ |
88 |
| - |
| 118 | + final Bigquery bigquery, |
| 119 | + final String cloudStoragePath, |
| 120 | + final TableReference table) throws IOException { |
| 121 | + |
89 | 122 | JobConfigurationExtract extract = new JobConfigurationExtract()
|
90 | 123 | .setSourceTable(table)
|
91 | 124 | .setDestinationUri(cloudStoragePath);
|
92 | 125 |
|
93 |
| - return bigquery.jobs().insert(table.getProjectId(), |
| 126 | + return bigquery.jobs().insert(table.getProjectId(), |
94 | 127 | new Job().setConfiguration(new JobConfiguration().setExtract(extract)))
|
95 | 128 | .execute();
|
96 | 129 | }
|
97 | 130 | // [END extract_job]
|
98 | 131 |
|
99 |
| - |
100 |
| - |
| 132 | + |
| 133 | + |
101 | 134 |
|
102 | 135 | }
|
0 commit comments