I'm loading data from GCS according to this 
article https://cloud.google.com/bigquery/loading-data-into-bigquery

The load job is taking extremely long to finish the task or throwing this 
error "Unexpected. Please try again".

Please help!

Here is my code:

def authorize():
  credentials = appengine.AppAssertionCredentials(
    scope="https://www.googleapis.com/auth/bigquery";)
  http = credentials.authorize(httplib2.Http())
  return discovery.build("bigquery", "v2", http=http)

def table_id(date, table_prefix=None):
  if not table_prefix:
    table_prefix = os.environ["BIGQUERY_TABLE_PREFIX"]
  result = [
    table_prefix,
    str(date.year),
    str(date.month).zfill(2),
    str(date.day).zfill(2)]
  return "".join(result)

def wait(job, job_id):
  # Timeout exceeded: keep polling until the job is complete.
  while True:
    response = job.get(
      projectId=os.environ["BIGQUERY_PROJECT_ID"],
      jobId=job_id).execute()
    if "errorResult" in response["status"]:
      return response
    elif "DONE" == response["status"]["state"]:
      return response
    time.sleep(10)

def load_table(date, file_uri):
  body = {
    "projectId": os.environ["BIGQUERY_PROJECT_ID"],
    "configuration": {
        "load": {
          "sourceUris": [file_uri],
          "schema": {
            "fields": [
              {
                "name": "logged_at",
                "type": "TIMESTAMP"},
              {
                "name": "publisher_id",
                "type": "INTEGER"},
              {
                "name": "video_id",
                "type": "INTEGER"},
              {
                "name": "sc_bytes",
                "type": "INTEGER"}]},
          "destinationTable": {
            "projectId": os.environ["BIGQUERY_PROJECT_ID"],
            "datasetId": os.environ["BIGQUERY_DATASET_ID"],
            "tableId": table_id(date)},
          "skipLeadingRows": 1}}}

  bg_service = authorize()
  job = bg_service.jobs()
  response = job.insert(
    projectId=os.environ["BIGQUERY_PROJECT_ID"],
    body=body).execute()
  return wait(job, response["jobReference"]["jobId"])

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-appengine.
For more options, visit https://groups.google.com/d/optout.

Reply via email to