Create and query a graph
This document shows you how to use BigQuery Graph to create a graph with financial information and run graph queries using the Graph Query Language (GQL).
Required roles
To get the permissions that
you need to work with graphs,
ask your administrator to grant you the
BigQuery Data Editor (roles/bigquery.dataEditor) IAM role on the dataset in which you create the node tables, edge tables, and graph.
For more information about granting roles, see Manage access to projects, folders, and organizations.
This predefined role contains the permissions required to work with graphs. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to work with graphs:
-
Create a graph:
bigquery.propertyGraphs.create -
List graphs and their metadata:
bigquery.propertyGraphs.list -
Get the metadata and definition of a graph:
bigquery.propertyGraphs.get -
Update the metadata and definition of a graph:
bigquery.propertyGraphs.update -
Delete a graph:
bigquery.propertyGraphs.delete
You might also be able to get these permissions with custom roles or other predefined roles.
Create node and edge tables
Graphs are built from existing BigQuery tables and stored in
datasets.
To store the tables and graph that you create in the following examples,
create a dataset.
The following query creates a dataset called graph_db:
CREATE SCHEMA IF NOT EXISTS graph_db;
The following tables contain information about people and accounts, and the relationships between each of these entities:
Person: information about people.Account: information about bank accounts.PersonOwnAccount: information about who owns which accounts.AccountTransferAccount: information about transfers between accounts.
To create these tables, run the following
CREATE TABLE statements:
CREATE OR REPLACE TABLE graph_db.Person (
id INT64,
name STRING,
birthday TIMESTAMP,
country STRING,
city STRING,
PRIMARY KEY (id) NOT ENFORCED
);
CREATE OR REPLACE TABLE graph_db.Account (
id INT64,
create_time TIMESTAMP,
is_blocked BOOL,
nick_name STRING,
PRIMARY KEY (id) NOT ENFORCED
);
CREATE OR REPLACE TABLE graph_db.PersonOwnAccount (
id INT64 NOT NULL,
account_id INT64 NOT NULL,
create_time TIMESTAMP,
PRIMARY KEY (id, account_id) NOT ENFORCED,
FOREIGN KEY (id) REFERENCES graph_db.Person(id) NOT ENFORCED,
FOREIGN KEY (account_id) REFERENCES graph_db.Account(id) NOT ENFORCED
);
CREATE OR REPLACE TABLE graph_db.AccountTransferAccount (
id INT64 NOT NULL,
to_id INT64 NOT NULL,
amount FLOAT64,
create_time