Java connection neo4j embedded inside

Source: Internet
Author: User
Tags neo4j

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.