MATLAB Examples

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 docid:database_ug.bveys16-1.

Assume that you have graph data that is stored on a Neo4j® database which represents a social neighborhood. This database has seven nodes and eight relationships. Each node has only one unique property key name with values User1 through User7. Each relationship has type knows.

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

Contents

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.

neo4jconn.Message
ans =

     []

The blank Message property indicates a successful connection.

Explore Structure of Entire Graph

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

nlabels = nodeLabels(neo4jconn)
nlabels =

  cell

    'Person'

Find all the relationship types in the Neo4j® database.

reltypes = relationTypes(neo4jconn)
reltypes =

  cell

    'knows'

Find the property keys in the Neo4j® database.

propkeys = propertyKeys(neo4jconn)
propkeys =

  2×1 cell array

    'name'
    'property'

Search for Nodes

Search for all the nodes with the node label Person.

nlabel = 'Person';

nodesinfo = searchNode(neo4jconn,nlabel)
nodesinfo = 

         NodeLabels      NodeData                NodeObject          
         __________    ____________    ______________________________

    0    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]
    1    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]
    2    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]
    3    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]
    4    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]
    5    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]
    6    'Person'      [1×1 struct]    [1x1 database.neo4j.Neo4jNode]

nodesinfo contains node labels, node data, and the Neo4jNode objects for each matched node.

Search for the node that has the node identifier 2.

nodeid = 2;

nodeinfo = searchNodeByID(neo4jconn,nodeid)
nodeinfo = 

  Neo4jNode with properties:

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

nodeinfo contains the node identifier, node data, and node labels for the node with node identifier 2.

Search for Relationships

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

nodereltypes = nodeRelationTypes(nodeinfo,'in')
nodereltypes =

  cell

    '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 all incoming relationships that belong to the node nodeinfo.

relinfo = searchRelation(neo4jconn,nodeinfo,'in')
relinfo = 

  struct with fields:

       Origin: 2
        Nodes: [2×3 table]
    Relations: [1×4 table]

relinfo contains data about the starting and ending nodes and all matched relationships from the origin node.

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×4 table]

graphinfo contains node data for all starting and ending nodes for each matched relationship. graphinfo also contains relationship data for each matched relationship.