|
| 1 | +Google Cloud Java Client for BigQuery |
| 2 | +==================================== |
| 3 | + |
| 4 | +Java idiomatic client for [Google Cloud BigQuery] (https://cloud.google.com/bigquery). |
| 5 | + |
| 6 | +[](https://travis-ci.org/GoogleCloudPlatform/gcloud-java) |
| 7 | +[](https://coveralls.io/r/GoogleCloudPlatform/gcloud-java?branch=master) |
| 8 | +<!-- TODO(mziccard): add in the maven shield once the artifact is pushed to maven --> |
| 9 | + |
| 10 | +- [Homepage] (https://googlecloudplatform.github.io/gcloud-java/) |
| 11 | +- [API Documentation] (http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/bigquery/package-summary.html) |
| 12 | + |
| 13 | +> Note: This client is a work-in-progress, and may occasionally |
| 14 | +> make backwards-incompatible changes. |
| 15 | +
|
| 16 | +Quickstart |
| 17 | +---------- |
| 18 | +If you are using Maven, add this to your pom.xml file |
| 19 | +<!-- TODO(mziccard): add mvn dependency code --> |
| 20 | + |
| 21 | +If you are using Gradle, add this to your dependencies |
| 22 | +<!-- TODO(mziccard): add gradle dependency code --> |
| 23 | + |
| 24 | +If you are using SBT, add this to your dependencies |
| 25 | +<!-- TODO(mziccard): add sbt dependency code --> |
| 26 | + |
| 27 | +Example Application |
| 28 | +------------------- |
| 29 | + |
| 30 | +<!-- TODO(mziccard): add example application --> |
| 31 | + |
| 32 | +Authentication |
| 33 | +-------------- |
| 34 | + |
| 35 | +See the [Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) section in the base directory's README. |
| 36 | + |
| 37 | +About Google Cloud BigQuery |
| 38 | +-------------------------- |
| 39 | + |
| 40 | +[Google Cloud BigQuery][cloud-bigquery] is a fully managed, NoOps, low cost data analytics service. |
| 41 | +Data can be streamed into BigQuery at millions of rows per second to enable real-time analysis. |
| 42 | +With BigQuery you can easily deploy Petabyte-scale Databases. |
| 43 | + |
| 44 | +Be sure to activate the Google Cloud BigQuery API on the Developer's Console to use BigQuery from |
| 45 | +your project. |
| 46 | + |
| 47 | +See the ``gcloud-java`` API [bigquery documentation][bigquery-api] to learn how to interact |
| 48 | +with Google Cloud BigQuery using this Client Library. |
| 49 | + |
| 50 | +Getting Started |
| 51 | +--------------- |
| 52 | +#### Prerequisites |
| 53 | +For this tutorial, you will need a |
| 54 | +[Google Developers Console](https://console.developers.google.com/) project with the BigQuery API |
| 55 | +enabled. You will need to [enable billing](https://support.google.com/cloud/answer/6158867?hl=en) to |
| 56 | +use Google Cloud BigQuery. |
| 57 | +[Follow these instructions](https://cloud.google.com/docs/authentication#preparation) to get your |
| 58 | +project set up. You will also need to set up the local development environment by [installing the |
| 59 | +Google Cloud SDK](https://cloud.google.com/sdk/) and running the following commands in command line: |
| 60 | +`gcloud auth login` and `gcloud config set project [YOUR PROJECT ID]`. |
| 61 | + |
| 62 | +#### Installation and setup |
| 63 | +You'll need to obtain the `gcloud-java-bigquery` library. See the [Quickstart](#quickstart) section |
| 64 | +to add `gcloud-java-bigquery` as a dependency in your code. |
| 65 | + |
| 66 | +#### Creating an authorized service object |
| 67 | +To make authenticated requests to Google Cloud BigQuery, you must create a service object with |
| 68 | +credentials. You can then make API calls by calling methods on the BigQuery service object. The |
| 69 | +simplest way to authenticate is to use |
| 70 | +[Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials). |
| 71 | +These credentials are automatically inferred from your environment, so you only need the following |
| 72 | +code to create your service object: |
| 73 | + |
| 74 | +```java |
| 75 | +import com.google.gcloud.bigquery.BigQuery; |
| 76 | +import com.google.gcloud.bigquery.BigQueryOptions; |
| 77 | + |
| 78 | +BigQuery bigquery = BigQueryOptions.defaultInstance().service(); |
| 79 | +``` |
| 80 | + |
| 81 | +For other authentication options, see the |
| 82 | +[Authentication](https://github.com/GoogleCloudPlatform/gcloud-java#authentication) page. |
| 83 | + |
| 84 | +#### Creating a dataset |
| 85 | +With BigQuery you can create datasets. A dataset is a grouping mechanism that holds zero or more |
| 86 | +tables. Add the following import at the top of your file: |
| 87 | + |
| 88 | +```java |
| 89 | +import com.google.gcloud.bigquery.DatasetInfo; |
| 90 | +``` |
| 91 | +Then, to create the dataset, use the following code: |
| 92 | + |
| 93 | +```java |
| 94 | +// Create a dataset |
| 95 | +String datasetId = "my_dataset_id"; |
| 96 | +bigquery.create(DatasetInfo.builder(datasetId).build()); |
| 97 | +``` |
| 98 | + |
| 99 | +#### Creating a table |
| 100 | +With BigQuery you can create different types of tables: normal tables with an associated schema, |
| 101 | +external tables backed by data stored on [Google Cloud Storage][cloud-storage] and view tables that |
| 102 | +are created from a BigQuery SQL query. In this code snippet we show how to create a normal table |
| 103 | +with only one string field. Add the following imports at the top of your file: |
| 104 | + |
| 105 | +```java |
| 106 | +import com.google.gcloud.bigquery.BaseTableInfo; |
| 107 | +import com.google.gcloud.bigquery.Field; |
| 108 | +import com.google.gcloud.bigquery.Schema; |
| 109 | +import com.google.gcloud.bigquery.TableId; |
| 110 | +import com.google.gcloud.bigquery.TableInfo; |
| 111 | +``` |
| 112 | +Then add the following code to create the table: |
| 113 | + |
| 114 | +```java |
| 115 | +TableId tableId = TableId.of(datasetId, "my_table_id"); |
| 116 | +// Table field definition |
| 117 | +Field stringField = Field.of("StringField", Field.Type.string()); |
| 118 | +// Table schema definition |
| 119 | +Schema schema = Schema.of(stringField); |
| 120 | +// Create a table |
| 121 | +TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, schema)); |
| 122 | +``` |
| 123 | + |
| 124 | +#### Loading data into a table |
| 125 | +BigQuery provides several ways to load data into a table: streaming rows or loading data from a |
| 126 | +Google Cloud Storage file. In this code snippet we show how to stream rows into a table. |
| 127 | +Add the following imports at the top of your file: |
| 128 | + |
| 129 | +```java |
| 130 | +import com.google.gcloud.bigquery.InsertAllRequest; |
| 131 | +import com.google.gcloud.bigquery.InsertAllResponse; |
| 132 | + |
| 133 | +import java.util.HashMap; |
| 134 | +import java.util.Map; |
| 135 | +``` |
| 136 | +Then add the following code to insert data: |
| 137 | + |
| 138 | +```java |
| 139 | +Map<String, Object> firstRow = new HashMap<>(); |
| 140 | +Map<String, Object> secondRow = new HashMap<>(); |
| 141 | +firstRow.put("StringField", "value1"); |
| 142 | +secondRow.put("StringField", "value2"); |
| 143 | +// Create an insert request |
| 144 | +InsertAllRequest insertRequest = InsertAllRequest.builder(tableId) |
| 145 | + .addRow(firstRow) |
| 146 | + .addRow(secondRow) |
| 147 | + .build(); |
| 148 | +// Insert rows |
| 149 | +InsertAllResponse insertResponse = bigquery.insertAll(insertRequest); |
| 150 | +// Check if errors occurred |
| 151 | +if (insertResponse.hasErrors()) { |
| 152 | + System.out.println("Errors occurred while inserting rows"); |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +#### Querying data |
| 157 | +BigQuery enables querying data by running queries and waiting for the result. Queries can be run |
| 158 | +directly or through a Query Job. In this code snippet we show how to run a query directly and wait |
| 159 | +for the result. Add the following imports at the top of your file: |
| 160 | + |
| 161 | +```java |
| 162 | +import com.google.gcloud.bigquery.FieldValue; |
| 163 | +import com.google.gcloud.bigquery.QueryRequest; |
| 164 | +import com.google.gcloud.bigquery.QueryResponse; |
| 165 | + |
| 166 | +import java.util.Iterator; |
| 167 | +import java.util.List; |
| 168 | +``` |
| 169 | +Then add the following code to run the query and wait for the result: |
| 170 | + |
| 171 | +```java |
| 172 | +// Create a query request |
| 173 | +QueryRequest queryRequest = |
| 174 | + QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id") |
| 175 | + .maxWaitTime(60000L) |
| 176 | + .maxResults(1000L) |
| 177 | + .build(); |
| 178 | +// Request query to be executed and wait for results |
| 179 | +QueryResponse queryResponse = bigquery.query(queryRequest); |
| 180 | +while (!queryResponse.jobComplete()) { |
| 181 | + Thread.sleep(1000L); |
| 182 | + queryResponse = bigquery.getQueryResults(queryResponse.jobId()); |
| 183 | +} |
| 184 | +// Read rows |
| 185 | +Iterator<List<FieldValue>> rowIterator = queryResponse.result().iterateAll(); |
| 186 | +System.out.println("Table rows:"); |
| 187 | +while (rowIterator.hasNext()) { |
| 188 | + System.out.println(rowIterator.next()); |
| 189 | +} |
| 190 | +``` |
| 191 | +#### Complete source code |
| 192 | + |
| 193 | +Here we put together all the code shown above into one program. This program assumes that you are |
| 194 | +running on Compute Engine or from your own desktop. To run this example on App Engine, simply move |
| 195 | +the code from the main method to your application's servlet class and change the print statements to |
| 196 | +display on your webpage. |
| 197 | + |
| 198 | +```java |
| 199 | +import com.google.gcloud.bigquery.BaseTableInfo; |
| 200 | +import com.google.gcloud.bigquery.BigQuery; |
| 201 | +import com.google.gcloud.bigquery.BigQueryOptions; |
| 202 | +import com.google.gcloud.bigquery.DatasetInfo; |
| 203 | +import com.google.gcloud.bigquery.Field; |
| 204 | +import com.google.gcloud.bigquery.FieldValue; |
| 205 | +import com.google.gcloud.bigquery.InsertAllRequest; |
| 206 | +import com.google.gcloud.bigquery.InsertAllResponse; |
| 207 | +import com.google.gcloud.bigquery.QueryRequest; |
| 208 | +import com.google.gcloud.bigquery.QueryResponse; |
| 209 | +import com.google.gcloud.bigquery.Schema; |
| 210 | +import com.google.gcloud.bigquery.TableId; |
| 211 | +import com.google.gcloud.bigquery.TableInfo; |
| 212 | + |
| 213 | +import java.util.HashMap; |
| 214 | +import java.util.Iterator; |
| 215 | +import java.util.List; |
| 216 | +import java.util.Map; |
| 217 | + |
| 218 | +public class GcloudBigQueryExample { |
| 219 | + |
| 220 | + public static void main(String[] args) throws InterruptedException { |
| 221 | + |
| 222 | + // Create a service instance |
| 223 | + BigQuery bigquery = BigQueryOptions.defaultInstance().service(); |
| 224 | + |
| 225 | + // Create a dataset |
| 226 | + String datasetId = "my_dataset_id"; |
| 227 | + bigquery.create(DatasetInfo.builder(datasetId).build()); |
| 228 | + |
| 229 | + TableId tableId = TableId.of(datasetId, "my_table_id"); |
| 230 | + // Table field definition |
| 231 | + Field stringField = Field.of("StringField", Field.Type.string()); |
| 232 | + // Table schema definition |
| 233 | + Schema schema = Schema.of(stringField); |
| 234 | + // Create a table |
| 235 | + TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, schema)); |
| 236 | + |
| 237 | + // Define rows to insert |
| 238 | + Map<String, Object> firstRow = new HashMap<>(); |
| 239 | + Map<String, Object> secondRow = new HashMap<>(); |
| 240 | + firstRow.put("StringField", "value1"); |
| 241 | + secondRow.put("StringField", "value2"); |
| 242 | + // Create an insert request |
| 243 | + InsertAllRequest insertRequest = InsertAllRequest.builder(tableId) |
| 244 | + .addRow(firstRow) |
| 245 | + .addRow(secondRow) |
| 246 | + .build(); |
| 247 | + // Insert rows |
| 248 | + InsertAllResponse insertResponse = bigquery.insertAll(insertRequest); |
| 249 | + // Check if errors occurred |
| 250 | + if (insertResponse.hasErrors()) { |
| 251 | + System.out.println("Errors occurred while inserting rows"); |
| 252 | + } |
| 253 | + |
| 254 | + // Create a query request |
| 255 | + QueryRequest queryRequest = |
| 256 | + QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id") |
| 257 | + .maxWaitTime(60000L) |
| 258 | + .maxResults(1000L) |
| 259 | + .build(); |
| 260 | + // Request query to be executed and wait for results |
| 261 | + QueryResponse queryResponse = bigquery.query(queryRequest); |
| 262 | + while (!queryResponse.jobComplete()) { |
| 263 | + Thread.sleep(1000L); |
| 264 | + queryResponse = bigquery.getQueryResults(queryResponse.jobId()); |
| 265 | + } |
| 266 | + // Read rows |
| 267 | + Iterator<List<FieldValue>> rowIterator = queryResponse.result().iterateAll(); |
| 268 | + System.out.println("Table rows:"); |
| 269 | + while (rowIterator.hasNext()) { |
| 270 | + System.out.println(rowIterator.next()); |
| 271 | + } |
| 272 | + } |
| 273 | +} |
| 274 | +``` |
| 275 | + |
| 276 | +Troubleshooting |
| 277 | +--------------- |
| 278 | + |
| 279 | +To get help, follow the `gcloud-java` links in the `gcloud-*`[shared Troubleshooting document](https://github.com/GoogleCloudPlatform/gcloud-common/blob/master/troubleshooting/readme.md#troubleshooting). |
| 280 | + |
| 281 | +Java Versions |
| 282 | +------------- |
| 283 | + |
| 284 | +Java 7 or above is required for using this client. |
| 285 | + |
| 286 | +Testing |
| 287 | +------- |
| 288 | + |
| 289 | +This library has tools to help make tests for code using Cloud BigQuery. |
| 290 | + |
| 291 | +See [TESTING] to read more about testing. |
| 292 | + |
| 293 | +Versioning |
| 294 | +---------- |
| 295 | + |
| 296 | +This library follows [Semantic Versioning] (http://semver.org/). |
| 297 | + |
| 298 | +It is currently in major version zero (``0.y.z``), which means that anything |
| 299 | +may change at any time and the public API should not be considered |
| 300 | +stable. |
| 301 | + |
| 302 | +Contributing |
| 303 | +------------ |
| 304 | + |
| 305 | +Contributions to this library are always welcome and highly encouraged. |
| 306 | + |
| 307 | +See [CONTRIBUTING] for more information on how to get started. |
| 308 | + |
| 309 | +License |
| 310 | +------- |
| 311 | + |
| 312 | +Apache 2.0 - See [LICENSE] for more information. |
| 313 | + |
| 314 | + |
| 315 | +[CONTRIBUTING]:https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/CONTRIBUTING.md |
| 316 | +[LICENSE]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/LICENSE |
| 317 | +[TESTING]: https://github.com/GoogleCloudPlatform/gcloud-java/blob/master/TESTING.md#testing-code-that-uses-bigquery |
| 318 | +[cloud-platform]: https://cloud.google.com/ |
| 319 | + |
| 320 | +[cloud-bigquery]: https://cloud.google.com/bigquery/ |
| 321 | +[cloud-storage]: https://cloud.google.com/storage/ |
| 322 | +[bigquery-api]: http://googlecloudplatform.github.io/gcloud-java/apidocs/index.html?com/google/gcloud/bigquery/package-summary.html |
0 commit comments