|
14 | 14 |
|
15 | 15 | import time
|
16 | 16 |
|
| 17 | +from google.api_core.retry import Retry |
17 | 18 | import google.auth
|
18 | 19 | import google.datalab
|
19 | 20 | import IPython
|
20 | 21 | from IPython.terminal import interactiveshell
|
21 | 22 | from IPython.testing import tools
|
22 | 23 | import pytest
|
23 | 24 |
|
24 |
| - |
25 | 25 | # Get default project
|
26 | 26 | _, PROJECT_ID = google.auth.default()
|
27 | 27 | # Set Datalab project ID
|
@@ -270,46 +270,50 @@ def test_client_library_load_table_from_gcs_csv(to_delete):
|
270 | 270 |
|
271 | 271 |
|
272 | 272 | def test_datalab_load_table_from_dataframe(to_delete):
|
273 |
| - # [START bigquery_migration_datalab_load_table_from_dataframe] |
274 |
| - import google.datalab.bigquery as bq |
275 |
| - import pandas |
276 |
| - |
277 |
| - # Create the dataset |
278 |
| - dataset_id = 'import_sample' |
279 |
| - # [END bigquery_migration_datalab_load_table_from_dataframe] |
280 |
| - # Use unique dataset ID to avoid collisions when running tests |
281 |
| - dataset_id = 'test_dataset_{}'.format(int(time.time() * 1000)) |
282 |
| - to_delete.append(dataset_id) |
283 |
| - # [START bigquery_migration_datalab_load_table_from_dataframe] |
284 |
| - bq.Dataset(dataset_id).create() |
285 |
| - |
286 |
| - # Create the table and load the data |
287 |
| - dataframe = pandas.DataFrame([ |
288 |
| - {'title': 'The Meaning of Life', 'release_year': 1983}, |
289 |
| - {'title': 'Monty Python and the Holy Grail', 'release_year': 1975}, |
290 |
| - {'title': 'Life of Brian', 'release_year': 1979}, |
291 |
| - { |
292 |
| - 'title': 'And Now for Something Completely Different', |
293 |
| - 'release_year': 1971 |
294 |
| - }, |
295 |
| - ]) |
296 |
| - schema = bq.Schema.from_data(dataframe) |
297 |
| - table = bq.Table( |
298 |
| - '{}.monty_python'.format(dataset_id)).create(schema=schema) |
299 |
| - table.insert(dataframe) # Starts steaming insert of data |
300 |
| - # [END bigquery_migration_datalab_load_table_from_dataframe] |
301 |
| - # The Datalab library uses tabledata().insertAll() to load data from |
302 |
| - # pandas DataFrames to tables. Because it can take a long time for the rows |
303 |
| - # to be available in the table, this test does not assert on the number of |
304 |
| - # rows in the destination table after the job is run. If errors are |
305 |
| - # encountered during the insertion, this test will fail. |
306 |
| - # See https://cloud.google.com/bigquery/streaming-data-into-bigquery |
| 273 | + """ Wrap test with retries to handle transient errors """ |
| 274 | + @Retry() |
| 275 | + def datalab_load_table_from_dataframe(to_delete): |
| 276 | + # [START bigquery_migration_datalab_load_table_from_dataframe] |
| 277 | + import google.datalab.bigquery as bq |
| 278 | + import pandas |
| 279 | + |
| 280 | + # Create the dataset |
| 281 | + dataset_id = 'import_sample' |
| 282 | + # [END bigquery_migration_datalab_load_table_from_dataframe] |
| 283 | + # Use unique dataset ID to avoid collisions when running tests |
| 284 | + dataset_id = 'test_dataset_{}'.format(int(time.time() * 1000)) |
| 285 | + to_delete.append(dataset_id) |
| 286 | + # [START bigquery_migration_datalab_load_table_from_dataframe] |
| 287 | + bq.Dataset(dataset_id).create() |
| 288 | + |
| 289 | + # Create the table and load the data |
| 290 | + dataframe = pandas.DataFrame([ |
| 291 | + {'title': 'The Meaning of Life', 'release_year': 1983}, |
| 292 | + {'title': 'Monty Python and the Holy Grail', 'release_year': 1975}, |
| 293 | + {'title': 'Life of Brian', 'release_year': 1979}, |
| 294 | + { |
| 295 | + 'title': 'And Now for Something Completely Different', |
| 296 | + 'release_year': 1971 |
| 297 | + }, |
| 298 | + ]) |
| 299 | + schema = bq.Schema.from_data(dataframe) |
| 300 | + table = bq.Table( |
| 301 | + '{}.monty_python'.format(dataset_id)).create(schema=schema) |
| 302 | + table.insert(dataframe) # Starts steaming insert of data |
| 303 | + # [END bigquery_migration_datalab_load_table_from_dataframe] |
| 304 | + # The Datalab library uses tabledata().insertAll() to load data from |
| 305 | + # pandas DataFrames to tables. Because it can take a long time for the rows |
| 306 | + # to be available in the table, this test does not assert on the number of |
| 307 | + # rows in the destination table after the job is run. If errors are |
| 308 | + # encountered during the insertion, this test will fail. |
| 309 | + # See https://cloud.google.com/bigquery/streaming-data-into-bigquery |
| 310 | + datalab_load_table_from_dataframe(to_delete) |
307 | 311 |
|
308 | 312 |
|
309 | 313 | def test_client_library_load_table_from_dataframe(to_delete):
|
310 | 314 | # [START bigquery_migration_client_library_load_table_from_dataframe]
|
311 |
| - from google.cloud import bigquery |
312 | 315 | import pandas
|
| 316 | + from google.cloud import bigquery |
313 | 317 |
|
314 | 318 | client = bigquery.Client(location='US')
|
315 | 319 |
|
|
0 commit comments