1
1
import calendar
2
+ import json
3
+ import logging
2
4
from collections import defaultdict
3
5
from datetime import datetime , timedelta
4
- from time import sleep
5
- from time import time
6
6
from hashlib import sha256
7
- import json
8
- import logging
7
+ from time import sleep , time
9
8
9
+ import httplib2
10
+ import six
10
11
from apiclient .discovery import build
11
12
from apiclient .errors import HttpError
12
- import httplib2
13
13
14
+ from bigquery .errors import (BigQueryTimeoutException , JobExecutingException ,
15
+ JobInsertException , UnfinishedQueryException )
14
16
from bigquery .schema_builder import schema_from_record
15
- from bigquery .errors import (
16
- JobExecutingException , JobInsertException ,
17
- UnfinishedQueryException , BigQueryTimeoutException
18
- )
19
17
20
18
BIGQUERY_SCOPE = 'https://www.googleapis.com/auth/bigquery'
21
19
BIGQUERY_SCOPE_READ_ONLY = 'https://www.googleapis.com/auth/bigquery.readonly'
@@ -154,7 +152,7 @@ def _submit_query_job(self, query_data):
154
152
projectId = self .project_id , body = query_data ).execute ()
155
153
except HttpError as e :
156
154
if query_data .get ("dryRun" , False ):
157
- return None , json .loads (e .content )
155
+ return None , json .loads (e .content . decode ( 'utf8' ) )
158
156
raise
159
157
160
158
job_id = query_reply ['jobReference' ].get ('jobId' )
@@ -266,7 +264,7 @@ def get_table_schema(self, dataset, table):
266
264
projectId = self .project_id ,
267
265
tableId = table ,
268
266
datasetId = dataset ).execute ()
269
- except HttpError , e :
267
+ except HttpError as e :
270
268
if int (e .resp ['status' ]) == 404 :
271
269
logging .warn ('Table %s.%s does not exist' , dataset , table )
272
270
return None
@@ -651,7 +649,7 @@ def import_data_from_uris(
651
649
skip_leading_rows = skip_leading_rows ,
652
650
quote = quote )
653
651
non_null_values = dict ((k , v ) for k , v
654
- in all_values .items ()
652
+ in list ( all_values .items () )
655
653
if v )
656
654
raise Exception ("Parameters field_delimiter, allow_jagged_rows, "
657
655
"allow_quoted_newlines, quote and "
@@ -837,6 +835,7 @@ def wait_for_job(self, job, interval=5, timeout=60):
837
835
Waits until the job indicated by job_resource is done or has failed
838
836
Args:
839
837
job: dict, representing a BigQuery job resource
838
+ or str, representing a BigQuery job id
840
839
interval: optional float polling interval in seconds, default = 5
841
840
timeout: optional float timeout in seconds, default = 60
842
841
Returns:
@@ -848,7 +847,9 @@ def wait_for_job(self, job, interval=5, timeout=60):
848
847
BigQueryTimeoutException on timeout
849
848
"""
850
849
complete = False
851
- job_id = job ['jobReference' ]['jobId' ]
850
+ job_id = str (job if isinstance (job ,
851
+ (six .binary_type , six .text_type , int ))
852
+ else job ['jobReference' ]['jobId' ])
852
853
job_resource = None
853
854
854
855
start_time = time ()
@@ -1048,7 +1049,7 @@ def _filter_tables_by_time(self, tables, start_time, end_time):
1048
1049
A list of table names that are inside the time range.
1049
1050
"""
1050
1051
1051
- return [table_name for (table_name , unix_seconds ) in tables .iteritems ()
1052
+ return [table_name for (table_name , unix_seconds ) in tables .items ()
1052
1053
if self ._in_range (start_time , end_time , unix_seconds )]
1053
1054
1054
1055
def _in_range (self , start_time , end_time , time ):
@@ -1167,7 +1168,7 @@ def _generate_hex_for_uris(self, uris):
1167
1168
Returns:
1168
1169
string of hexed uris
1169
1170
"""
1170
- return sha256 (":" .join (uris ) + str (time ())).hexdigest ()
1171
+ return sha256 (( ":" .join (uris ) + str (time ())). encode ( )).hexdigest ()
1171
1172
1172
1173
def _raise_insert_exception_if_error (self , job ):
1173
1174
error_http = job .get ('error' )
0 commit comments