This document shows you how to set up and query Spanner Graph using the Google Cloud console and client libraries. The following topics help you learn how:
To learn about Spanner pricing details, see Spanner pricing.
To try out a codelab, see Getting started with Spanner Graph.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
- The Spanner API should be auto-enabled. If not, enable it manually: Enable Spanner API
-
To get the permissions that you need to create instances and databases, ask your administrator to grant you the Cloud Spanner Admin (
roles/spanner.admin
) IAM role on your project.
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. This section shows you how to create an instance using the Google Cloud console.
In the Google Cloud console, go to the Spanner page.
Select or create a Google Cloud project if you haven't done so already.
Do one of the following:
If you haven't created a Spanner instance before, on the Welcome to Spanner page, click Create a provisioned instance.
If you've created a Spanner instance, on the Instances page, click Create instance.
On the Select an edition page, select Enterprise Plus or Enterprise.
Spanner Graph is available only in the Enterprise edition or Enterprise Plus edition. To compare the different editions, click Compare editions. For more information, see the Spanner editions overview.
Click Continue.
In Instance name, enter an instance name, for example,
test-instance
.In Instance ID keep or change the instance ID. Your instance ID defaults to your instance name, but you can change it. Your instance name and instance ID can be the same or they can be different.
Click Continue.
In Choose a configuration, do the following:
Keep Regional selected.
In Select a configuration, select a region. The region you select is where your instances are stored and replicated.
Click Continue.
In Configure compute capacity, do the following:
In Select unit, select Processing units (PUs).
In Choose a scaling mode, keep Manual allocation selected and in Quantity keep 1000 processing units.
Click Create. The Google Cloud console displays the Overview page for the instance you created.
Create a database with Spanner Graph schema
This section shows you how to use the Google Cloud console and client libraries to create a database with Spanner Graph schema.
Console
- In the Google Cloud console, go to the Spanner Instances page.
Click the instance you created, for example,
Test Instance
.In Overview, under the name of your instance, click Create database.
In Database name, enter a database name. For example,
example-db
.In Select database dialect, choose Google Standard SQL. Spanner Graph isn't available in the PostgreSQL dialect. Your database creation page now looks like this:
Copy and paste the following schema into the DDL Templates editor tab. The schema contains two node table definitions,
Person
andAccount
, and two edge table definitions,PersonOwnAccount
andAccountTransferAccount
. Spanner Graph uses relational tables to define graphs, so you see both relational tables and graph statements in the schema. To learn more about the Spanner Graph schema, see Spanner Graph schema overview.CREATE TABLE Person ( id INT64 NOT NULL, name STRING(MAX), birthday TIMESTAMP, country STRING(MAX), city STRING(MAX), ) PRIMARY KEY (id); CREATE TABLE Account ( id INT64 NOT NULL, create_time TIMESTAMP, is_blocked BOOL, nick_name STRING(MAX), ) PRIMARY KEY (id); CREATE TABLE PersonOwnAccount ( id INT64 NOT NULL, account_id INT64 NOT NULL, create_time TIMESTAMP, FOREIGN KEY (account_id) REFERENCES Account (id) ) PRIMARY KEY (id, account_id), INTERLEAVE IN PARENT Person ON DELETE CASCADE; CREATE TABLE AccountTransferAccount ( id INT64 NOT NULL, to_id INT64 NOT NULL, amount FLOAT64, create_time TIMESTAMP NOT NULL, order_number STRING(MAX), FOREIGN KEY (to_id) REFERENCES Account (id) ) PRIMARY KEY (id, to_id, create_time), INTERLEAVE IN PARENT Account ON DELETE CASCADE; CREATE OR REPLACE PROPERTY GRAPH FinGraph NODE TABLES (Account, Person) EDGE TABLES ( PersonOwnAccount SOURCE KEY (id) REFERENCES Person (id) DESTINATION KEY (account_id) REFERENCES Account (id) LABEL Owns, AccountTransferAccount SOURCE KEY (id) REFERENCES Account (id) DESTINATION KEY (to_id) REFERENCES Account (id) LABEL Transfers );
Don't make any changes in Show encryption options.
Click Create. Google Cloud console displays the Overview page for the database you created.
Client libraries
Python
Java
Go
C++
Insert graph data
This section shows you how to use Google Cloud console and client libraries to insert data into a Spanner Graph.
Console
On the Spanner Studio page, click
New tab or use the editor tab.Copy and paste the following graph data insert statements into the nodes and edges.
INSERT INTO Account (id, create_time, is_blocked, nick_name) VALUES (7,"2020-01-10 06:22:20.222",false,"Vacation Fund"), (16,"2020-01-27 17:55:09.206",true,"Vacation Fund"), (20,"2020-02-18 05:44:20.655",false,"Rainy Day Fund"); INSERT INTO Person (id, name, birthday, country, city) VALUES (1,"Alex","1991-12-21 00:00:00","Australia","Adelaide"), (2,"Dana","1980-10-31 00:00:00","Czech_Republic","Moravia"), (3,"Lee","1986-12-07 00:00:00","India","Kollam"); INSERT INTO AccountTransferAccount (id, to_id, amount, create_time, order_number) VALUES (7,16,300,"2020-08-29 15:28:58.647","304330008004315"), (7,16,100,"2020-10-04 16:55:05.342","304120005529714"), (16,20,300,"2020-09-25 02:36:14.926","103650009791820"), (20,7,500,"2020-10-04 16:55:05.342","304120005529714"), (20,16,200,"2020-10-17 03:59:40.247","302290001255747"); INSERT INTO PersonOwnAccount (id, account_id, create_time) VALUES (1,7,"2020-01-10 06:22:20.222"), (2,20,"2020-01-27 17:55:09.206"), (3,16,"2020-02-18 05:44:20.655");
Click Run. When the run completes, the Results tab shows that 3 rows were inserted.
Client libraries
Python
Java
Go
C++
The following graph shows the people, accounts, account ownership, and account transfers from the inserts:
Run a graph query
This section shows you how to use the Google Cloud console or client libraries to run a Spanner Graph schema query.
Console
On the database Overview page, click Spanner Studio in the navigation menu.
On the Spanner Studio page, click
New tab or use the editor tab.Enter the following query in the query editor. The query finds everyone that Dana transferred money to, and the amount of those transfers.
GRAPH FinGraph MATCH (from_person:Person {name: "Dana"})-[:Owns]-> (from_account:Account)-[transfer:Transfers]-> (to_account:Account)<-[:Owns]-(to_person:Person) RETURN from_person.name AS from_account_owner, from_account.id AS from_account_id, to_person.name AS to_account_owner, to_account.id AS to_account_id, transfer.amount AS amount
Click Run.
The Results tab displays the following paths from Dana through
Account {id:20}
:- To
Account {id:7}
owned by Alex. To
Account {id:16}
owned by Lee.
- To
Client libraries
Python
Java
Go
C++
Clean up
Many of the examples in What's next make use of the resources that you set up in this document. If you want to continue working with Spanner Graph using one of these examples, don't perform these cleanup steps yet. This section shows you how to use the Google Cloud console to clean up your resources.
Otherwise, to avoid additional charges to your Cloud Billing account, delete the database and the instance that you created during setup. Deleting an instance automatically deletes all databases created in the instance.
Delete the database
In the Google Cloud console, go to the Spanner Instances page.
Click the name of the instance that has the database that you want to delete, for example, Test Instance.
Click the name of the database that you want to delete, for example, example-db.
On the Database details page, click delete Delete database.
Confirm that you want to delete the database by entering the database name and clicking Delete.
Delete the instance
In Google Cloud console, go to the Spanner Instances page.
Click the name of the instance that you want to delete, for example, Test Instance.
Click Delete instance.
Confirm that you want to delete the instance by entering the instance name and clicking Delete.
What's next
- Learn more about Spanner Graph using a codelab.
- Learn about the Spanner Graph schema.
- Create, update, or drop a Spanner Graph schema.
- Insert, update, or delete Spanner Graph data.
- Spanner Graph queries overview.
- Migrate to Spanner Graph.