NEO4J connection Java currently has embedded, JDBC, and rest APIs.
Embedded: Using the Lib package in the neo4j download package lib (for Windows, not recommended to download the EXE version, because does not contain the code required LIB package)
To create nodes and relationships:
FinalString Db_path = "E:/neo4jdb";//Database PathGraphdatabaseservice graphdb =NewGraphdatabasefactory (). Newembeddeddatabase (Db_path);//more time-consuming connectionsTransaction tx = Graphdb.begintx ();//Transactionstx.success (); Node Firstnode,secondnode; Relationship Relationship;firstnode= Graphdb.createnode ();//Create a nodeFirstnode.setproperty ("name", "Node1");//indicates the properties of a nodeSecondnode =Graphdb.createnode (); Secondnode.setproperty ("Name", "Node2"); relationship= Firstnode.createrelationshipto (Secondnode, reltypes.knows);//Create a relationship between two nodesRelationship.setproperty ("Weight", 10);//set the properties of a relationship
If you need to specify a type when creating a node (relationship), you need to declare an enumeration:
Private Static enum Implements RelationshipType // node type and relationship type { KNOWS }
Querying data using cypher
New Executionengine (this = Engine.execute ("Match n return n.name as name;" ); Resourceiterator<String> iterator = Result.columnas ("name"); // get a column value, if you do not use as XXX, the value should be used N.name while (Iterator.hasnext ()) { System.out.println (Iterator.next ()); }
Query two direct shortest path: Currently embedded provides a variety of algorithms to calculate the shortest path, but at present only looked at Shorestpath and Dijkstra these two algorithms, Shorestpath calculate the number of nodes between two nodes the smallest path (regardless of the weight between the nodes), Dijkstra calculates the path with the least weight between two nodes.
//the shortest path between nodes, calculates the path with the smallest number of nodes between two points, and the weights of unrelated relationshipspathfinder<path> finder = Graphalgofactory.shortestpath (Traversal.expanderfortypes (RelTypes.KNOWS, direction.outgoing), 5); Node Startnode= This. Graphdb.getnodebyid (14); Node Endnode= This. Graphdb.getnodebyid (17); //query all the paths that pass through the same number of nodesiterable<path> paths =finder.findallpaths (Startnode, Endnode); Iterator<Path> iterator =Paths.iterator (); Path p; while(Iterator.hasnext ()) {p=Iterator.next (); System.out.println ("Shortestpath" +p.tostring ()); System.out.println (P.startnode (). GetId ()+ "\ T" +P.endnode (). GetId ()); } //take the first one in all pathsp =Finder.findsinglepath (Startnode, Endnode); System.out.println ("Shorestpath Singlepath:" +p.tostring ()); PathFinder<WeightedPath> Finder1 =Graphalgofactory.dijkstra (Traversal.expanderfortypes (Reltypes.knows, Direction.both),"Weight" ); Weightedpath Path=Finder1.findsinglepath (Startnode, Endnode); //Get The weight for the found pathSystem.out.println (Path.weight ()); System.out.println (Path.tostring ());
Java connection neo4j embedded inside