Insert the given rows and batch them if needed. Args: inserts: a list of Inserts, each of which represents a row. Returns: A json explained here: https://cloud.google.com/bigquery/docs/reference/rest/v2/tabledata/insertAll
(self, inserts)
| 362 | |
| 363 | @environment.local_noop |
| 364 | def insert(self, inserts): |
| 365 | # pylint: disable=line-too-long |
| 366 | """Insert the given rows and batch them if needed. |
| 367 | |
| 368 | Args: |
| 369 | inserts: a list of Inserts, each of which represents a row. |
| 370 | |
| 371 | Returns: |
| 372 | A json explained here: |
| 373 | https://cloud.google.com/bigquery/docs/reference/rest/v2/tabledata/insertAll |
| 374 | """ |
| 375 | result = {} |
| 376 | for i in range(0, len(inserts), INSERT_BATCH_SIZE): |
| 377 | response = self._insert_batch( |
| 378 | inserts[i:min(len(inserts), i + INSERT_BATCH_SIZE)]) |
| 379 | |
| 380 | # There is also quota for the total number of requests / rows per second. |
| 381 | time.sleep(1) |
| 382 | |
| 383 | if not result: |
| 384 | # Use result from the first batch, appending errors from the rest. |
| 385 | result = response |
| 386 | continue |
| 387 | |
| 388 | # If there are new errors from the current batch, append to the result. |
| 389 | new_errors = response.get('insertErrors') |
| 390 | if not new_errors: |
| 391 | continue |
| 392 | |
| 393 | # Apparently result may not have errors, use |setdefault| to be careful. |
| 394 | result.setdefault('insertErrors', []).extend(new_errors) |
| 395 | |
| 396 | return result |