GET SUBGRAPH¶
The GET SUBGRAPH
statement retrieves information of vertices and edges reachable from the source vertices of the specified edge types and returns information of the subgraph.
Syntax¶
GET SUBGRAPH [WITH PROP] [<step_count> STEPS] FROM {<vid>, <vid>...}
[{IN | OUT | BOTH} <edge_type>, <edge_type>...]
[YIELD [VERTICES AS <vertex_alias>] [,EDGES AS <edge_alias>]];
WITH PROP
shows the properties. If not specified, the properties will be hidden.
step_count
specifies the number of hops from the source vertices and returns the subgraph from 0 tostep_count
hops. It must be a non-negative integer. Its default value is 1.
vid
specifies the vertex IDs.
edge_type
specifies the edge type. You can useIN
,OUT
, andBOTH
to specify the traversal direction of the edge type. The default isBOTH
.
YIELD
defines the output that needs to be returned. You can return only vertexes or edges. The alias must be set. When you do not useYIELD
to define the output result,_vertices
and_edges
are returned by default.
Note
The path type of GET SUBGRAPH
is trail
. Only vertices can be repeatedly visited in graph traversal. For more information, see Path.
Examples¶
The following graph is used as the sample.
Insert the test data:
nebula> CREATE SPACE IF NOT EXISTS subgraph(partition_num=15, replica_factor=1, vid_type=fixed_string(30));
nebula> USE subgraph;
nebula> CREATE TAG IF NOT EXISTS player(name string, age int);
nebula> CREATE TAG IF NOT EXISTS team(name string);
nebula> CREATE EDGE IF NOT EXISTS follow(degree int);
nebula> CREATE EDGE IF NOT EXISTS serve(start_year int, end_year int);
nebula> INSERT VERTEX player(name, age) VALUES "player100":("Tim Duncan", 42);
nebula> INSERT VERTEX player(name, age) VALUES "player101":("Tony Parker", 36);
nebula> INSERT VERTEX player(name, age) VALUES "player102":("LaMarcus Aldridge", 33);
nebula> INSERT VERTEX team(name) VALUES "team203":("Trail Blazers"), "team204":("Spurs");
nebula> INSERT EDGE follow(degree) VALUES "player101" -> "player100":(95);
nebula> INSERT EDGE follow(degree) VALUES "player101" -> "player102":(90);
nebula> INSERT EDGE follow(degree) VALUES "player102" -> "player100":(75);
nebula> INSERT EDGE serve(start_year, end_year) VALUES "player101" -> "team204":(1999, 2018),"player102" -> "team203":(2006, 2015);
- This example goes one step from the vertex
player101
over all edge types and gets the subgraph.nebula> GET SUBGRAPH 1 STEPS FROM "player101" YIELD VERTICES AS nodes, EDGES AS relationships; +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+ | nodes | relationships | +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+ | [("player101" :player{})] | [[:serve "player101"->"team204" @0 {}], [:follow "player101"->"player100" @0 {}], [:follow "player101"->"player102" @0 {}]] | | [("team204" :team{}), ("player100" :player{}), ("player102" :player{})] | [[:follow "player102"->"player100" @0 {}]] | +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+
The returned subgraph is as follows.
- This example goes one step from the vertex
player101
over incomingfollow
edges and gets the subgraph.nebula> GET SUBGRAPH 1 STEPS FROM "player101" IN follow YIELD VERTICES AS nodes, EDGES AS relationships; +---------------------------+---------------+ | nodes | relationships | +---------------------------+---------------+ | [("player101" :player{})] | [] | | [] | [] | +---------------------------+---------------+
There is no incoming
follow
edge toplayer101
, so only the vertexplayer101
is returned.
- This example goes one step from the vertex
player101
over outgoingserve
edges, gets the subgraph, and shows the property of the edge.nebula> GET SUBGRAPH WITH PROP 1 STEPS FROM "player101" OUT serve YIELD VERTICES AS nodes, EDGES AS relationships; +-------------------------------------------------------+-------------------------------------------------------------------------+ | nodes | relationships | +-------------------------------------------------------+-------------------------------------------------------------------------+ | [("player101" :player{age: 36, name: "Tony Parker"})] | [[:serve "player101"->"team204" @0 {end_year: 2018, start_year: 1999}]] | | [("team204" :team{name: "Spurs"})] | [] | +-------------------------------------------------------+-------------------------------------------------------------------------+
The returned subgraph is as follows.
FAQ¶
Why is the number of hops in the returned result greater than step_count
?¶
To show the completeness of the subgraph, an additional hop is made on all vertices that meet the conditions. The following graph is used as the sample.
- The returned paths of
GET SUBGRAPH 1 STEPS FROM "A";
areA->B
,B->A
, andA->C
. To show the completeness of the subgraph, an additional hop is made on all vertices that meet the conditions, namelyB->C
.
- The returned path of
GET SUBGRAPH 1 STEPS FROM "A" IN follow;
isB->A
. To show the completeness of the subgraph, an additional hop is made on all vertices that meet the conditions, namelyA->B
.
If you only query paths or vertices that meet the conditions, we suggest you use MATCH or GO. The example is as follows.
nebula> match p= (v:player) -- (v2) where id(v)=="A" return p;
nebula> go 1 steps from "A" over follow;
Why is the number of hops in the returned result lower than step_count
?¶
The query stops when there is not enough subgraph data and will not return the null value.
nebula> GET SUBGRAPH 100 STEPS FROM "player101" OUT follow YIELD VERTICES AS nodes, EDGES AS relationships;
+----------------------------------------------------+--------------------------------------------------------------------------------------+
| nodes | relationships |
+----------------------------------------------------+--------------------------------------------------------------------------------------+
| [("player101" :player{})] | [[:follow "player101"->"player100" @0 {}], [:follow "player101"->"player102" @0 {}]] |
| [("player100" :player{}), ("player102" :player{})] | [[:follow "player102"->"player100" @0 {}]] |
+----------------------------------------------------+--------------------------------------------------------------------------------------+