Main Content

Explore Graph Database Structure

This example shows how to traverse a graph and explore its structure using the MATLAB® interface to Neo4j®. For details about the MATLAB interface to Neo4j, see Graph Database Workflow for Neo4j Database Interfaces.

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.

The local machine hosts the Neo4j database with the port number 7474, user name neo4j, and password matlab. This figure provides a visual representation of the data in the database.

Connect to Neo4j Database

Create a Neo4j connection object neo4jconn 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 =

     []

Explore Structure of Entire Graph

Find all the node labels in the Neo4j database using the Neo4j connection object neo4jconn.

nlabels = nodeLabels(neo4jconn)
nlabels = 1×1 cell array
    {'Person'}

Find all the relationship types in the Neo4j database.

reltypes = relationTypes(neo4jconn)
reltypes = 1×1 cell array
    {'knows'}

Find the property keys in the Neo4j database.

propkeys = propertyKeys(neo4jconn)
propkeys = 15×1 cell array
    {'Name'       }
    {'property'   }
    {'title'      }
    {'Description'}
    {'EndNodes'   }
    {'Location'   }
    {'EndDate'    }
    {'Address'    }
    {'Project'    }
    {'Department' }
    {'StartDate'  }
    {'Title'      }
    {'Date'       }
    {'Weight'     }
    {'name'       }

Search for Nodes

Search for all the nodes with the node label Person. The nodesinfo output argument contains node labels, node data, and the Neo4jNode objects for each matched node.

nlabel = 'Person';

nodesinfo = searchNode(neo4jconn,nlabel)
nodesinfo=7×3 table
         NodeLabels      NodeData                  NodeObject             
         __________    ____________    ___________________________________

    0     '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]

Search for the node with the node identifier 2. The nodeinfo output argument contains the node identifier, node data, and node labels for the node with node identifier 2.

nodeid = 2;

nodeinfo = searchNodeByID(neo4jconn,nodeid)
nodeinfo = 
  Neo4jNode with properties:

        NodeID: 2
      NodeData: [1×1 struct]
    NodeLabels: 'Person'

Search for Relationships

Search for incoming relationship types that belong to the node nodeinfo.

nodereltypes = nodeRelationTypes(nodeinfo,'in')
nodereltypes = 1×1 cell array
    {'knows'}

Search for the degree of all incoming relationships that belong to the node nodeinfo.

degree = nodeDegree(nodeinfo,'in')
degree = struct with fields:
    knows: 1

Search for the relationship with the node identifier 4.

relationid = 4;

relationinfo = searchRelationByID(neo4jconn,relationid)
relationinfo = 
  Neo4jRelation with properties:

      RelationID: 4
    RelationData: [1×1 struct]
     StartNodeID: 3
    RelationType: 'knows'
       EndNodeID: 5

Search for all incoming relationships that belong to the node nodeinfo. The relinfo output argument contains data about the start and end nodes and all matched relationships from the origin node.

relinfo = searchRelation(neo4jconn,nodeinfo,'in')
relinfo = struct with fields:
       Origin: 2
        Nodes: [2×3 table]
    Relations: [1×5 table]

Retrieve Entire Graph

Retrieve the entire graph using node labels nlabels.

graphinfo = searchGraph(neo4jconn,nlabels)
graphinfo = struct with fields:
        Nodes: [7×3 table]
    Relations: [8×5 table]

graphinfo contains node data for all start and end nodes for each matched relationship. graphinfo also contains relationship data for each matched relationship.

Close Database Connection

close(neo4jconn)

See Also

| | | | | | | | |

Related Topics

External Websites