Convert graph data to Json
我正在尝试开发一个小程序来表示网站页面之间的链接。我想用关系表示每个链接,用节点表示每个页面。
目前我使用的是一个小数据集,它看起来像:
1 2 | HOME -> PAGE 1 -> PAGE 3 -> PAGE 5 -> PAGE 2 -> PAGE 4 |
在插入了所有节点和关系之后,我希望遍历我的图并在JSON中打印数据,以获得如下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | { "name":"HOME", "children": [ { "name":"PAGE 1", "children": [ { "name":"PAGE 3", "children": [ {"name":"PAGE 5"} ] }, { "name":"PAGE 2", "children": [ {"name":"PAGE 4"} ] } ] } ] } |
号
是否有任何函数执行此操作,或者我必须自己编写JSON?
这是我的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 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" ) +" "; } System.out.println(output); } finally { tx.finish(); } } |
页面类代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public class Page implements Serializable{ private static final long serialVersionUID = 1L; static final String URL ="url"; private final Node underlyingNode; private List<Page> 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<Page>(); } 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() +"]"; } |
。
假设您有一个bean代表您的页面,如下所示:
1 2 3 4 5 6 7 | public class Page implements Serializable { private String name; private List<Page> children; private transient GraphDatabaseService dbService; // Constructors, Getters/Setters } |
您可以使用各种各样的JSON库(如Jackson或GSON)轻松地将其序列化。下面是一个简单的杰克逊例子:
1 2 3 | final Page home ; // Initialize and construct the home page final ObjectMapper mapper = new ObjectMapper(); final String json = mapper.writeValueAsString(home); |
号
我认为最有效的格式是类似于node和edgelist的格式,有关csv版本,请参见https://github.com/jexp/batch-import,对于javascript,d3执行类似的操作,请参见json中读取的d3示例。
彼得