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 1e5fad2

Browse filesBrowse files
author
Jon Wayne Parrott
authored
Add spanner stale data sample (GoogleCloudPlatform#1107)
1 parent cbce816 commit 1e5fad2
Copy full SHA for 1e5fad2

File tree

Expand file treeCollapse file tree

2 files changed

+35
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+35
-0
lines changed

‎spanner/cloud-client/snippets.py

Copy file name to clipboardExpand all lines: spanner/cloud-client/snippets.py
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,27 @@ def read_data(instance_id, database_id):
119119
print(u'SingerId: {}, AlbumId: {}, AlbumTitle: {}'.format(*row))
120120

121121

122+
def read_stale_data(instance_id, database_id):
123+
"""Reads sample data from the database. The data is exactly 10 seconds
124+
stale."""
125+
import datetime
126+
127+
spanner_client = spanner.Client()
128+
instance = spanner_client.instance(instance_id)
129+
database = instance.database(database_id)
130+
staleness = datetime.timedelta(seconds=10)
131+
132+
with database.snapshot(exact_staleness=staleness) as snapshot:
133+
keyset = spanner.KeySet(all_=True)
134+
results = snapshot.read(
135+
table='Albums',
136+
columns=('SingerId', 'AlbumId', 'AlbumTitle',),
137+
keyset=keyset)
138+
139+
for row in results:
140+
print(u'SingerId: {}, AlbumId: {}, AlbumTitle: {}'.format(*row))
141+
142+
122143
def query_data_with_new_column(instance_id, database_id):
123144
"""Queries sample data from the database using SQL.
124145
@@ -424,6 +445,7 @@ def read_only_transaction(instance_id, database_id):
424445
subparsers.add_parser('insert_data', help=insert_data.__doc__)
425446
subparsers.add_parser('query_data', help=query_data.__doc__)
426447
subparsers.add_parser('read_data', help=read_data.__doc__)
448+
subparsers.add_parser('read_stale_data', help=read_stale_data.__doc__)
427449
subparsers.add_parser('add_column', help=add_column.__doc__)
428450
subparsers.add_parser('update_data', help=update_data.__doc__)
429451
subparsers.add_parser(
@@ -454,6 +476,8 @@ def read_only_transaction(instance_id, database_id):
454476
query_data(args.instance_id, args.database_id)
455477
elif args.command == 'read_data':
456478
read_data(args.instance_id, args.database_id)
479+
elif args.command == 'read_stale_data':
480+
read_stale_data(args.instance_id, args.database_id)
457481
elif args.command == 'add_column':
458482
add_column(args.instance_id, args.database_id)
459483
elif args.command == 'update_data':

‎spanner/cloud-client/snippets_test.py

Copy file name to clipboardExpand all lines: spanner/cloud-client/snippets_test.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ def test_read_data(temporary_database, capsys):
7373
assert 'Total Junk' in out
7474

7575

76+
def test_read_stale_data(temporary_database, capsys):
77+
snippets.read_stale_data(SPANNER_INSTANCE, temporary_database.database_id)
78+
79+
out, _ = capsys.readouterr()
80+
81+
# It shouldn't be in the output because it was *just* inserted by the
82+
# temporary database fixture and this sample reads 10 seconds into the
83+
# past.
84+
assert 'Total Junk' not in out
85+
86+
7687
@pytest.fixture(scope='module')
7788
def temporary_database_with_column(temporary_database):
7889
snippets.add_column(SPANNER_INSTANCE, temporary_database.database_id)

0 commit comments

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