Objectives
This tutorial walks you through the following steps using the Spanner client library for Go:
- Create a Spanner instance and database.
- Write, read, and execute SQL queries on data in the database.
- Update the database schema.
- Update data using a read-write transaction.
- Add a secondary index to the database.
- Use the index to read and execute SQL queries on data.
- Retrieve data using a read-only transaction.
Costs
This tutorial uses Spanner, which is a billable component of the Google Cloud. For information on the cost of using Spanner, see Pricing .
Before you begin
Complete the steps described in Set up , which cover creating and setting a default Google Cloud project, enabling billing, enabling the Cloud Spanner API, and setting up OAuth 2.0 to get authentication credentials to use the Cloud Spanner API.
In particular, make sure that you run gcloud auth
application-default login
to set up your local development environment with authentication
credentials.
Prepare your local Go environment
-
Install Go ( download ) on your development machine if it is not already installed.
-
Configure the
GOPATH
environment variable if it is not already configured, as described in Test your installation . -
Download the samples to your machine.
git clone https : //github.com/GoogleCloudPlatform/golang-samples $GOPATH/src/github.com/GoogleCloudPlatform/golang-samples
-
Change to the directory that contains the Spanner sample code:
cd $ GOPATH / src / github . com / GoogleCloudPlatform / golang - samples / spanner / spanner_snippets
-
Set the
GCLOUD_PROJECT
environment variable to your Google Cloud project ID:export GCLOUD_PROJECT =[ MY_PROJECT_ID ]
Create an instance
When you first use Spanner, you must create an instance, which is an allocation of resources that are used by Spanner databases. When you create an instance, you choose an instance configuration , which determines where your data is stored, and also the number of nodes to use, which determines the amount of serving and storage resources in your instance.
See Create an instance
to learn how to create a Spanner instance using any of the
following methods. You can name your instance test-instance
to use it with
other topics in this document that reference an instance named test-instance
.
- The Google Cloud CLI
- The Google Cloud console
- A client library (C++, C#, Go, Java, Node.js, PHP, Python, or Ruby)
Look through sample files
The samples repository contains a sample that shows how to use Spanner with Go.
Take a look through thesnippet.go
file, which shows how to use
Spanner. The code shows how to create and use a new database. The data
uses the example schema shown in the Schema and data model
page. Create a database
GoogleSQL
go
run
snippet
.
go
createdatabase
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
PostgreSQL
go
run
snippet
.
go
pgcreatedatabase
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
You should see:
Created
database
[
example
-
db
]
GoogleSQL
PostgreSQL
The next step is to write data to your database.
Create a database client
Before you can do reads or writes, you must create aClient
:
You can think of a Client
as a database connection: all of your interactions
with Spanner must go through a Client
. Typically you create a Client
when your application starts up, then you re-use that Client
to read, write,
and execute transactions. Each client uses resources in
Spanner.
If you create multiple clients in the same app, you should call Client.Close()
to clean up
the client's resources, including network connections, as soon as it is no
longer needed.
Read more in the Client
reference.
The code in the previous example also shows how to create a DatabaseAdminClient
,
which is used to create a database.
Write data with DML
You can insert data using Data Manipulation Language (DML) in a read-write transaction.
You use the Update()
method to execute a DML statement.
GoogleSQL
PostgreSQL
Run the sample using the dmlwrite
argument for Google SQL and the pgdmlwrite
argument for PostgreSQL:
GoogleSQL
go
run
snippet
.
go
dmlwrite
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
PostgreSQL
go
run
snippet
.
go
pgdmlwrite
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
You should see:
4
record
(
s
)
inserted
.
Write data with mutations
You can also insert data using mutations .
A Mutation
is
a container for mutation operations. A Mutation
represents a sequence of
inserts, updates, and deletes that Spanner applies atomically to
different rows and tables in a Spanner database.
Use Mutation.InsertOrUpdate()
to construct an INSERT_OR_UPDATE
mutation, which adds a new row or updates
column values if the row already exists. Alternatively, use the Mutation.Insert()
method to construct an INSERT
mutation, which adds a new row.
Client.Apply()
applies
mutations atomically to a database. This code shows how to write the data using mutations:
Run the sample using the write
argument:
go
run
snippet
.
go
write
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
You should see the command run successfully.
Query data using SQL
Spanner supports a SQL interface for reading data, which you can access on the command line using the Google Cloud CLI or programmatically using the Spanner client library for Go.
On the command line
Execute the following SQL statement to read the values of all columns from the Albums
table:
gcloud
spanner
databases
execute
-
sql
example
-
db
--
instance
=
test
-
instance
\
--
sql
=
'
SELECT
SingerId
,
AlbumId
,
AlbumTitle
FROM
Albums
'
The result shows:
SingerId
AlbumId
AlbumTitle
1
1
Total
Junk
1
2
Go
,
Go
,
Go
2
1
Green
2
2
Forever
Hold
Your
Peace
2
3
Terrified
Use the Spanner client library for Go
In addition to executing a SQL statement on the command line, you can issue the same SQL statement programmatically using the Spanner client library for Go.
The following methods and types are used to run the SQL query:-
Client.Single()
: use this to read the value of one or more columns from one or more rows in a Spanner table.Client.Single
returns aReadOnlyTransaction
, which is used for running a read or SQL statement. -
ReadOnlyTransaction.Query()
: use this method to execute a query against a database. - The
Statement
type: use this to construct a SQL string. - The
Row
type: use this to access the data returned by a SQL statement or read call.
Here's how to issue the query and access the data:
Run the sample using the query
argument.
go
run
snippet
.
go
query
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
You should see the following result:
1
1
Total
Junk
1
2
Go
,
Go
,
Go
2
1
Green
2
2
Forever
Hold
Your
Peace
2
3
Terrified
Query using a SQL parameter
If your application has a frequently executed query, you can improve its performance by parameterizing it. The resulting parametric query can be cached and reused, which reduces compilation costs. For more information, see Use query parameters to speed up frequently executed queries .
Here is an example of using a parameter in the WHERE
clause to
query records containing a specific value for LastName
.
GoogleSQL
PostgreSQL
Run the sample using the querywithparameter
argument for Google SQL and the pgqueryparameter
argument for PostgreSQL.
GoogleSQL
go
run
snippet
.
go
querywithparameter
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
PostgreSQL
go
run
snippet
.
go
pgqueryparameter
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
You should see output similar to:
12
Melissa
Garcia
Read data using the read API
In addition to Spanner's SQL interface, Spanner also supports a read interface.
UseReadOnlyTransaction.Read()
to read rows from the database. Use KeySet
to define a collection of keys and key ranges to read. Here's how to read the data:
Run the sample using the read
argument.
go
run
snippet
.
go
read
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
You should see output similar to:
1
1
Total
Junk
1
2
Go
,
Go
,
Go
2
1
Green
2
2
Forever
Hold
Your
Peace
2
3
Terrified
Update the database schema
Assume you need to add a new column called MarketingBudget
to the Albums
table. Adding a new column to an existing table requires an update to your
database schema. Spanner supports schema updates to a database while the
database continues to serve traffic. Schema updates don't require taking the
database offline and they don't lock entire tables or columns; you can continue
writing data to the database during the schema update. Read more about supported
schema updates and schema change performance in Make schema updates
.
Add a column
You can add a column on the command line using the Google Cloud CLI or programmatically using the Spanner client library for Go.
On the command line
Use the following ALTER TABLE
command to
add the new column to the table:
GoogleSQL
gcloud
spanner
databases
ddl
update
example
-
db
--
instance
=
test
-
instance
\
--
ddl
=
'
ALTER
TABLE
Albums
ADD
COLUMN
MarketingBudget
INT64
'
PostgreSQL
gcloud
spanner
databases
ddl
update
example
-
db
--
instance
=
test
-
instance
\
--
ddl
=
'
ALTER
TABLE
Albums
ADD
COLUMN
MarketingBudget
BIGINT
'
You should see:
Schema
updating
...
done
.
Use the Spanner client library for Go
UseDatabaseAdminClient.UpdateDatabaseDdl()
to modify the schema: GoogleSQL
PostgreSQL
Run the sample using the addnewcolumn
argument for Google SQL and the pgaddnewcolumn
argument for PostgreSQL.
GoogleSQL
go
run
snippet
.
go
addnewcolumn
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
PostgreSQL
go
run
snippet
.
go
pgaddnewcolumn
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
You should see:
Added
MarketingBudget
column
.
Write data to the new column
The following code writes data to the new column. It sets MarketingBudget
to 100000
for the row keyed by Albums(1, 1)
and to 500000
for the row keyed
by Albums(2, 2)
.
Run the sample using the update
argument.
go
run
snippet
.
go
update
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
You can also execute a SQL query or a read call to fetch the values that you just wrote.
Here's the code to execute the query:
GoogleSQL
PostgreSQL
To execute this query, run the sample using the querynewcolumn
argument for Google SQL and the pgquerynewcolumn
argument for PostgreSQL.
GoogleSQL
go
run
snippet
.
go
querynewcolumn
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
PostgreSQL
go
run
snippet
.
go
pgquerynewcolumn
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
You should see:
1
1
100000
1
2
NULL
2
1
NULL
2
2
500000
2
3
NULL
Update data
You can update data using DML in a read-write transaction.
You use the Update()
method to execute a DML statement.
GoogleSQL
PostgreSQL
Run the sample using the dmlwritetxn
argument.
go
run
snippet
.
go
dmlwritetxn
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
You should see:
Moved
200000
from
Album2
'
s
MarketingBudget
to
Album1
'
s
.
Use a secondary index
Suppose you wanted to fetch all rows of Albums
that have AlbumTitle
values
in a certain range. You could read all values from the AlbumTitle
column using
a SQL statement or a read call, and then discard the rows that don't meet the
criteria, but doing this full table scan is expensive, especially for tables
with a lot of rows. Instead you can speed up the retrieval of rows when
searching by non-primary key columns by creating a secondary index
on the table.
Adding a secondary index to an existing table requires a schema update. Like other schema updates, Spanner supports adding an index while the database continues to serve traffic. Spanner automatically backfills the index with your existing data. Backfills might take a few minutes to complete, but you don't need to take the database offline or avoid writing to the indexed table during this process. For more details, see Add a secondary index .
After you add a secondary index, Spanner automatically uses it for SQL queries that are likely to run faster with the index. If you use the read interface, you must specify the index that you want to use.
Add a secondary index
You can add an index on the command line using the gcloud CLI or programmatically using the Spanner client library for Go.
On the command line
Use the following CREATE INDEX
command
to add an index to the database:
gcloud
spanner
databases
ddl
update
example-db
--instance =
test-instance
\
--ddl =
'CREATE INDEX AlbumsByAlbumTitle ON Albums(AlbumTitle)'
You should see:
Schema
updating
...
done
.
Using the Spanner client library for Go
UseUpdateDatabaseDdl()
to add an index:
Adding an index can take a few minutes. After the index is added, you should see:
Added
index
Read using the index
For SQL queries, Spanner automatically uses an appropriate index. In the read interface, you must specify the index in your request.
To use the index in the read interface, use ReadOnlyTransaction.ReadUsingIndex()
, which reads zero or
more rows from a database using an index.
The following code fetches all AlbumId
, and AlbumTitle
columns from the AlbumsByAlbumTitle
index.
Run the sample using the readindex
argument.
go
run
snippet
.
go
readindex
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
You should see:
2
Forever
Hold
Your
Peace
2
Go
,
Go
,
Go
1
Green
3
Terrified
1
Total
Junk
Add an index for index-only reads
You might have noticed that the previous read example doesn't include reading
the MarketingBudget
column. This is because Spanner's read interface
doesn't support the ability to join an index with a data table to look up values
that are not stored in the index.
Create an alternate definition of AlbumsByAlbumTitle
that stores a copy of MarketingBudget
in the index.
On the command line
GoogleSQL
gcloud
spanner
databases
ddl
update
example
-
db
--
instance
=
test
-
instance
\
--
ddl
=
'
CREATE
INDEX
AlbumsByAlbumTitle2
ON
Albums
(
AlbumTitle
)
STORING
(
MarketingBudget
)
PostgreSQL
gcloud
spanner
databases
ddl
update
example
-
db
--
instance
=
test
-
instance
\
--
ddl
=
'
CREATE
INDEX
AlbumsByAlbumTitle2
ON
Albums
(
AlbumTitle
)
INCLUDE
(
MarketingBudget
)
Adding an index can take a few minutes. After the index is added, you should see:
Schema
updating
...
done
.
Using the Spanner client library for Go
UseUpdateDatabaseDdl()
to add an index with a STORING
clause for GoogleSQL and INCLUDE
clause for PostgreSQL: GoogleSQL
PostgreSQL
Run the sample using the addstoringindex
argument.
GoogleSQL
go
run
snippet
.
go
addstoringindex
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
PostgreSQL
go
run
snippet
.
go
pgaddstoringindex
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
Adding an index can take a few minutes. After the index is added, you should see:
Added
storing
index
Now you can execute a read that fetches all AlbumId
, AlbumTitle
, and MarketingBudget
columns from the AlbumsByAlbumTitle2
index:
Run the sample using the readstoringindex
argument.
go
run
snippet
.
go
readstoringindex
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
You should see output similar to:
2
Forever
Hold
Your
Peace
300000
2
Go
,
Go
,
Go
NULL
1
Green
NULL
3
Terrified
NULL
1
Total
Junk
300000
Retrieve data using read-only transactions
Suppose you want to execute more than one read at the same timestamp. Read-only
transactions
observe a consistent
prefix of the transaction commit history, so your application always gets
consistent data.
Use the ReadOnlyTransaction
type for executing read-only transactions. Use Client.ReadOnlyTransaction()
to get a ReadOnlyTransaction
.
The following shows how to run a query and perform a read in the same read-only transaction:
Run the sample using the readonlytransaction
argument.
go
run
snippet
.
go
readonlytransaction
projects
/
GCLOUD_PROJECT
/
instances
/
test
-
instance
/
databases
/
example
-
db
You should see output similar to:
2
2
Forever
Hold
Your
Peace
1
2
Go
,
Go
,
Go
2
1
Green
2
3
Terrified
1
1
Total
Junk
1
1
Total
Junk
1
2
Go
,
Go
,
Go
2
1
Green
2
2
Forever
Hold
Your
Peace
2
3
Terrified
Cleanup
To avoid incurring additional charges to your Cloud Billing account for the resources used in this tutorial, drop the database and delete the instance that you created.
Delete the database
If you delete an instance, all databases within it are automatically deleted. This step shows how to delete a database without deleting an instance (you would still incur charges for the instance).
On the command line
gcloud
spanner
databases
delete
example
-
db
--
instance
=
test
-
instance
Using the Google Cloud console
-
Go to the Spanner Instancespage in the Google Cloud console.
-
Click the instance.
-
Click the database that you want to delete.
-
In the Database detailspage, click Delete.
-
Confirm that you want to delete the database and click Delete.
Delete the instance
Deleting an instance automatically drops all databases created in that instance.
On the command line
gcloud
spanner
instances
delete
test
-
instance
Using the Google Cloud console
-
Go to the Spanner Instancespage in the Google Cloud console.
-
Click your instance.
-
Click Delete.
-
Confirm that you want to delete the instance and click Delete.
What's next
-
Learn how to access Spanner with a virtual machine instance .
-
Learn about authorization and authentication credentials in Authenticate to Cloud services using client libraries .
-
Learn more about Spanner Schema design best practices .