|
| 1 | +# Copyright 2018 Google Inc. All Rights Reserved. |
| 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 | +"""This application demonstrates how to do batch operations using Cloud |
| 16 | +Spanner. |
| 17 | +
|
| 18 | +For more information, see the README.rst under /spanner. |
| 19 | +""" |
| 20 | + |
| 21 | +import argparse |
| 22 | +import concurrent.futures |
| 23 | +import time |
| 24 | + |
| 25 | +from google.cloud import spanner |
| 26 | + |
| 27 | + |
| 28 | +# [START spanner_batch_client] |
| 29 | +def run_batch_query(instance_id, database_id): |
| 30 | + """Runs an example batch query.""" |
| 31 | + |
| 32 | + # Expected Table Format: |
| 33 | + # CREATE TABLE Singers ( |
| 34 | + # SingerId INT64 NOT NULL, |
| 35 | + # FirstName STRING(1024), |
| 36 | + # LastName STRING(1024), |
| 37 | + # SingerInfo BYTES(MAX), |
| 38 | + # ) PRIMARY KEY (SingerId); |
| 39 | + |
| 40 | + spanner_client = spanner.Client() |
| 41 | + instance = spanner_client.instance(instance_id) |
| 42 | + database = instance.database(database_id) |
| 43 | + |
| 44 | + # Create the batch transaction and generate partitions |
| 45 | + snapshot = database.batch_snapshot() |
| 46 | + partitions = snapshot.generate_read_batches( |
| 47 | + table='Singers', |
| 48 | + columns=('SingerId', 'FirstName', 'LastName',), |
| 49 | + keyset=spanner.KeySet(all_=True) |
| 50 | + ) |
| 51 | + |
| 52 | + # Create a pool of workers for the tasks |
| 53 | + start = time.time() |
| 54 | + with concurrent.futures.ThreadPoolExecutor() as executor: |
| 55 | + futures = [executor.submit(process, snapshot, p) for p in partitions] |
| 56 | + |
| 57 | + for future in concurrent.futures.as_completed(futures, timeout=3600): |
| 58 | + finish, row_ct = future.result() |
| 59 | + elapsed = finish - start |
| 60 | + print(u'Completed {} rows in {} seconds'.format(row_ct, elapsed)) |
| 61 | + |
| 62 | + # Clean up |
| 63 | + snapshot.close() |
| 64 | + |
| 65 | + |
| 66 | +def process(snapshot, partition): |
| 67 | + """Processes the requests of a query in an separate process.""" |
| 68 | + print('Started processing partition.') |
| 69 | + row_ct = 0 |
| 70 | + for row in snapshot.process_read_batch(partition): |
| 71 | + print(u'SingerId: {}, AlbumId: {}, AlbumTitle: {}'.format(*row)) |
| 72 | + row_ct += 1 |
| 73 | + return time.time(), row_ct |
| 74 | +# [END spanner_batch_client] |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + parser = argparse.ArgumentParser( |
| 79 | + description=__doc__, |
| 80 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
| 81 | + parser.add_argument( |
| 82 | + 'instance_id', help='Your Cloud Spanner instance ID.') |
| 83 | + parser.add_argument( |
| 84 | + 'database_id', help='Your Cloud Spanner database ID.', |
| 85 | + default='example_db') |
| 86 | + |
| 87 | + args = parser.parse_args() |
| 88 | + |
| 89 | + run_batch_query(args.instance_id, args.database_id) |
0 commit comments