neo4j入门
neo4j是一个nosql的图形数据库。nosql的意思是说不支持sql。图形数据库是说用graph作为建模工具。我们知道关系型数据采用二维表作为建模工具,一些nosql数据库使用hash表,有的使用json。neo4j颠覆传统,使用graph。众所周知,图上的数据操作一般有:
- 1. 添加 删除节点;
2. 添加删除 边;
3. 深度优先遍历;
4. 广度优先遍历;
5. 最短路径;
6. 最小生成子树。
当然还有别的算法。neo4j的边是两个节点关系,边上有属性,可以存储数据。可以使用java代码插入数据,如下所示:
firstNode = graphDb.createNode();
firstNode.setProperty( "message", "Hello, " );
secondNode = graphDb.createNode();
secondNode.setProperty( "message", "World!" );
relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
relationship.setProperty( "message", "brave Neo4j " );
neo4j还支持事务的插入和回滚。
图形数据库可以使用cypher语言进行查询,它具有如下的元素:
START: Starting points in the graph, obtained via index lookups or by element IDs.MATCH: The graph pattern to match, bound to the starting points inSTART.WHERE: Filtering criteria.RETURN: What to return.CREATE: Creates nodes and relationships.DELETE: Removes nodes, relationships and properties.SET: Set values to properties.FOREACH: Performs updating actions once per element in a list.WITH: Divides a query into multiple, distinct parts.
使用cypher语言可以很容易的创建图,修改图和查询图。
图形数据库很适合哪些本质上是图的数据模型,不然人与人之间的关系,或者一个项目的各个环节,或者一个数据流任务的节点。
浙公网安备 33010602011771号