Note that there are some explanatory texts on larger screens.

plurals
  1. POConvert graph data to Json
    primarykey
    data
    text
    <p>I'm trying to develop a little program representing link between page on a website. I want to represent each link by a relationship, and each page by a node.</p> <p>For now i'm using a little data set, and it's look like :</p> <pre><code>HOME -&gt; PAGE 1 -&gt; PAGE 3 -&gt; PAGE 5 -&gt; PAGE 2 -&gt; PAGE 4 </code></pre> <p>After inserting all my node and relation I want to traverse my graph and print data in Json to have something like : </p> <pre><code>{ "name": "HOME", "children": [ { "name": "PAGE 1", "children": [ { "name": "PAGE 3", "children": [ {"name": "PAGE 5"} ] }, { "name": "PAGE 2", "children": [ {"name": "PAGE 4"} ] } ] } ] } </code></pre> <p>Is there any function doing this, or I have to write json by my own ?</p> <p>Here is my code : </p> <pre><code>private static void routing( final GraphDatabaseService graphDb ) { Transaction tx = graphDb.beginTx(); Page home, p1, p2, p3, p4, p5; try { home = new Page(graphDb, "http://www.site.com/"); p1 = new Page(graphDb, "http://www.site.com/page1.html"); p2 = new Page(graphDb, "http://www.site.com/page2.html"); p3 = new Page(graphDb, "http://www.site.com/page3.html"); p4 = new Page(graphDb, "http://www.site.com/page4.html"); p5 = new Page(graphDb, "http://www.site.com/page5.html"); home.createLinkTo(p1); home.createLinkTo(p2); p1.createLinkTo(p3); p2.createLinkTo(p4); p3.createLinkTo(p5); tx.success(); tx = graphDb.beginTx(); final TraversalDescription linkTraversal = Traversal.description().depthFirst().relationships( RelationshipTypes.LINK ); String output = ""; for ( Node node : linkTraversal.traverse(home.getUnderlyingNode()).nodes() ) { output += node.getProperty( "url" ) + "\n"; } System.out.println(output); } finally { tx.finish(); } } </code></pre> <p>Code of Page class</p> <pre><code>public class Page implements Serializable{ private static final long serialVersionUID = 1L; static final String URL = "url"; private final Node underlyingNode; private List&lt;Page&gt; children = null; public Page( final Node node ) { this.underlyingNode = node; } public Page( final GraphDatabaseService graphDb, final String url ) { this.underlyingNode = graphDb.createNode(); underlyingNode.setProperty( URL, url ); children = new ArrayList&lt;Page&gt;(); } public Node getUnderlyingNode() { return underlyingNode; } public String getUrl() { return (String) underlyingNode.getProperty( URL ); } public void createLinkTo( final Page other ) { Relationship link = underlyingNode.createRelationshipTo(other.underlyingNode, RelationshipTypes.LINK ); children.add(other); //link.setProperty( ANCHOR, 'Mon ancre' ); } @Override public String toString() { return "Page [url=" + getUrl() + "]"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload