This document shows you how to set up and query a Spanner Graph database to model, store, and analyze complex data relationships 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.
-
Verify 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.
-
Verify that billing is enabled for your Google Cloud project.
- Enable the Spanner API:
Go to 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.
-
To get the permissions that you need to query Spanner graphs if you're not granted the Cloud Spanner Admin role, ask your administrator to grant you the Cloud Spanner Database Reader (
roles/spanner.databaseReader
) IAM role on your project.
Create a Spanner instance
When you first use Spanner, you must create an instance, which allocates resources for 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 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 different.
Click Continue.
In Choose a configuration, do the following:
Keep Regional selected.
In Select a configuration, select a region. Spanner stores and replicates your instances in the region you select.
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 shows the Overview page for the instance you created.
Create a graph database using a schema
This section shows you how to create a Spanner Graph database using a Spanner Graph schema. You can use either the Google Cloud console or client libraries.
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.
Enter 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 the 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 );
Keep the default settings 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
After you create your database, you can add data to it. This section shows you how to insert data into your graph using the Google Cloud console for a graphical interface, or by using the client libraries programmatically.
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
After inserting data into your graph, you can query it to find patterns and relationships. This section shows you how to run queries using the Google Cloud console for a graphical interface, or programmatically with client libraries.
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, who owns account number 20:
- To
Account {id:7}
owned by Alex. To
Account {id:16}
owned by Lee.
- To
Client libraries
Python
Java
Go
C++
Clean up
This section shows you how to clean up the resources that you created to avoid incurring charges to your Cloud Billing account. If you plan to explore other Spanner Graph examples in the What's next section, don't perform these steps yet. Deleting a Spanner instance also deletes all databases within that instance.
Delete the database
In the Google Cloud console, go to Spanner Instances.
Click the instance name, for example, Test Instance.
Click the database name, for example, example-db.
On the Database details page, click 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.