实践Neo4j之三模型和建模

Node

Vertice,顶点,节点

1
2
3
Node user1 = graphDb.createNode();
Node user2 = graphDb.createNode();
Node movie1 = graphDb.createNode();

Node

id是neo4j自动生成的

Label

可以理解为Node Type

1
2
3
public interface Label {
	java.lang.String name();
}
1
2
3
public enum MyLabels implements Label{
	MOVIES, USERS
}
1
movie1.addLabel(MyLabels.MOVIE);

DynamicLabel

动态创建Label

1
Label label1 = DynamicLabel.label("MOVIES");

RelationShip

Edge,关系,边

RelationShip

id是neo4j自动生成的

RelationShipType

1
2
3
public interface RelationshipType{
public String name();
}
1
2
3
4
public enum MyRelationshipTypes implements RelationshipType {
    IS_FRIEND_OF,
    HAS_SEEN;
}
1
Relationship rel1 = user1.createRelationshipTo(movie1, MyRelationshipTypes.HAS_SEEN);

DynamicRelationshipType

动态创建RelationShipType

1
RelationshipType rt1 = DynamicRelationshipType.withName("IS_FRIEND_OF");

Properties

1
2
user1.setProperty("name", "John Johnson");
rel1.setProperty("stars", 4);

在进行图建模时,对于实体、关系和属性的使用要适当权衡。该用属性表示的地方如果硬拆出来用实体表示,则可能会引入过多的实体和关系。一些复杂的场景下难以表达清楚,可能需要额外引入实体和关系。