Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c6f0f38

Browse filesBrowse files
author
chenyumic
authored
Added sample for using Cloud Spanner w/ Cloud Functions (GoogleCloudPlatform#1681)
1 parent a34f991 commit c6f0f38
Copy full SHA for c6f0f38

File tree

Expand file treeCollapse file tree

4 files changed

+82
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+82
-0
lines changed
Open diff view settings
Collapse file

‎functions/spanner/README.md‎

Copy file name to clipboard
+11Lines changed: 11 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Google Cloud Functions - Cloud Spanner sample
4+
5+
See:
6+
7+
* [Cloud Functions Cloud Spanner tutorial][tutorial]
8+
* [Cloud Functions Cloud Spanner source code][code]
9+
10+
[tutorial]: https://cloud.google.com/spanner/docs/use-cloud-functions
11+
[code]: main.py
Collapse file

‎functions/spanner/main.py‎

Copy file name to clipboard
+39Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START spanner_functions_quickstart]
16+
from google.cloud import spanner
17+
18+
instance_id = 'test-instance'
19+
database_id = 'example-db'
20+
21+
client = spanner.Client()
22+
23+
24+
def spanner_read_data(request):
25+
instance = client.instance(instance_id)
26+
database = instance.database(database_id)
27+
28+
query = 'SELECT * FROM Albums'
29+
30+
outputs = []
31+
with database.snapshot() as snapshot:
32+
results = snapshot.execute_sql(query)
33+
34+
for row in results:
35+
output = 'SingerId: {}, AlbumId: {}, AlbumTitle: {}'.format(*row)
36+
outputs.append(output)
37+
38+
return '\n'.join(outputs)
39+
# [END spanner_functions_quickstart]
Collapse file

‎functions/spanner/main_test.py‎

Copy file name to clipboard
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the 'License');
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an 'AS IS' BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from unittest.mock import MagicMock
16+
17+
from google.cloud import spanner
18+
19+
spanner.Client = MagicMock()
20+
21+
22+
def test_main():
23+
import main
24+
snapshot_mock = main.client.instance().database().snapshot().__enter__()
25+
snapshot_mock.execute_sql.return_value = [('SingerID', 'AlbumID', 'Album')]
26+
27+
response = main.spanner_read_data(None)
28+
29+
assert 'SingerID' in response
30+
assert 'AlbumID' in response
31+
assert 'Album' in response
Collapse file

‎functions/spanner/requirements.txt‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-spanner==1.4.0

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.