This page introduces statements for managing graphs in a database.
Showing Graphs
The SHOW GRAPH
statement retrieves information about all graphs in the database.
SHOW GRAPH
The information about graphs is organized into tables _graph
, _graph_shard_1
, _graph_shard_2
, and so on:
- The
_graph
table contains all graphs in the database. - Each
_graph_shard_<N>
table contains the graphs that have data stored in the shard with id<N>
.
Each table includes fields that provide essential details about each graph:
Field |
Description |
---|---|
id |
The unique id of the graph. |
name |
The unique name assigned to the graph. |
total_nodes |
The total count of nodes in the graph. Only available in _graph . |
total_edges |
The total count of edges in the graph. Only available in _graph . |
description |
The description given to the graph. |
status |
The current state of the graph, which can be NORMAL , LOADING_SNAPSHOT , CREATING , DROPPING , or SCALING . |
shards |
The ids of shards where the graph data is distributed. |
partition_by |
The function that computes the hash value for the sharding key, which is essential for sharding the graph data. |
meta_version |
The version number utilized by meta servers to synchronize DDL (Data Definition Language) operations on the graph with shard servers. |
Creating Graphs
The CREATE GRAPH
statement creates new graphs in the database.
<create graph statement> ::=
"CREATE GRAPH" <graph name> <graph structure> <graph sharding> <graph description>
<graph structure> ::=
"{" [ <element schemas> ] "}"
<element schemas> ::=
<element schema> [ { "," <element schema> }... ]
<element schema> ::=
<node schema> | <edge schema>
<node schema> ::=
"NODE" [ "TYPE" ] <node schema name> "(" <property types> ")"
<edge schema> ::=
"EDGE" [ "TYPE" ] <edge schema name> "()-[" <property types> "]->()"
<property types> ::=
"{" <property type> [ { "," <property type> }... ] "}"
<property type> ::=
<property name> <property value type>
<graph sharding> ::=
"PARTITION BY" <hash function> "SHARDS" <shard id list>
<graph description> ::=
"COMMENTS" <description string>
When creating a graph, you can specify four components:
Component |
Description |
---|---|
<graph name> |
The unique name of the graph. See naming conventions. |
<graph structure> |
A graph structure that defines the <node schema> s and <edge schema> s (collectively referred to as <element schema> s) of the graph. It can be omitted by using an empty {} .Each <element schema> includes:
|
<graph sharding> |
The sharding strategy for the graph, including:
|
<graph description> |
Optional. Description of the graph. |
To create a graph g1
with the following specifications:
- Node schemas:
User
andClub
- Edge schemas:
Follows
andJoins
- Sharding: Data distributed across shards
1
,2
, and3
using theCrc32
hash function
CREATE GRAPH g1 {
NODE User ({name string, gender string}),
NODE Club ({name string, since uint32}),
EDGE Follows ()-[{createdOn datetime}]->(),
EDGE Joins ()-[{memberNo uint64}]->()
} PARTITION BY HASH(Crc32) SHARDS [1,2,3] COMMENT 'My first graph'
To create a graph g2
with node and edge schemas only:
CREATE GRAPH g2 {
NODE User (),
NODE Club (),
EDGE Joins ()-[{}]->()
} PARTITION BY HASH(Crc32) SHARDS [1,2]
To create a graph g3
with an empty structure, and distribute its data only to shard 1
using the CityHash64
hash function:
CREATE GRAPH g3 {} PARTITION BY HASH(CityHash64) SHARDS [1]
To create two graphs:
CREATE GRAPH g1 {
NODE User ({name string, gender string}),
NODE Club ({name string, since uint32}),
EDGE Follows ({createdOn datetime}),
EDGE Joins ({memberNo uint64})
} PARTITION BY HASH(Crc32) SHARDS [1,2,3] COMMENT 'My first graph'
CREATE GRAPH g2 {} PARTITION BY HASH(CityHash64) SHARDS [1]
Dropping Graphs
The DROP GRAPH
statement drops graphs from the database.
<drop graph statement> ::=
"DROP GRAPH" <graph name>
To drop the graph g1
:
DROP GRAPH g1
To drop two graphs:
DROP GRAPH g1
DROP GRAPH g2
Adding Schemas to Graph
The ALTER GRAPH
statement allows you to add node and edge schemas to a graph.
<alter graph statement> ::=
"ALTER GRAPH" <graph name> <add element schemas>
<add element schemas> ::=
<add node schemas> | <add edge schemas>
<add node schemas> ::=
"ADD NODE" "{" <add node schema> [ { "," <add node schema> }... ] "}"
<add node schema> ::=
<node schema name> "(" <property types> ")"
<add edge schemas> ::=
"ADD EDGE" "{" <add edge schema> [ { "," <add edge schema> }... ] "}"
<add edge schema> ::=
<edge schema name> "(" <property types> ")"
Details
To add node schemas Book
and Company
to the graph g1
:
ALTER GRAPH g1 ADD NODE {Book ({title string, rating float}), Company ({name string})}
To add an edge schema Rates
to the graph g1
:
ALTER GRAPH g1 ADD EDGE {Rates ({value float})}
Dropping Schemas from Graph
The ALTER GRAPH
statement allows you to drop node and edge schemas from a graph. Dropping a node or edge schema deletes the schema along with the nodes or edges belonging to it from the database. Note that the deletion of a node leads to the removal of all edges that are connected to it. The two default schemas cannot be dropped.
The schema dropping operation runs as a job, you may run SHOW JOB
afterward to verify its completion.
<alter graph statement> ::=
"ALTER GRAPH" <graph name> <drop element schemas>
<drop element schemas> ::=
<drop node schemas> | <drop edge schemas>
<drop node schemas> ::=
"DROP NODE" <node schema name> [ { "," <node schema name> }... ]
<drop edge schemas> ::=
"DROP EDGE" <edge schema name> [ { "," <edge schema name> }... ]
To drop a node schema User
from the graph g1
:
ALTER GRAPH g1 DROP NODE User
To drop edge schemas Follows
and Joins
from the graph g1
:
ALTER GRAPH g1 DROP EDGE Follows, Joins