Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spanner stale data sample #1107

Merged
merged 1 commit into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions spanner/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,27 @@ def read_data(instance_id, database_id):
print(u'SingerId: {}, AlbumId: {}, AlbumTitle: {}'.format(*row))


def read_stale_data(instance_id, database_id):
"""Reads sample data from the database. The data is exactly 10 seconds
stale."""
import datetime

spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)
staleness = datetime.timedelta(seconds=10)

with database.snapshot(exact_staleness=staleness) as snapshot:
keyset = spanner.KeySet(all_=True)
results = snapshot.read(
table='Albums',
columns=('SingerId', 'AlbumId', 'AlbumTitle',),
keyset=keyset)

for row in results:
print(u'SingerId: {}, AlbumId: {}, AlbumTitle: {}'.format(*row))


def query_data_with_new_column(instance_id, database_id):
"""Queries sample data from the database using SQL.

Expand Down Expand Up @@ -424,6 +445,7 @@ def read_only_transaction(instance_id, database_id):
subparsers.add_parser('insert_data', help=insert_data.__doc__)
subparsers.add_parser('query_data', help=query_data.__doc__)
subparsers.add_parser('read_data', help=read_data.__doc__)
subparsers.add_parser('read_stale_data', help=read_stale_data.__doc__)
subparsers.add_parser('add_column', help=add_column.__doc__)
subparsers.add_parser('update_data', help=update_data.__doc__)
subparsers.add_parser(
Expand Down Expand Up @@ -454,6 +476,8 @@ def read_only_transaction(instance_id, database_id):
query_data(args.instance_id, args.database_id)
elif args.command == 'read_data':
read_data(args.instance_id, args.database_id)
elif args.command == 'read_stale_data':
read_stale_data(args.instance_id, args.database_id)
elif args.command == 'add_column':
add_column(args.instance_id, args.database_id)
elif args.command == 'update_data':
Expand Down
11 changes: 11 additions & 0 deletions spanner/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ def test_read_data(temporary_database, capsys):
assert 'Total Junk' in out


def test_read_stale_data(temporary_database, capsys):
snippets.read_stale_data(SPANNER_INSTANCE, temporary_database.database_id)

out, _ = capsys.readouterr()

# It shouldn't be in the output because it was *just* inserted by the
# temporary database fixture and this sample reads 10 seconds into the
# past.
assert 'Total Junk' not in out


@pytest.fixture(scope='module')
def temporary_database_with_column(temporary_database):
snippets.add_column(SPANNER_INSTANCE, temporary_database.database_id)
Expand Down