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 ca76108

Browse filesBrowse files
feat: add sample for pre-split feature (#1333)
* feat: add sample for pre-split feature * build error fixes * build failure fixes * build fixes * lint fixes * fixes lint * fixed the build error * fixed the build error * chore: fix positional argument issue Signed-off-by: Sri Harsha CH <sriharshach@google.com> * fixed the index test case * added comment on the splits for idex keys * fixed indent * lint fixes * lint fixes * chore: tests fix Signed-off-by: Sri Harsha CH <sriharshach@google.com> * chore: update sample to not change editions due to failing test case Signed-off-by: Sri Harsha CH <sriharshach@google.com> --------- Signed-off-by: Sri Harsha CH <sriharshach@google.com> Co-authored-by: Sri Harsha CH <sriharshach@google.com> Co-authored-by: Sri Harsha CH <57220027+harshachinta@users.noreply.github.com>
1 parent beb33d2 commit ca76108
Copy full SHA for ca76108

File tree

Expand file treeCollapse file tree

2 files changed

+100
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+100
-1
lines changed

‎samples/samples/snippets.py

Copy file name to clipboardExpand all lines: samples/samples/snippets.py
+93-1Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from google.cloud.spanner_v1 import DirectedReadOptions, param_types
3434
from google.cloud.spanner_v1.data_types import JsonObject
3535
from google.protobuf import field_mask_pb2 # type: ignore
36+
from google.protobuf import struct_pb2 # type: ignore
3637

3738
from testdata import singer_pb2
3839

@@ -90,7 +91,7 @@ def update_instance(instance_id):
9091
labels={
9192
"sample_name": "snippets-update_instance-explicit",
9293
},
93-
edition=spanner_instance_admin.Instance.Edition.ENTERPRISE, # Optional
94+
edition=spanner_instance_admin.Instance.Edition.STANDARD, # Optional
9495
),
9596
field_mask=field_mask_pb2.FieldMask(paths=["labels", "edition"]),
9697
)
@@ -3204,6 +3205,7 @@ def create_instance_with_autoscaling_config(instance_id):
32043205
"sample_name": "snippets-create_instance_with_autoscaling_config",
32053206
"created": str(int(time.time())),
32063207
},
3208+
edition=spanner_instance_admin.Instance.Edition.ENTERPRISE, # Optional
32073209
),
32083210
)
32093211

@@ -3509,6 +3511,90 @@ def query_data_with_proto_types_parameter(instance_id, database_id):
35093511
# [END spanner_query_with_proto_types_parameter]
35103512

35113513

3514+
# [START spanner_database_add_split_points]
3515+
def add_split_points(instance_id, database_id):
3516+
"""Adds split points to table and index."""
3517+
3518+
from google.cloud.spanner_admin_database_v1.types import spanner_database_admin
3519+
3520+
spanner_client = spanner.Client()
3521+
database_admin_api = spanner_client.database_admin_api
3522+
3523+
request = spanner_database_admin.UpdateDatabaseDdlRequest(
3524+
database=database_admin_api.database_path(
3525+
spanner_client.project, instance_id, database_id
3526+
),
3527+
statements=["CREATE INDEX IF NOT EXISTS SingersByFirstLastName ON Singers(FirstName, LastName)"],
3528+
)
3529+
3530+
operation = database_admin_api.update_database_ddl(request)
3531+
3532+
print("Waiting for operation to complete...")
3533+
operation.result(OPERATION_TIMEOUT_SECONDS)
3534+
3535+
print("Added the SingersByFirstLastName index.")
3536+
3537+
addSplitPointRequest = spanner_database_admin.AddSplitPointsRequest(
3538+
database=database_admin_api.database_path(
3539+
spanner_client.project, instance_id, database_id
3540+
),
3541+
# Table split
3542+
# Index split without table key part
3543+
# Index split with table key part: first key is the index key and second the table key
3544+
split_points=[
3545+
spanner_database_admin.SplitPoints(
3546+
table="Singers",
3547+
keys=[
3548+
spanner_database_admin.SplitPoints.Key(
3549+
key_parts=struct_pb2.ListValue(
3550+
values=[struct_pb2.Value(string_value="42")]
3551+
)
3552+
)
3553+
],
3554+
),
3555+
spanner_database_admin.SplitPoints(
3556+
index="SingersByFirstLastName",
3557+
keys=[
3558+
spanner_database_admin.SplitPoints.Key(
3559+
key_parts=struct_pb2.ListValue(
3560+
values=[
3561+
struct_pb2.Value(string_value="John"),
3562+
struct_pb2.Value(string_value="Doe"),
3563+
]
3564+
)
3565+
)
3566+
],
3567+
),
3568+
spanner_database_admin.SplitPoints(
3569+
index="SingersByFirstLastName",
3570+
keys=[
3571+
spanner_database_admin.SplitPoints.Key(
3572+
key_parts=struct_pb2.ListValue(
3573+
values=[
3574+
struct_pb2.Value(string_value="Jane"),
3575+
struct_pb2.Value(string_value="Doe"),
3576+
]
3577+
)
3578+
),
3579+
spanner_database_admin.SplitPoints.Key(
3580+
key_parts=struct_pb2.ListValue(
3581+
values=[struct_pb2.Value(string_value="38")]
3582+
)
3583+
),
3584+
3585+
],
3586+
),
3587+
],
3588+
)
3589+
3590+
operation = database_admin_api.add_split_points(addSplitPointRequest)
3591+
3592+
print("Added split points.")
3593+
3594+
3595+
# [END spanner_database_add_split_points]
3596+
3597+
35123598
if __name__ == "__main__": # noqa: C901
35133599
parser = argparse.ArgumentParser(
35143600
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
@@ -3666,6 +3752,10 @@ def query_data_with_proto_types_parameter(instance_id, database_id):
36663752
"query_data_with_proto_types_parameter",
36673753
help=query_data_with_proto_types_parameter.__doc__,
36683754
)
3755+
subparsers.add_parser(
3756+
"add_split_points",
3757+
help=add_split_points.__doc__,
3758+
)
36693759

36703760
args = parser.parse_args()
36713761

@@ -3815,3 +3905,5 @@ def query_data_with_proto_types_parameter(instance_id, database_id):
38153905
update_data_with_proto_types_with_dml(args.instance_id, args.database_id)
38163906
elif args.command == "query_data_with_proto_types_parameter":
38173907
query_data_with_proto_types_parameter(args.instance_id, args.database_id)
3908+
elif args.command == "add_split_points":
3909+
add_split_points(args.instance_id, args.database_id)

‎samples/samples/snippets_test.py

Copy file name to clipboardExpand all lines: samples/samples/snippets_test.py
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,3 +1009,10 @@ def test_query_data_with_proto_types_parameter(
10091009
)
10101010
out, _ = capsys.readouterr()
10111011
assert "SingerId: 2, SingerInfo: singer_id: 2" in out
1012+
1013+
1014+
@pytest.mark.dependency(name="add_split_points", depends=["insert_data"])
1015+
def test_add_split_points(capsys, instance_id, sample_database):
1016+
snippets.add_split_points(instance_id, sample_database.database_id)
1017+
out, _ = capsys.readouterr()
1018+
assert "Added split points." in out

0 commit comments

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