1

I have used the following code to build an app engine project to move data from google cloud bucket into the bigquery table

import argparse
import time
import uuid

from google.cloud import bigquery


def load_data_from_gcs(dataset_name, table_name, source):
    bigquery_client = bigquery.Client()
    dataset = bigquery_client.dataset(dataset_name)
    table = dataset.table(table_name)
    job_name = str(uuid.uuid4())

    job = bigquery_client.load_table_from_storage(
        job_name, table, source)

    job.begin()

    wait_for_job(job)

    print('Loaded {} rows into {}:{}.'.format(
        job.output_rows, dataset_name, table_name))


def wait_for_job(job):
    while True:
        job.reload()
        if job.state == 'DONE':
            if job.error_result:
                raise RuntimeError(job.error_result)
            return
        time.sleep(1)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('dataset_name')
    parser.add_argument('table_name')
    parser.add_argument(
        'source', help='The Google Cloud Storage object to load. Must be in '
        'the format gs://bucket_name/object_name')

    args = parser.parse_args()

    load_data_from_gcs(
        args.dataset_name,
        args.table_name,
        args.source)

I have also altered the default app.yaml file as the above file and deleted the webapp2 library entry and my app.yaml file looks like this

application: gcstobq
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: .*
  script: main.app

As I am new to python and app engine I dont know if I need to include the libraries defines in main.py file into the app.yaml and if i need to run this app using the command line tool.

Please let me know if I am missing something here?

Chet
  • 21,375
  • 10
  • 40
  • 58
LondonUK
  • 437
  • 1
  • 8
  • 20
  • i have not uploaded the application to the cloud yet but when I run it on my localhost i get the following in log file but nothing happens in terms of no data has been sent to my big query table ImportError: No module named cloud – LondonUK Nov 04 '16 at 11:31
  • Have you installed the Google python libs? i.e. pip install --upgrade google-cloud-bigquery – Graham Polley Nov 04 '16 at 11:35
  • I have not installed anything apart from google cloud sdk and app engine, do you think this error is related to these missing libraries. – LondonUK Nov 04 '16 at 11:52
  • I think so. Please try it. – Graham Polley Nov 06 '16 at 22:16
  • I have now installed the google python library but when i run the file it is giving me the following error - from google.cloud import bigquery ImportError: No module named cloud – LondonUK Nov 07 '16 at 14:52
  • i have uploaded the whole project to my app engine instance but when i click on the service it gives a server error and no data gets imported into the target table – LondonUK Nov 07 '16 at 15:21
  • What pip version are you using? What Python version are you using? Sounds like you may have more than one Python version installed. – Graham Polley Nov 08 '16 at 13:24
  • i am using python 2.7 and it is installed on windows 7 under the google sdk as bundled python which was a part of sdk installation however i do have another version of python 2.7 installed. – LondonUK Nov 09 '16 at 06:06
  • I have now managed to run the above code by first installing pip and then reinstalling google-cloud-client library however this code is still not running as an application and now giving me oauthclient import error. I have only managed to run the main.py file. – LondonUK Nov 16 '16 at 21:31

1 Answers1

1

Google Cloud uses the new Python namespace format (if you look at the source you'll notice that there's no __init__.py in the directory structure). This was changed in Python 3.3 with PEP-420

Fortunately in Python 2.7 you can fix this easily by avoiding implicit imports. Just add this to the very top of your file (before any other imports) to get the Python 3 behavior:

from __future__ import absolute_import

Chet
  • 21,375
  • 10
  • 40
  • 58