20
|
1 package wikigraph;
|
|
2
|
|
3 import java.util.HashMap;
|
21
|
4 import java.util.Map;
|
20
|
5
|
|
6 import org.neo4j.graphdb.GraphDatabaseService;
|
|
7 import org.neo4j.graphdb.Node;
|
21
|
8 import org.neo4j.graphdb.Path;
|
20
|
9 import org.neo4j.graphdb.Relationship;
|
|
10 import org.neo4j.graphdb.Transaction;
|
21
|
11 import org.neo4j.graphdb.traversal.Traverser;
|
20
|
12 import org.neo4j.kernel.EmbeddedGraphDatabase;
|
21
|
13 import org.neo4j.tooling.GlobalGraphOperations;
|
|
14
|
|
15 import wikigraph.LinkToNode.RelTypes;
|
20
|
16
|
|
17 public class ReadWikiLink {
|
|
18
|
|
19 public static void main(String[] args) {
|
|
20
|
|
21 GraphDatabaseService graphDb = new EmbeddedGraphDatabase("wikiLinkDB");
|
23
|
22 GlobalGraphOperations graphOpe = GlobalGraphOperations.at(graphDb);
|
21
|
23 LinkToNode ltn = new LinkToNode(graphDb, graphOpe);
|
23
|
24 Transaction tx = graphDb.beginTx();
|
21
|
25
|
|
26 try {
|
23
|
27
|
|
28 // final long AllNodeNumber = ltn.searchPageTitleAllNodes();
|
|
29 final long AllNodeNumber = ltn.searchAllNodes();
|
|
30
|
|
31 long maxRelCount = 0;
|
|
32 String tmpKey = "";
|
|
33 // relHash record number of relationships of each node.
|
|
34 // Key: page_title value: number of relationships
|
|
35 HashMap<String, Long> relHash = new HashMap<String, Long>();
|
|
36
|
|
37 // relKeyHash
|
|
38 // key: number of relationships value: ID
|
|
39 HashMap<Long, Long> relKeyHash = new HashMap<Long, Long>();
|
|
40
|
|
41 HashMap<String,WikiPage> wikiHash = ltn.getWikiPageHash();
|
|
42
|
|
43 for (String title: wikiHash.keySet()) {
|
|
44 WikiPage w = wikiHash.get(title);
|
|
45 long id = w.getId();
|
|
46
|
|
47 Node node = graphDb.getNodeById(id);
|
|
48
|
|
49 Iterable<Relationship> relIter = node.getRelationships();
|
|
50 long count = 0;
|
|
51 // compute number of relationship.
|
|
52 for (Relationship rel : relIter) {
|
|
53 if (ltn.isHasLink(rel))
|
|
54 count++;
|
|
55 }
|
|
56 w.setOutLink(count);
|
|
57
|
|
58 relHash.put(title, count);
|
|
59
|
|
60 if (maxRelCount < count) {
|
|
61 maxRelCount = count;
|
|
62 tmpKey = title;
|
|
63 }
|
|
64 relKeyHash.put(count, id);
|
|
65 }
|
20
|
66
|
23
|
67 for (String title: wikiHash.keySet()) {
|
|
68 System.out.println(title);
|
20
|
69 }
|
23
|
70
|
|
71 System.out.println("AllNodeNumber = " + AllNodeNumber);
|
|
72 System.out.println("Most :\n" + tmpKey + ":" + maxRelCount);
|
20
|
73
|
23
|
74 String output = "";
|
|
75 Node n = graphDb.getNodeById(1);
|
|
76 Traverser hasLinkTraverser = ltn.getInHasLink(n);
|
|
77 System.out.println(n.getProperty(LinkToNode.PAGE_TITLE));
|
|
78 for (Path hasLinkPath : hasLinkTraverser) {
|
|
79 if (hasLinkPath.length() > 1) break;
|
|
80 output = "At depth"
|
|
81 + hasLinkPath.length()
|
|
82 + " => "
|
|
83 + hasLinkPath.endNode().getProperty(
|
|
84 LinkToNode.PAGE_TITLE) + "\n";
|
|
85 System.out.println(output);
|
|
86 }
|
|
87
|
21
|
88 System.out.println(output);
|
|
89
|
|
90 } catch (Exception e) {
|
|
91 e.printStackTrace();
|
|
92 } finally {
|
|
93 tx.success();
|
|
94 tx.finish();
|
|
95 graphDb.shutdown();
|
|
96 }
|
23
|
97
|
20
|
98 }
|
23
|
99
|
20
|
100 }
|