Main Content

storeDigraph

Store directed graph in Neo4j database

Description

example

storeDigraph(neo4jconn,G) converts a directed graph to a Neo4j® graph and stores it in a Neo4j database using a Neo4j database connection. The variables in the node and edge tables of the digraph object (except the EndNodes variable) become the properties of the nodes and relationships in the Neo4j graph.

example

storeDigraph(neo4jconn,G,Name,Value) specifies additional options using one or more name-value pair arguments. For example, 'GlobalNodeLabel','Person' stores all nodes in the directed graph by using the Person node label.

example

graphinfo = storeDigraph(___) returns graph information as a structure using any of the input argument combinations in the previous syntaxes.

Examples

collapse all

Create a digraph object and store its contents in a Neo4j® database. Display the contents of the resulting Neo4j graph.

Assume that you have graph data stored in a Neo4j database that represents a social neighborhood. This database has seven nodes and eight relationships. Each node has only one unique property key name with a value ranging from User1 through User7. Each relationship has the type knows.

Create a Neo4j database connection using the URL http://localhost:7474/db/data, user name neo4j, and password matlab.

url = 'http://localhost:7474/db/data';
username = 'neo4j';
password = 'matlab';

neo4jconn = neo4j(url,username,password);

Check the Message property of the Neo4j connection object neo4jconn. The blank Message property indicates a successful connection.

neo4jconn.Message
ans =

     []

Create a digraph object with three nodes, which represents a new Neo4j graph. The nodes represent three additional people: User8, User9, and User10.

G = digraph([1 1 3],[2 3 2],[1 2 3],{'User8','User9','User10'});

Store the data as a Neo4j graph in the Neo4j database.

storeDigraph(neo4jconn,G)

By default, the storeDigraph function stores the directed graph without node labels. Also, the function stores the relationships with the default relationship type Edge.

Display information about the Neo4j graph nodes. graphinfo is a structure that contains node and relationship information.

criteria = ["Edge"];
graphinfo = searchGraph(neo4jconn,criteria);
graphinfo.Nodes
ans=3×3 table
          NodeLabels      NodeData                  NodeObject             
          __________    ____________    ___________________________________

    7         []        [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    52        []        [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    47        []        [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]

Nodes is a table that contains these variables:

  • Node label

  • Node data

  • Neo4jNode object

Display information about the Neo4j graph relationships.

graphinfo.Relations
ans=3×5 table
          StartNodeID    RelationType    EndNodeID    RelationData                RelationObject             
          ___________    ____________    _________    ____________    _______________________________________

    17         7            'Edge'          52        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    18        47            'Edge'          52        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    35         7            'Edge'          47        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]

Relations is a table that contains these variables:

  • Start node identifier

  • Relationship type

  • End node identifier

  • Relationship data

  • Neo4jRelation object

Close the database connection.

close(neo4jconn)

Create a digraph object and store its contents in a Neo4j® database. Specify a node label to apply to all nodes in the resulting Neo4j graph. Specify a relationship type to apply to all relationships in the resulting Neo4j graph. Display the contents of the graph.

Assume that you have graph data stored in a Neo4j database that represents a social neighborhood. This database has seven nodes and eight relationships. Each node has only one unique property key name with a value ranging from User1 through User7. Each relationship has the type knows.

Create a Neo4j database connection using the URL http://localhost:7474/db/data, user name neo4j, and password matlab.

url = 'http://localhost:7474/db/data';
username = 'neo4j';
password = 'matlab';

neo4jconn = neo4j(url,username,password);

Check the Message property of the Neo4j connection object neo4jconn. The blank Message property indicates a successful connection.

neo4jconn.Message
ans =

     []

Create a digraph object with three nodes, which represents a new Neo4j graph. The nodes represent three additional people: User8, User9, and User10.

G = digraph([1 1 3],[2 3 2],[1 2 3],["User8" "User9" "User10"]);

Store the data as a Neo4j graph in the Neo4j database. Specify the node label Person for each node in the resulting Neo4j graph by using the 'GlobalNodeLabel' name-value pair argument. Specify the relationship type knows for each relationship in the graph by using the 'GlobalRelationType' name-value pair argument.

storeDigraph(neo4jconn,G,'GlobalNodeLabel','Person', ...
    'GlobalRelationType','knows')

Display information about the Neo4j graph nodes. graphinfo is a structure that contains node and relationship information.

criteria = {'Person'};
graphinfo = searchGraph(neo4jconn,criteria);
graphinfo.Nodes
ans=10×3 table
          NodeLabels      NodeData                  NodeObject             
          __________    ____________    ___________________________________

    0      'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    48     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    1      'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    2      'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    3      'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    4      'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    5      'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    9      'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    49     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    50     'Person'     [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]

Nodes is a table that contains these variables:

  • Node label

  • Node data

  • Neo4jNode object

graphinfo contains the three additional nodes.

Display information about the Neo4j graph relationships.

graphinfo.Relations
ans=11×5 table
          StartNodeID    RelationType    EndNodeID    RelationData                RelationObject             
          ___________    ____________    _________    ____________    _______________________________________

    1          0           'knows'           1        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    0          0           'knows'           2        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    3          1           'knows'           3        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    2          2           'knows'           1        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    5          3           'knows'           4        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    4          3           'knows'           5        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    6          5           'knows'           4        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    8          5           'knows'           9        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    19        48           'knows'          49        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    7         48           'knows'          50        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    9         50           'knows'          49        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]

Relations is a table that contains these variables:

  • Start node identifier

  • Relationship type

  • End node identifier

  • Relationship data

  • Neo4jRelation object

graphinfo contains the three additional relationships.

Close the database connection.

close(neo4jconn)

Create a digraph object by specifying nodes and edges. Then, store the directed graph in a Neo4j® database by specifying node labels and relationship types. Display the contents of the resulting Neo4j graph. Access the graph information using an output argument.

Assume that you have graph data stored in a Neo4j database that represents a social neighborhood. This database has seven nodes and eight relationships. Each node has only one unique property key name with a value ranging from User1 through User7. Each relationship has the type knows.

Create a Neo4j® database connection using the URL http://localhost:7474/db/data, user name neo4j, and password matlab.

url = 'http://localhost:7474/db/data';
username = 'neo4j';
password = 'matlab';

neo4jconn = neo4j(url,username,password);

Check the Message property of the Neo4j® connection object neo4jconn. The blank Message property indicates a successful connection.

neo4jconn.Message
ans =

     []

Create a table for nodes. Define the names variable, which contains the names of three additional people: User8, User9, and User10. Then, define the classification variable to classify each person as Person. Also, define the titles variable, which contains the job title of each person. The first two people are analysts and the third is a technician.

names = {'User8';'User9';'User10'};
classification = {'Person';'Person';'Person'};
titles = {'Analyst';'Analyst';'Technician'};
nodetable = table(names,classification,titles,'VariableNames', ... 
    {'Name','Classification','Title'});

Create a table with two edges. One edge specifies that two people know each other. The other edge specifies that two people work with each other.

edge1 = [1 2];
edge2 = [3 3];
description = {'knows','works with'};
edgetable = table([edge1',edge2'],description', ...
    'VariableNames',{'EndNodes','Description'});

Create a digraph object using the edge and node tables.

G = digraph(edgetable,nodetable);

Store the data in the digraph object as a Neo4j graph in the Neo4j database. Specify the node labels for each node in the resulting Neo4j graph by using the 'NodeLabel' name-value pair argument. The graph uses the Classification and Title variables of the node table for the node labels. Also, the graph uses the Description variable of the edge table for the relationship types.

labels = {'Classification';'Title'};
relation = 'Description';
graphinfo = storeDigraph(neo4jconn,G,'NodeLabel',labels, ...
    'RelationType',relation)
graphinfo = struct with fields:
        Nodes: [3×3 table]
    Relations: [2×5 table]

Display information about the Neo4j graph nodes.

graphinfo.Nodes
ans=3×3 table
          NodeLabels      NodeData                  NodeObject             
          __________    ____________    ___________________________________

    6     {2×1 cell}    [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    52    {2×1 cell}    [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]
    7     {2×1 cell}    [1×1 struct]    [1x1 database.neo4j.http.Neo4jNode]

Nodes is a table that contains these variables:

  • Node label

  • Node data

  • Neo4jNode object

Display information about the Neo4j graph relationships.

graphinfo.Relations
ans=2×5 table
          StartNodeID    RelationType    EndNodeID    RelationData                RelationObject             
          ___________    ____________    _________    ____________    _______________________________________

    17         6         'knows'             7        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]
    35        52         'works with'        7        [1×1 struct]    [1x1 database.neo4j.http.Neo4jRelation]

Relations is a table that contains these variables:

  • Start node identifier

  • Relationship type

  • End node identifier

  • Relationship data

  • Neo4jRelation object

Close the database connection.

close(neo4jconn)

Input Arguments

collapse all

Neo4j database connection, specified as a Neo4jConnect object created with the function neo4j.

Directed graph, specified as a digraph object.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: graphinfo = storeDigraph(neo4jconn,G,'GlobalNodeLabel','Person','GlobalRelationType','knows') stores a directed graph and specifies that all nodes in the resulting graph have the Person node label and all relationships have the knows type.

Note

If you do not specify 'GlobalNodeLabel' or 'NodeLabel', the resulting Neo4j graph contains nodes without labels.

Global node label, specified as the comma-separated pair consisting of 'GlobalNodeLabel' and a character vector, cell array of character vectors, string scalar, or string array. To specify one node label, use a character vector or string scalar. To specify multiple node labels, use a cell array of character vectors or a string array.

After you execute the storeDigraph function, each node in the resulting Neo4j graph contains node labels that you specify using this name-value pair argument.

Example: "Person"

Example: {'Person','Employee'}

Data Types: char | string | cell

Node label, specified as the comma-separated pair consisting of 'NodeLabel' and a character vector, cell array of character vectors, string scalar, or string array. To specify one node label, use a character vector or string scalar. To specify multiple node labels, specify a cell array of character vectors or a string array.

To specify different labels for nodes in the resulting Neo4j graph, use this name-value pair argument. The specified node labels must match the variable names in the table of node information in the digraph object.

Example: "Person"

Example: {'Name','Title'}

Data Types: char | string | cell

Global relationship type, specified as the comma-separated pair consisting of 'GlobalRelationType' and a character vector or string scalar. To specify the same type of relationship for all relationships between nodes in the resulting Neo4j graph, use this name-value pair argument.

Note

When specifying the type of relationship, use either the 'GlobalRelationType' or 'RelationType' name-value pair argument. You cannot specify both of these arguments at the same time.

Example: "knows"

Data Types: char | string

Relationship type, specified as the comma-separated pair consisting of 'RelationType' and a character vector or string scalar. To specify different types of relationships between nodes in the resulting Neo4j graph, use this name-value pair argument. The specified types must match the variable names in the table of edge information in the digraph object.

Note

When specifying the type of relationship, use either the 'RelationType' or 'GlobalRelationType' name-value pair argument. You cannot specify both of these arguments at the same time.

Example: 'Description'

Data Types: char | string

Output Arguments

collapse all

Graph information in the Neo4j database, returned as a structure with these fields.

FieldDescription

Nodes

Table that contains node information for each node in the Relations table. The Nodes table contains these variables:

  • NodeLabels — Character vector that denotes the node label

  • NodeData — Structure array that contains node information such as property keys

  • NodeObjectNeo4jNode object that represents each node

The row names in the table are Neo4j node identifiers.

Relations

Table that contains relationship information for the nodes in the Nodes table. The Relations table contains these variables:

  • StartNodeID — Node identifier for the start node of each relationship

  • RelationType — Character vector that denotes the type of each relationship

  • EndNodeID — Node identifier for the end node of each relationship

  • RelationData — Structure array that contains property keys associated with each relationship

  • RelationObjectNeo4jRelation object that represents each relationship

The row names in the table are Neo4j relationship identifiers.

Tips

  • The storeDigraph function stores all MATLAB® objects as JSON string equivalents in the Neo4j graph. For example, the function stores the date datetime('Jan/01/2017') as "Jan/01/2017" in the Neo4j graph.

Version History

Introduced in R2018a