/
samples.py
105 lines (86 loc) · 3.54 KB
/
samples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
# Copyright 2024 Google, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This application demonstrates how to do basic operations using Cloud
Spanner.
For more information, see the README.rst under /spanner.
"""
import time
from google.cloud import spanner
from google.cloud.spanner_admin_instance_v1.types import spanner_instance_admin
from google.cloud.spanner_admin_database_v1.types import spanner_database_admin
OPERATION_TIMEOUT_SECONDS = 240
# [START spanner_create_instance]
def create_instance(instance_id):
"""Creates an instance."""
spanner_client = spanner.Client()
config_name = "{}/instanceConfigs/regional-us-central1".format(
spanner_client.project_name
)
operation = spanner_client.instance_admin_api.create_instance(
parent="projects/{}".format(spanner_client.project),
instance_id=instance_id,
instance=spanner_instance_admin.Instance(
config=config_name,
display_name="This is a display name.",
node_count=1,
labels={
"cloud_spanner_samples": "true",
"sample_name": "snippets-create_instance-explicit",
"created": str(int(time.time())),
},
),
)
print("Waiting for operation to complete...")
operation.result(OPERATION_TIMEOUT_SECONDS)
print("Created instance {}".format(instance_id))
# [END spanner_create_instance]
# [START spanner_create_database_with_default_leader]
def create_database_with_default_leader(instance_id, database_id, default_leader):
"""Creates a database with tables with a default leader."""
spanner_client = spanner.Client()
operation = spanner_client.database_admin_api.create_database(
request=spanner_database_admin.CreateDatabaseRequest(
parent="projects/{}/instances/{}".format(
spanner_client.project, instance_id
),
create_statement="CREATE DATABASE {}".format(database_id),
extra_statements=[
"""CREATE TABLE Singers (
SingerId INT64 NOT NULL,
FirstName STRING(1024),
LastName STRING(1024),
SingerInfo BYTES(MAX)
) PRIMARY KEY (SingerId)""",
"""CREATE TABLE Albums (
SingerId INT64 NOT NULL,
AlbumId INT64 NOT NULL,
AlbumTitle STRING(MAX)
) PRIMARY KEY (SingerId, AlbumId),
INTERLEAVE IN PARENT Singers ON DELETE CASCADE""",
"ALTER DATABASE {}"
" SET OPTIONS (default_leader = '{}')".format(
database_id, default_leader
),
],
)
)
print("Waiting for operation to complete...")
database = operation.result(OPERATION_TIMEOUT_SECONDS)
print(
"Database {} created with default leader {}".format(
database.name, database.default_leader
)
)
# [END spanner_create_database_with_default_leader]