|
| 1 | +# Copyright 2015, Google, Inc. |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# |
| 14 | +import json |
| 15 | +import httplib2 |
| 16 | +from samples.utils import get_service, poll_job |
| 17 | +from oauth2client.client import GoogleCredentials |
| 18 | + |
| 19 | + |
| 20 | +# [START make_post] |
| 21 | +def make_post(http, schema, data, projectId, datasetId, tableId): |
| 22 | + url = ('https://www.googleapis.com/upload/bigquery/v2/projects/' + |
| 23 | + projectId + '/jobs') |
| 24 | + # Create the body of the request, separated by a boundary of xxx |
| 25 | + resource = ('--xxx\n' + |
| 26 | + 'Content-Type: application/json; charset=UTF-8\n' + '\n' + |
| 27 | + '{\n' + |
| 28 | + ' "configuration": {\n' + |
| 29 | + ' "load": {\n' + |
| 30 | + ' "schema": {\n' |
| 31 | + ' "fields": ' + str(schema) + '\n' + |
| 32 | + ' },\n' + |
| 33 | + ' "destinationTable": {\n' + |
| 34 | + ' "projectId": "' + projectId + '",\n' + |
| 35 | + ' "datasetId": "' + datasetId + '",\n' + |
| 36 | + ' "tableId": "' + tableId + '"\n' + |
| 37 | + ' }\n' + |
| 38 | + ' }\n' + |
| 39 | + ' }\n' + |
| 40 | + '}\n' + |
| 41 | + '--xxx\n' + |
| 42 | + 'Content-Type: application/octet-stream\n' + |
| 43 | + '\n') |
| 44 | + # Append data to the request body |
| 45 | + resource += data |
| 46 | + |
| 47 | + # Signify the end of the body |
| 48 | + resource += ('--xxx--\n') |
| 49 | + |
| 50 | + headers = {'Content-Type': 'multipart/related; boundary=xxx'} |
| 51 | + |
| 52 | + return http.request(url, |
| 53 | + method='POST', |
| 54 | + body=resource, |
| 55 | + headers=headers) |
| 56 | + # [END make_post] |
| 57 | + |
| 58 | + |
| 59 | +# [START main] |
| 60 | +def main(): |
| 61 | + credentials = GoogleCredentials.get_application_default() |
| 62 | + http = credentials.authorize(httplib2.Http()) |
| 63 | + projectId = raw_input('Enter the project ID: ') |
| 64 | + datasetId = raw_input('Enter a dataset ID: ') |
| 65 | + tableId = raw_input('Enter a table name to load the data to: ') |
| 66 | + schema_path = raw_input( |
| 67 | + 'Enter the path to the schema file for the table: ') |
| 68 | + |
| 69 | + with open(schema_path, 'r') as schema_file: |
| 70 | + schema = schema_file.read() |
| 71 | + |
| 72 | + data_path = raw_input('Enter the path to the data file: ') |
| 73 | + |
| 74 | + with open(data_path, 'r') as data_file: |
| 75 | + data = data_file.read() |
| 76 | + |
| 77 | + resp, content = make_post(http, |
| 78 | + schema, |
| 79 | + data, |
| 80 | + projectId, |
| 81 | + datasetId, |
| 82 | + tableId) |
| 83 | + |
| 84 | + if resp.status == 200: |
| 85 | + job_resource = json.loads(content) |
| 86 | + service = get_service(credentials) |
| 87 | + poll_job(service, **job_resource['jobReference']) |
| 88 | + print("Success!") |
| 89 | + else: |
| 90 | + print("Http error code: {}".format(resp.status)) |
| 91 | +# [END main] |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + main() |
0 commit comments