|
24 | 24 | import base64
|
25 | 25 | import datetime
|
26 | 26 | import decimal
|
| 27 | +import logging |
27 | 28 |
|
28 | 29 | from google.cloud import spanner
|
29 | 30 | from google.cloud.spanner_v1 import param_types
|
@@ -969,6 +970,44 @@ def insert_singers(transaction):
|
969 | 970 | # [END spanner_dml_standard_insert]
|
970 | 971 |
|
971 | 972 |
|
| 973 | +# [START spanner_get_commit_stats] |
| 974 | +def log_commit_stats(instance_id, database_id): |
| 975 | + """Inserts sample data using DML and displays the commit statistics. """ |
| 976 | + # By default, commit statistics are logged via stdout at level Info. |
| 977 | + # This sample uses a custom logger to access the commit statistics. |
| 978 | + class CommitStatsSampleLogger(logging.Logger): |
| 979 | + def __init__(self): |
| 980 | + self.last_commit_stats = None |
| 981 | + super().__init__("commit_stats_sample") |
| 982 | + |
| 983 | + def info(self, msg, *args, **kwargs): |
| 984 | + if kwargs["extra"] and "commit_stats" in kwargs["extra"]: |
| 985 | + self.last_commit_stats = kwargs["extra"]["commit_stats"] |
| 986 | + super().info(msg) |
| 987 | + |
| 988 | + spanner_client = spanner.Client() |
| 989 | + instance = spanner_client.instance(instance_id) |
| 990 | + database = instance.database(database_id, logger=CommitStatsSampleLogger()) |
| 991 | + database.log_commit_stats = True |
| 992 | + |
| 993 | + def insert_singers(transaction): |
| 994 | + row_ct = transaction.execute_update( |
| 995 | + "INSERT Singers (SingerId, FirstName, LastName) " |
| 996 | + " VALUES (110, 'Virginia', 'Watson')" |
| 997 | + ) |
| 998 | + |
| 999 | + print("{} record(s) inserted.".format(row_ct)) |
| 1000 | + |
| 1001 | + database.run_in_transaction(insert_singers) |
| 1002 | + commit_stats = database.logger.last_commit_stats |
| 1003 | + print( |
| 1004 | + "{} mutation(s) in transaction.".format( |
| 1005 | + commit_stats.mutation_count |
| 1006 | + ) |
| 1007 | + ) |
| 1008 | +# [END spanner_get_commit_stats] |
| 1009 | + |
| 1010 | + |
972 | 1011 | def update_data_with_dml(instance_id, database_id):
|
973 | 1012 | """Updates sample data from the database using a DML statement. """
|
974 | 1013 | # [START spanner_dml_standard_update]
|
@@ -1710,6 +1749,7 @@ def create_client_with_query_options(instance_id, database_id):
|
1710 | 1749 | "query_nested_struct_field", help=query_nested_struct_field.__doc__
|
1711 | 1750 | )
|
1712 | 1751 | subparsers.add_parser("insert_data_with_dml", help=insert_data_with_dml.__doc__)
|
| 1752 | + subparsers.add_parser("log_commit_stats", help=log_commit_stats.__doc__) |
1713 | 1753 | subparsers.add_parser("update_data_with_dml", help=update_data_with_dml.__doc__)
|
1714 | 1754 | subparsers.add_parser("delete_data_with_dml", help=delete_data_with_dml.__doc__)
|
1715 | 1755 | subparsers.add_parser(
|
@@ -1820,6 +1860,8 @@ def create_client_with_query_options(instance_id, database_id):
|
1820 | 1860 | query_nested_struct_field(args.instance_id, args.database_id)
|
1821 | 1861 | elif args.command == "insert_data_with_dml":
|
1822 | 1862 | insert_data_with_dml(args.instance_id, args.database_id)
|
| 1863 | + elif args.command == "log_commit_stats": |
| 1864 | + log_commit_stats(args.instance_id, args.database_id) |
1823 | 1865 | elif args.command == "update_data_with_dml":
|
1824 | 1866 | update_data_with_dml(args.instance_id, args.database_id)
|
1825 | 1867 | elif args.command == "delete_data_with_dml":
|
|
0 commit comments