|
31 | 31 | from google.cloud import spanner |
32 | 32 | from google.cloud.spanner_admin_instance_v1.types import spanner_instance_admin |
33 | 33 | from google.cloud.spanner_v1 import param_types |
| 34 | +from google.cloud.spanner_v1 import DirectedReadOptions |
34 | 35 | from google.type import expr_pb2 |
35 | 36 | from google.iam.v1 import policy_pb2 |
36 | 37 | from google.cloud.spanner_v1.data_types import JsonObject |
@@ -2723,6 +2724,78 @@ def drop_sequence(instance_id, database_id): |
2723 | 2724 |
|
2724 | 2725 | # [END spanner_drop_sequence] |
2725 | 2726 |
|
| 2727 | + |
| 2728 | +def directed_read_options( |
| 2729 | + instance_id, |
| 2730 | + database_id, |
| 2731 | +): |
| 2732 | + """ |
| 2733 | + Shows how to run an execute sql request with directed read options. |
| 2734 | + Only one of exclude_replicas or include_replicas can be set |
| 2735 | + Each accepts a list of replicaSelections which contains location and type |
| 2736 | + * `location` - The location must be one of the regions within the |
| 2737 | + multi-region configuration of your database. |
| 2738 | + * `type_` - The type of the replica |
| 2739 | + Some examples of using replica_selectors are: |
| 2740 | + * `location:us-east1` --> The "us-east1" replica(s) of any available type |
| 2741 | + will be used to process the request. |
| 2742 | + * `type:READ_ONLY` --> The "READ_ONLY" type replica(s) in nearest |
| 2743 | + available location will be used to process the |
| 2744 | + request. |
| 2745 | + * `location:us-east1 type:READ_ONLY` --> The "READ_ONLY" type replica(s) |
| 2746 | + in location "us-east1" will be used to process |
| 2747 | + the request. |
| 2748 | + include_replicas also contains an option for auto_failover_disabled which when set |
| 2749 | + Spanner will not route requests to a replica outside the |
| 2750 | + include_replicas list when all the specified replicas are unavailable |
| 2751 | + or unhealthy. The default value is `false` |
| 2752 | + """ |
| 2753 | + # [START spanner_directed_read] |
| 2754 | + # instance_id = "your-spanner-instance" |
| 2755 | + # database_id = "your-spanner-db-id" |
| 2756 | + |
| 2757 | + directed_read_options_for_client = { |
| 2758 | + "exclude_replicas": { |
| 2759 | + "replica_selections": [ |
| 2760 | + { |
| 2761 | + "location": "us-east4", |
| 2762 | + }, |
| 2763 | + ], |
| 2764 | + }, |
| 2765 | + } |
| 2766 | + |
| 2767 | + # directed_read_options can be set at client level and will be used in all |
| 2768 | + # read-only transaction requests |
| 2769 | + spanner_client = spanner.Client( |
| 2770 | + directed_read_options=directed_read_options_for_client |
| 2771 | + ) |
| 2772 | + instance = spanner_client.instance(instance_id) |
| 2773 | + database = instance.database(database_id) |
| 2774 | + |
| 2775 | + directed_read_options_for_request = { |
| 2776 | + "include_replicas": { |
| 2777 | + "replica_selections": [ |
| 2778 | + { |
| 2779 | + "type_": DirectedReadOptions.ReplicaSelection.Type.READ_ONLY, |
| 2780 | + }, |
| 2781 | + ], |
| 2782 | + "auto_failover_disabled": True, |
| 2783 | + }, |
| 2784 | + } |
| 2785 | + |
| 2786 | + with database.snapshot() as snapshot: |
| 2787 | + # Read rows while passing directed_read_options directly to the query. |
| 2788 | + # These will override the options passed at Client level. |
| 2789 | + results = snapshot.execute_sql( |
| 2790 | + "SELECT SingerId, AlbumId, AlbumTitle FROM Albums", |
| 2791 | + directed_read_options=directed_read_options_for_request, |
| 2792 | + ) |
| 2793 | + |
| 2794 | + for row in results: |
| 2795 | + print("SingerId: {}, AlbumId: {}, AlbumTitle: {}".format(*row)) |
| 2796 | + # [END spanner_directed_read] |
| 2797 | + |
| 2798 | + |
2726 | 2799 | if __name__ == "__main__": # noqa: C901 |
2727 | 2800 | parser = argparse.ArgumentParser( |
2728 | 2801 | description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
@@ -2862,6 +2935,7 @@ def drop_sequence(instance_id, database_id): |
2862 | 2935 | "--database_role", default="new_parent" |
2863 | 2936 | ) |
2864 | 2937 | enable_fine_grained_access_parser.add_argument("--title", default="condition title") |
| 2938 | + subparsers.add_parser("directed_read_options", help=directed_read_options.__doc__) |
2865 | 2939 |
|
2866 | 2940 | args = parser.parse_args() |
2867 | 2941 |
|
@@ -2993,3 +3067,5 @@ def drop_sequence(instance_id, database_id): |
2993 | 3067 | args.database_role, |
2994 | 3068 | args.title, |
2995 | 3069 | ) |
| 3070 | + elif args.command == "directed_read_options": |
| 3071 | + directed_read_options(args.instance_id, args.database_id) |
0 commit comments