Skip to content

Commit df4b8eb

Browse files
evaogbetseaver
authored andcommitted
Add bigquery jobid to table (#3605)
1 parent 59fd1e4 commit df4b8eb

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

bigquery/google/cloud/bigquery/table.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,8 @@ def upload_from_file(self,
842842
quote_character=None,
843843
skip_leading_rows=None,
844844
write_disposition=None,
845-
client=None):
845+
client=None,
846+
job_name=None):
846847
"""Upload the contents of this table from a file-like object.
847848
848849
The content type of the upload will either be
@@ -915,6 +916,10 @@ def upload_from_file(self,
915916
:param client: Optional. The client to use. If not passed, falls back
916917
to the ``client`` stored on the current dataset.
917918
919+
:type job_name: str
920+
:param job_name: Optional. The id of the job. Generated if not
921+
explicitly passed in.
922+
918923
:rtype: :class:`google.cloud.bigquery.jobs.LoadTableFromStorageJob`
919924
:returns: the job instance used to load the data (e.g., for
920925
querying status). Note that the job is already started:
@@ -977,7 +982,7 @@ def upload_from_file(self,
977982
encoding, field_delimiter,
978983
ignore_unknown_values, max_bad_records,
979984
quote_character, skip_leading_rows,
980-
write_disposition)
985+
write_disposition, job_name)
981986

982987
upload = Upload(file_obj, content_type, total_bytes,
983988
auto_transfer=False)
@@ -1033,7 +1038,8 @@ def _configure_job_metadata(metadata, # pylint: disable=too-many-arguments
10331038
max_bad_records,
10341039
quote_character,
10351040
skip_leading_rows,
1036-
write_disposition):
1041+
write_disposition,
1042+
job_name):
10371043
"""Helper for :meth:`Table.upload_from_file`."""
10381044
load_config = metadata['configuration']['load']
10391045

@@ -1067,6 +1073,9 @@ def _configure_job_metadata(metadata, # pylint: disable=too-many-arguments
10671073
if write_disposition is not None:
10681074
load_config['writeDisposition'] = write_disposition
10691075

1076+
if job_name is not None:
1077+
load_config['jobReference'] = {'jobId': job_name}
1078+
10701079

10711080
def _parse_schema_resource(info):
10721081
"""Parse a resource fragment into a schema field.

bigquery/tests/unit/test_table.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1844,6 +1844,22 @@ class _UploadConfig(object):
18441844
self.assertEqual(req['body'], BODY)
18451845
# pylint: enable=too-many-statements
18461846

1847+
def test_upload_from_file_w_jobid(self):
1848+
import json
1849+
from google.cloud._helpers import _to_bytes
1850+
1851+
requested, PATH, BODY = self._upload_from_file_helper(job_name='foo')
1852+
parse_chunk = _email_chunk_parser()
1853+
req = requested[0]
1854+
ctype, boundary = [x.strip()
1855+
for x in req['headers']['content-type'].split(';')]
1856+
divider = b'--' + _to_bytes(boundary[len('boundary="'):-1])
1857+
chunks = req['body'].split(divider)[1:-1] # discard prolog / epilog
1858+
text_msg = parse_chunk(chunks[0].strip())
1859+
metadata = json.loads(text_msg._payload)
1860+
load_config = metadata['configuration']['load']
1861+
self.assertEqual(load_config['jobReference'], {'jobId': 'foo'})
1862+
18471863

18481864
class Test_parse_schema_resource(unittest.TestCase, _SchemaBase):
18491865

0 commit comments

Comments
 (0)