Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Google Cloud Spanner Tutorial

Introduction to Cloud Spanner

Google Cloud Spanner is a fully managed, scalable, globally distributed, and strongly consistent database service. It combines the benefits of a relational database structure with non-relational horizontal scale. It is designed to support applications that require high availability, strong consistency, and high performance.

Setting Up Your Environment

Before you can start using Cloud Spanner, you need to set up your Google Cloud environment. Follow these steps:

Step 1: Create a Google Cloud Project

Go to the Google Cloud Console and create a new project.

Step 2: Enable the Cloud Spanner API

In the Google Cloud Console, go to the API & Services Dashboard and enable the Cloud Spanner API.

Step 3: Set up billing

Ensure that billing is enabled for your Google Cloud project.

Creating a Cloud Spanner Instance

To use Cloud Spanner, you need to create an instance. An instance is a container for your databases. Follow these steps to create an instance:

Step 1: Go to the Spanner Console

Navigate to the Spanner Console.

Step 2: Create an Instance

Click on "Create Instance" and fill in the necessary details such as instance name, instance ID, and configuration. Choose the number of nodes based on your needs.

Creating a Database

Once you have an instance, you can create a database within that instance. Follow these steps:

Step 1: Go to Your Instance

In the Spanner Console, click on the instance you created.

Step 2: Create a Database

Click on "Create Database" and provide a name for your database.

Defining Schema

Defining the schema is an essential part of setting up your database. Here's how you can define a schema for a sample database:

Step 1: Open the Database

Navigate to the database you created in the Spanner Console.

Step 2: Define the Schema

Click on "Edit Schema" and enter the following SQL statements:

CREATE TABLE Singers (
  SingerId INT64 NOT NULL,
  FirstName STRING(1024),
  LastName STRING(1024),
  SingerInfo BYTES(MAX)
) PRIMARY KEY (SingerId);

CREATE TABLE Albums (
  AlbumId INT64 NOT NULL,
  SingerId INT64 NOT NULL,
  AlbumTitle STRING(MAX)
) PRIMARY KEY (AlbumId),
  INTERLEAVE IN PARENT Singers ON DELETE CASCADE;

Inserting Data

Now that you have defined your schema, you can insert data into your tables. Here's an example of how to insert data:

Step 1: Open the Database

Navigate to the database you created in the Spanner Console.

Step 2: Insert Data

Click on "Query" and enter the following SQL statements to insert data:

INSERT INTO Singers (SingerId, FirstName, LastName) VALUES (1, 'Marc', 'Richards');
INSERT INTO Singers (SingerId, FirstName, LastName) VALUES (2, 'Catalina', 'Smith');
INSERT INTO Albums (AlbumId, SingerId, AlbumTitle) VALUES (1, 1, 'Total Junk');
INSERT INTO Albums (AlbumId, SingerId, AlbumTitle) VALUES (2, 2, 'Go, Go, Go');

Click "Run" to execute the statements.

Querying Data

To query the data you have inserted, you can use SQL. Here's how you can query data:

Step 1: Open the Database

Navigate to the database you created in the Spanner Console.

Step 2: Run a Query

Click on "Query" and enter the following SQL statement:

SELECT * FROM Singers;

Click "Run" to execute the query and see the results.

+----------+-----------+----------+
| SingerId | FirstName | LastName |
+----------+-----------+----------+
|        1 | Marc      | Richards |
|        2 | Catalina  | Smith    |
+----------+-----------+----------+
            

Conclusion

In this tutorial, you have learned the basics of Google Cloud Spanner, including setting up your environment, creating an instance, defining a schema, inserting data, and querying data. Cloud Spanner is a powerful tool for applications requiring high availability, strong consistency, and horizontal scalability.