Skip to content

Commit ab867fb

Browse files
author
Takashi Matsuo
committed
Made the test green.
1 parent a610842 commit ab867fb

23 files changed

+334
-273
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
.tox
55
coverage.xml
66
nosetests.xml
7+
python-docs-samples.json

bigquery/__init__.py

Whitespace-only changes.

bigquery/samples/__init__.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +0,0 @@
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-
#

bigquery/samples/async_query.py

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
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
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
1212
# limitations under the License.
1313
#
1414
from __future__ import print_function # For python 2/3 interoperability
15-
from samples.utils import get_service, paging, poll_job
16-
import uuid
15+
1716
import json
17+
import uuid
18+
19+
from bigquery.samples.utils import get_service
20+
from bigquery.samples.utils import paging
21+
from bigquery.samples.utils import poll_job
1822

1923

2024
# [START async_query]
@@ -23,19 +27,19 @@ def async_query(service, project_id, query, batch=False, num_retries=5):
2327
# don't accidentally duplicate query
2428
job_data = {
2529
'jobReference': {
26-
'projectId': project_id,
27-
'job_id': str(uuid.uuid4())
28-
},
30+
'projectId': project_id,
31+
'job_id': str(uuid.uuid4())
32+
},
2933
'configuration': {
30-
'query': {
31-
'query': query,
32-
'priority': 'BATCH' if batch else 'INTERACTIVE',
33-
},
34-
}
34+
'query': {
35+
'query': query,
36+
'priority': 'BATCH' if batch else 'INTERACTIVE'
37+
}
3538
}
39+
}
3640
return service.jobs().insert(
37-
projectId=project_id,
38-
body=job_data).execute(num_retries=num_retries)
41+
projectId=project_id,
42+
body=job_data).execute(num_retries=num_retries)
3943
# [END async_query]
4044

4145

@@ -55,7 +59,6 @@ def run(project_id, query_string, batch, num_retries, interval):
5559
interval,
5660
num_retries)
5761

58-
5962
for page in paging(service,
6063
service.jobs().getQueryResults,
6164
num_retries=num_retries,
@@ -69,18 +72,13 @@ def run(project_id, query_string, batch, num_retries, interval):
6972
def main():
7073
project_id = raw_input("Enter the project ID: ")
7174
query_string = raw_input("Enter the Bigquery SQL Query: ")
72-
batch = raw_input("Run query as batch (y/n)?: ") in ('True',
73-
'true',
74-
'y',
75-
'Y',
76-
'yes',
77-
'Yes')
78-
75+
batch = raw_input("Run query as batch (y/n)?: ") in (
76+
'True', 'true', 'y', 'Y', 'yes', 'Yes')
7977

8078
num_retries = raw_input(
81-
"Enter number of times to retry in case of 500 error: ")
79+
"Enter number of times to retry in case of 500 error: ")
8280
interval = raw_input(
83-
"Enter how often to poll the query for completion (seconds): ")
81+
"Enter how often to poll the query for completion (seconds): ")
8482

8583
for result in run(project_id, query_string, batch, num_retries, interval):
8684
print(result)

bigquery/samples/discovery_doc.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
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
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
1212
# limitations under the License.
1313
#
14-
import os
14+
"""
15+
A module that takes care of caching and updating discovery docs for
16+
google-api-python-clients (until such a feature is integrated).
17+
"""
18+
1519
import json
16-
import httplib2
20+
import os
1721
import time
1822

19-
# [START build_and_update]
23+
import httplib2
2024

21-
RESOURCE_PATH='..' #look for discovery docs in the parent folder
22-
MAX_AGE = 86400 #update discovery docs older than a day
25+
# [START build_and_update]
2326

24-
# A module that takes care of caching and updating discovery docs
25-
# for google-api-python-clients (until such a feature is integrated)
27+
RESOURCE_PATH = '..' # look for discovery docs in the parent folder
28+
MAX_AGE = 86400 # update discovery docs older than a day
29+
BIGQUERY_SCOPES = ['https://www.googleapis.com/auth/bigquery']
2630

2731

2832
def build_and_update(api, version):
2933
from oauth2client.client import GoogleCredentials
3034
from googleapiclient.discovery import build_from_document
3135

32-
3336
path = os.path.join(RESOURCE_PATH, '{}.{}'.format(api, version))
3437
try:
3538
age = time.time() - os.path.getmtime(path)
@@ -38,11 +41,14 @@ def build_and_update(api, version):
3841
except os.error:
3942
_update_discovery_doc(api, version, path)
4043

44+
credentials = GoogleCredentials.get_application_default()
45+
if credentials.create_scoped_required():
46+
credentials = credentials.create_scoped(BIGQUERY_SCOPES)
4147
with open(path, 'r') as discovery_doc:
4248
return build_from_document(discovery_doc.read(),
43-
http=httplib2.Http(),
44-
credentials=GoogleCredentials
45-
.get_application_default())
49+
http=httplib2.Http(),
50+
credentials=credentials)
51+
4652

4753
def _update_discovery_doc(api, version, path):
4854
from apiclient.discovery import DISCOVERY_URI
@@ -61,5 +67,5 @@ def _update_discovery_doc(api, version, path):
6167
json.dump(discovery_json, discovery_doc)
6268
except ValueError:
6369
raise InvalidJsonError(
64-
'Bad JSON: %s from %s.' % (content, requested_url))
70+
'Bad JSON: %s from %s.' % (content, requested_url))
6571
# [END build_and_update]

bigquery/samples/export_data_to_cloud_storage.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1-
from samples.utils import get_service, poll_job
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+
#
214
import uuid
315

16+
from bigquery.samples.utils import get_service
17+
from bigquery.samples.utils import poll_job
18+
419

520
# [START export_table]
621
def export_table(service, cloud_storage_path,
@@ -9,21 +24,21 @@ def export_table(service, cloud_storage_path,
924
# Generate a unique job_id so retries
1025
# don't accidentally duplicate export
1126
job_data = {
12-
'jobReference': {
27+
'jobReference': {
28+
'projectId': projectId,
29+
'jobId': str(uuid.uuid4())
30+
},
31+
'configuration': {
32+
'extract': {
33+
'sourceTable': {
1334
'projectId': projectId,
14-
'jobId': str(uuid.uuid4())
15-
},
16-
'configuration': {
17-
'extract': {
18-
'sourceTable': {
19-
'projectId': projectId,
20-
'datasetId': datasetId,
21-
'tableId': tableId,
22-
},
23-
'destinationUris': [cloud_storage_path],
24-
}
25-
}
35+
'datasetId': datasetId,
36+
'tableId': tableId,
37+
},
38+
'destinationUris': [cloud_storage_path],
2639
}
40+
}
41+
}
2742
return service.jobs().insert(
2843
projectId=projectId,
2944
body=job_data).execute(num_retries=num_retries)
@@ -52,11 +67,11 @@ def main():
5267
datasetId = raw_input("Enter a dataset ID: ")
5368
tableId = raw_input("Enter a table name to copy: ")
5469
cloud_storage_path = raw_input(
55-
"Enter a Google Cloud Storage URI: ")
70+
"Enter a Google Cloud Storage URI: ")
5671
interval = raw_input(
57-
"Enter how often to poll the job (in seconds): ")
72+
"Enter how often to poll the job (in seconds): ")
5873
num_retries = raw_input(
59-
"Enter the number of retries in case of 500 error: ")
74+
"Enter the number of retries in case of 500 error: ")
6075

6176
run(cloud_storage_path,
6277
projectId, datasetId, tableId,

bigquery/samples/load_data_by_post.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
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
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
1212
# limitations under the License.
1313
#
1414
import json
15+
16+
from bigquery.samples.utils import get_service, poll_job
17+
1518
import httplib2
16-
from samples.utils import get_service, poll_job
19+
1720
from oauth2client.client import GoogleCredentials
1821

1922

@@ -64,7 +67,7 @@ def main():
6467
datasetId = raw_input('Enter a dataset ID: ')
6568
tableId = raw_input('Enter a table name to load the data to: ')
6669
schema_path = raw_input(
67-
'Enter the path to the schema file for the table: ')
70+
'Enter the path to the schema file for the table: ')
6871

6972
with open(schema_path, 'r') as schema_file:
7073
schema = schema_file.read()

0 commit comments

Comments
 (0)