neo4j simple learning

background

In some forums or news recently saw a Neo4j , good database for processing graphics. It is said that is well suited to do some join relational query, so taking the time to also looked under the relevant documents, give yourself to be a technical reserve.

 

process

Before delving into the study, the first on the Internet to find a bit of a learning document summarizes the others, standing on the shoulders of others is always the fastest, most effective learning.

 

Along these lines, progressive view of some neo4j wiki documentation, and a map excerpts: 

FIG neo4j basic model: 




For the figure of some basic concepts: 
  • node: Node
  • relationships: relationship, that is, edges in the graph, attention is directed edges
  • properties: property, for node / relationship can be set up property
  • Traversal: graph traversal tool
  • Indexes: Index
By node relationship and we can form a directed graph, using the property so that it can bring the corresponding data, to become an image corresponding database.

node (Node)

  1. Each node can have multiple relationships (relationship) between a plurality of nodes and
  2. Single node may be provided a plurality of keys (Key, Value) of the attribute properties

relationships (relationship)

  1. Each relationship will include a startNode and endNode
  2. Each relationship can be provided a plurality of keys (Key, Value) of the attribute properties
  3. May be defined corresponding relationship between the type of relation (RelationshipType)
    * DynamicRelationshipType dynamic relationship type
    * XXXRelationshipType static relationship types (RelationshipType implements interface)

Traversal (traversing)

cross:   http://wiki.neo4j.org/content/Traversal

one example: 

 

Java code   Collection Code
  1. Traverser trav = swedenNode.traverse(Order.DEPTH_FIRST, StopEvaluator.END_OF_GRAPH,  
  2.     new ReturnableEvaluator()  
  3.     {  
  4.         public boolean isReturnableNode( TraversalPosition pos )  
  5.         {  
  6.             return !pos.isStartNode() && pos.lastRelationshipTraversed().isType( CUSTOMER_TO_ORDER );  
  7.         }  
  8.     },  
  9.     LIVES_IN, Direction.INCOMING,  
  10.     CUSTOMER_TO_ORDER, Direction.OUTGOING );  
  11. // iterate over traverser...<span style="white-space: normal;">  
  12. </span>  

 

Order: traversal algorithm corresponding to FIG.

 

  • DEPTH_FIRST: depth-first search is to find the first node, recursive been looking down until after no suitable node before backtracking
  • BREADTH_FIRST: BFS
Direction: the direction of the corresponding edge of the drawing
  • OUTGOING: the edge
  • INCOMING: the edge
  • BOTH: Guming Si Yee
StopEvaluator: Stop defined graph search terms, there are two default
  • DEPTH_ONE: After more than one depth stop
  • END_OF_GRAPH: no suitable results and stop
ReturnableEvaluator: results of the processor, may be provided to return a corresponding result, there is a default:
  • ALL_BUT_START_NODE: exclude initial node
  • ALL: return all nodes
TraversalPosition: node information corresponding to the node in the search process, comprising:
  • A node information
  • Information on an incoming Relationship
  • Search Depth
  • So far the number of nodes that satisfy the condition

Indexs (index)

are stored in neo4j independently for each node / relationship / property, it is the natural order. In order to support some scenarios, such as for relational database queries based on the primary key name corresponding to the person node, ordinary Traversal difficult to meet such a demand, and people are not used to solve this thing. Therefore neo4j it leads to the concept of an index. 

 

Early versions of the index is to use a IndexService ( http://wiki.neo4j.org/content/Indexing_with_IndexService )

one example:

 

Java code   Collection Code
  1. GraphDatabaseService graphDb = new EmbeddedGraphDatabase( "path/to/neo4j-db" );  
  2. IndexService index = new LuceneIndexService( graphDb );  
  3.   
  4. Node andy = graphDb.createNode();  
  5. Node larry = graphDb.createNode();  
  6.   
  7. andy.setProperty( "name""Andy Wachowski" );  
  8. andy.setProperty( "title""Director" );  
  9. larry.setProperty( "name""Larry Wachowski" );  
  10. larry.setProperty( "title""Director" );  
  11. index.index( andy, "name", andy.getProperty( "name" ) );  
  12. index.index( andy, "title", andy.getProperty( "title" ) );  
  13. index.index( larry, "name", larry.getProperty( "name" ) );  
  14. index.index( larry, "title", larry.getProperty( "title" ) );  

 

IndexService as an external component is extended definition. 

 

Now the official documentation is recommended to use Integrated Index Framework

 

  1. The official document:  http://docs.neo4j.org/chunked/stable/indexing.html
  2. Migration scenarios:  http://wiki.neo4j.org/content/Transitioning_To_Index_Framework
Examples of the new version: 
Java code   Collection Code
  1. IndexManager index = graphDb.index();  
  2. Index<Node> actors = index.forNodes( "actors" );  
  3. Index<Node> movies = index.forNodes( "movies" );  
  4. RelationshipIndex roles = index.forRelationships( "roles" );  
The new version, the index has been done to achieve its core, not the mechanism for external expansion pack, so its importance Ah, want to know the specific content can be detailed look at the corresponding official documents.

Query Syntax (Cyphe Query Language)

neo4j own search algorithm based on graph theory, to achieve a set of query language parsing, provides some common aggregate functions (max, sum, min, count, etc.).

 

Syntax example:

 

Java code   Collection Code
  1. Join query:  
  2. start n=(1) match (n)-[:BLOCKS]->(x) return x  
  3.   
  4. Where conditions:  
  5. start n=(21) where (n.age < 30 and n.name = "Tobias") or not(n.name = "Tobias"return n  
  6.   
  7. Aggregation function:  
  8. start n=(2,3,4return avg(n.property)  
  9.   
  10. Order:  
  11. start n=(1,2,3return n order by n.name DESC  
  12.   
  13. Paging:  
  14. start n=(1,2,3,4,5return n order by n.name skip 1 limit 2  

 

 

Call example:

 

Java code   Collection Code
  1. db = new ImpermanentGraphDatabase();  
  2. engine = new ExecutionEngine( db );  
  3. CypherParser parser = new CypherParser();  
  4. ExecutionEngine engine = new ExecutionEngine(db);  
  5. Query query = parser.parse( "start n=(0) where 1=1 return n" );  
  6. ExecutionResult result = engine.execute( query );  
  7. assertThat( result.columns(), hasItem( "n" ) );  
  8. Iterator<Node> n_column = result.columnAs( "n" );  
  9. assertThat (asIterable (n_column), hasItem (db.getNodeById ( 0 )));  
  10. assertThat( result.toString(), containsString("Node[0]") );  

 

other

Now with nosql, except for some feature functionality problems, it is important to be concerned about the other two points Scalability & Availability

Expansibility

Not yet see a corresponding expansion of the program

Availability (HA mechanism)

Ha neo4j currently support a simple mechanism, it is managed by the zookeeper.


 

Its mechanism is very simple, is responsible for neo4j server heartbeat detection by the zookeeper.

1. Discover hung up after the master, initiates an election (not seen the source code, reckoned to achieve the election will be very simple, according to the corresponding serverid, take the smallest id as the new master).

2. Insert the new broadcast to all slave master, this time in the electoral process, does not accept the write request corresponding to the (all returns an exception)
3. The new machine joins the cluster, as a slave to the master will communicate, synchronize two data content provider (if the current slave of the new master tid ratio will produce a data conflict requiring a manual intervention)

 

Problems:

Timeliness 1. zookeeper heartbeat detection, the default delay of three minutes (because there packet retry)

2. During the master election, write request can not be processed directly returns an exception (although master of timing of the elections will be relatively end, but the client is not friendly enough)

 

Points for improvement:

1. To provide the client api, provide a mechanism for controlling one failover retry.

Console page

neo4j supports two modes of embedded and standalone deployment, deployed at neo4j independent deployment server, the effect is as follows:

 

Graphics management background, you can see aspects of relationships between nodes

 

 

rest interface api, graphics and offers several ways pure data:

Other documents

 

  • 大小: 42.7 KB
  • 大小: 138.5 KB
  • 大小: 43.8 KB
  • 大小: 35.9 KB
Original Address: https: //www.iteye.com/blog/agapple-1128400

Guess you like

Origin www.cnblogs.com/jpfss/p/11490535.html