Main Content

Update Friend Information in Social Neighborhood

This example shows how to create, update, and delete information in a social neighborhood, which is represented by a Neo4j® database, using the MATLAB® interface to Neo4j.

For details about the MATLAB interface to Neo4j, seeGraph Database Workflow for Neo4j Database Interfaces.

Assume that you have graph data stored in a Neo4j database that represents the social neighborhood. This database has seven nodes and eight relationships. Each node has only one unique property keynamewith a value ranging fromUser1throughUser7. Each relationship has the typeknows.

The local machine hosts the Neo4j database with the port number7474, user nameneo4j, and passwordmatlab. This figure provides a visual representation of the data in the database.

Connect to Neo4j Database

Create a Neo4j connection objectneo4jconnusing the URLhttp://localhost:7474/db/data, user nameneo4j, and passwordmatlab.

url ='http://localhost:7474/db/data'; username ='neo4j'; password ='matlab'; neo4jconn = neo4j(url,username,password);

Check theMessageproperty of the Neo4j connection objectneo4jconn. The blankMessageproperty indicates a successful connection.

neo4jconn.Message
ans = []

Add Two Friends to Social Neighborhood

Create two nodes in the database using the Neo4j database connection. Use the'Labels'name-value pair argument to specify the labelPersonfor each node.

label ='Person'; user8 = createNode(neo4jconn,'Labels',label); user9 = createNode(neo4jconn,'Labels',label);

Search for the node with the labelPersonand the property keynameset to the valueUser7by using the Neo4j database connection.

nlabel ='Person'; user7 = searchNode(neo4jconn,nlabel,'PropertyKey','name',...'PropertyValue','User7');

Create two relationships using the Neo4j database connection. Specify the relationship types asworks withandstudies with. The two relationships are:

  • User8works withUser7

  • User8studies withUser9

relationinfois a table that contains the relationship and node information.

startnode = [user8,user8]; endnode = [user7,user9]; relationtype = {'works with','studies with'}; relationinfo = createRelation(neo4jconn,startnode,endnode,relationtype);

Update Node Information for Added Friend

Update the properties of the nodeUser8. Create a table with one row that contains the name and job title for this person.nodeinfois aNeo4jNodeobject.

properties = table("User8","Analyst",'VariableNames',{'Name','Title'}); nodeinfo = setNodeProperty(neo4jconn,user8,properties);

Add the node labelStudenttoUser9.

labels ='Student'; nodeinfo = addNodeLabel(neo4jconn,user9,labels);

Update Relationship Information for Added Friends

Create a table that defines the relationship properties. Here,User8works withUser7在工作场所,User8studies withUser9in the library. Also,User8started working withUser7on January 2, 2017, andUser8started studying withUser9on March 6, 2017.

properties = table(["Workplace";"Library"],["01/02/2017";"03/06/2017"],...'VariableNames',{'Location','Date'});

Update both relationships with these properties.relationinfois a table that contains the updated relationships.

relations = relationinfo.RelationObject; relationinfo = setRelationProperty(neo4jconn,relations,properties);

Delete Relationship for Added Friend

Delete the relationship that connectsUser8toUser7.

relation = relations(1); deleteRelation(neo4jconn,relation)

Delete Friends

Delete the added nodes and any associated relationships.

nodes = [user8,user9]; deleteNode(neo4jconn,nodes,'DeleteRelations',真正的)

Close Database Connection

close(neo4jconn)

See Also

|||||||||

Related Topics

External Websites