PackageCn.dh.neo;ImportJava.io.File;Importjava.io.IOException;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportOrg.neo4j.cypher.javacompat.ExecutionEngine;ImportOrg.neo4j.cypher.javacompat.ExecutionResult;ImportOrg.neo4j.graphdb.GraphDatabaseService;ImportOrg.neo4j.graphdb.Node;ImportOrg.neo4j.graphdb.RelationshipType;Importorg.neo4j.graphdb.Transaction;Importorg.neo4j.graphdb.factory.GraphDatabaseFactory;ImportOrg.neo4j.graphdb.index.Index;Importorg.neo4j.kernel.impl.util.FileUtils;/*** * <p>user: Pei Donghui * <p>date:2014-8-6 Morning 10:43:10 * <p>version:1.0*/ Public classNeosql { Public Static enumRelationshiptypesImplementsrelationshiptype {fllow,//User chain singer Publishing albumEMAIL//User-Mailbox album contains many songs } Public FinalString userpk= "Name"; Public FinalString emailpk= "Email"; /*** Initialization of data *@authorPei Donghui *@since2014-8-6 Morning 10:46:18*/ Public voidInitData () {Try{fileutils.deleterecursively (NewFile ("db/user.db"));}Catch(IOException e) {E.printstacktrace ();}//Deleting a databaseGraphdatabaseservice graphdb =NewGraphdatabasefactory (). Newembeddeddatabase ("Db/user.db" ); Try(Transaction tx =Graphdb.begintx ()) { //Node Indexindex<node> Usersindex = Graphdb.index (). Fornodes ("Usersindex"); //-----------------1th User, 1th user's 2 mailboxesNode Node1 =Graphdb.createnode (); Node1.setproperty (USERPK,"Admin"); Usersindex.add (Node1, USERPK, Node1.getproperty (USERPK)); Node node1_1=Graphdb.createnode (); Node1_1.setproperty (EMAILPK,"[Email protected]"); Node1_1.setproperty ("Comment", "work"); Usersindex.add (Node1_1, EMAILPK, Node1_1.getproperty (EMAILPK)); Node node1_2=Graphdb.createnode (); Node1_2.setproperty (EMAILPK,"[Email protected]"); Node1_2.setproperty ("Comment", "Home"); Usersindex.add (Node1_2, EMAILPK, Node1_2.getproperty (EMAILPK)); Node1.createrelationshipto (Node1_1, Relationshiptypes.email); Node1.createrelationshipto (Node1_2, Relationshiptypes.email); //-----------------2nd user, 2nd user's 2 mailboxesNode Node2 =Graphdb.createnode (); Node2.setproperty (USERPK,"User"); Usersindex.add (Node2, USERPK, Node2.getproperty (USERPK)); Node Node2_1=Graphdb.createnode (); Node2_1.setproperty (EMAILPK,"[Email protected]"); Node2_1.setproperty ("Comment", "work"); Usersindex.add (Node2_1, EMAILPK, Node2_1.getproperty (EMAILPK)); Node node2_2=Graphdb.createnode (); Node2_2.setproperty (EMAILPK,"[Email protected]"); Node2_2.setproperty ("Comment", "Home"); Usersindex.add (Node2_2, EMAILPK, Node2_2.getproperty (EMAILPK)); Node2.createrelationshipto (Node2_1, Relationshiptypes.email); Node2.createrelationshipto (Node2_2, Relationshiptypes.email); //----association between two usersNode1.createrelationshipto (Node2, Relationshiptypes.fllow); Tx.success (); }finally{graphdb.shutdown (); } } /*** SELECT * from t_user where name = ' Admin ' * start user=node:usersindex (name = {name}) return user, where {NA Me} passes through the parameters of a map in the past params * 1, SQL starts with the result of Want-we SELECT what we want and then declare how to SOURC E it. In Cypher, the START clause are quite a different concept which specifies starting points in the graph from which the query would execute. 2. From a-a-SQL point of view, the identifiers in START is like table names, which point to a set of nodes or Relat Ionships. The set can listed literally, come via parameters, or as I show in the following example, being defined by an I Ndex look-up. 3. So in fact rather than being select-like, the START clause was somewhere between the From and the WHERE Claus E in SQL. * @authorPei Donghui *@since2014-8-6 Morning 11:08:44*/ Public voidfinduserbyname (String name) {Graphdatabaseservice Graphdb=NewGraphdatabasefactory (). Newembeddeddatabase ("Db/user.db" ); Try(Transaction tx =Graphdb.begintx ()) {Executionengine engine=NewExecutionengine (GRAPHDB); Map<string, object> params =NewHashmap<string, object>(); Params.put ("Name", name); ExecutionResult result= Engine.execute ("Start user=node:usersindex (name = {name}) return user", params); Iterator<Node> Nodeita=result.columnas ("User"); Node Node=NULL; while(Nodeita.hasnext ()) {node=Nodeita.next (); INFOLN (Node+"-"+Node.getproperty (USERPK)); } tx.success (); }finally{graphdb.shutdown (); } } /** * 1, select t_email.* from T_user join t_email on t_user.id = t_email.user_id where t_user.name = ' Admin ' 2, start user=node:usersindex (name = {name}) match User-[:email]->email return Email 3, unlike SQL which operates on sets, Cypher predominantly works on sub-graphs. The relational equivalent is the current set of tuples being evaluated during a SELECT query. 4. The shape of the sub-graph is specified in the MATCH clause. The MATCH clause is analogous to the JOIN in SQL. A normal a→b relationship is an inner join between nodes A and b both sides has to has at least one match, O R nothing is returned. 5, we ' ll start with a simple example, where We find all email addresses that is connected to the person "admin ”. This was an ordinary one-to-many relationship. 6, there is no join table here, but if one is necessary the next exampleWriting the pattern relationship like so:-[r:belongs_to]-> 'll introduce (the E Quivalent of) Join table available asthe variable R. In reality the is a named relationship in Cypher, so we ' re saying "join person to Group viabelongs_to." To illustrate this, consider this image, comparing the SQL model and Neo4j/cypher. *@authorPei Donghui *@since2014-8-6 Morning 11:40:10*/ Public voidfindemailbyusername (String name) {Graphdatabaseservice Graphdb=NewGraphdatabasefactory (). Newembeddeddatabase ("Db/user.db" ); Try(Transaction tx =Graphdb.begintx ()) {Executionengine engine=NewExecutionengine (GRAPHDB); Map<string, object> params =NewHashmap<string, object>(); Params.put ("Name", name); ExecutionResult result= Engine.execute ("Start user=node:usersindex (name = {name}) match User-[:email]->email return EMAIL", params); Iterator<Node> Nodeita=result.columnas ("email"); Node Node=NULL; while(Nodeita.hasnext ()) {node=Nodeita.next (); INFOLN (Node+"-"+Node.getproperty (EMAILPK)); } tx.success (); }finally{graphdb.shutdown (); } } Public voidinfo (Object j) {System.out.print (j.tostring ());} Public voidInfoln (Object j) {System.out.println (j.tostring ());} /** * @authorPei Donghui *@since2014-8-6 a.m. 10:42:54 *@paramargs*/ Public Static voidMain (string[] args) {Newneosql (). InitData (); NewNeosql (). Finduserbyname ("User"); NewNeosql (). Findemailbyusername ("admin"); } }