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
|
|
20 public static void main(String[] args) {
|
|
21
|
|
22
|
|
23 GraphDatabaseService graphDb = new EmbeddedGraphDatabase("wikiLinkDB");
|
21
|
24 GlobalGraphOperations graphOpe = GlobalGraphOperations.at(graphDb);
|
|
25 LinkToNode ltn = new LinkToNode(graphDb, graphOpe);
|
20
|
26 Transaction tx = graphDb. beginTx();
|
21
|
27
|
|
28 try {
|
|
29
|
|
30 final long AllNodeNumber = ltn.searchPageTitleAllNodes();
|
20
|
31
|
21
|
32 // Key: page_title value: Node ID
|
|
33 HashMap<String,Long> pageIdHash = ltn.getPageIdTable();
|
|
34 HashMap<String,Long> pageRankHash = ltn.getPageRankTable();
|
20
|
35
|
21
|
36 long maxRelCount = 0;
|
20
|
37 String tmpKey="";
|
|
38 // relHash record number of relationships of each node.
|
|
39 // Key: page_title value: number of relationships
|
|
40 HashMap<String,Long> relHash = new HashMap<String,Long>();
|
|
41
|
|
42 // relKeyHash
|
|
43 // key: number of relationships value: ID
|
|
44 HashMap<Long,Long> relKeyHash = new HashMap<Long,Long>();
|
|
45
|
21
|
46 for (String key : pageIdHash.keySet()) {
|
|
47 long id = pageIdHash.get(key);
|
20
|
48 Node node = graphDb.getNodeById(id);
|
|
49
|
|
50 Iterable<Relationship> relIter = node.getRelationships();
|
|
51 long count = 0;
|
|
52 for (Relationship rel : relIter) {
|
21
|
53 if ( ltn.isHasLink(rel))
|
|
54 count++;
|
20
|
55 }
|
|
56 relHash.put(key, count);
|
|
57
|
21
|
58 if (maxRelCount < count ) {
|
|
59 maxRelCount = count;
|
20
|
60 tmpKey = key;
|
|
61 }
|
|
62 relKeyHash.put(count, id);
|
|
63 }
|
|
64
|
21
|
65 System.out.println("AllNodeNumber = "+ AllNodeNumber);
|
|
66 System.out.println("Most :\n"+tmpKey+":"+maxRelCount);
|
20
|
67
|
|
68
|
21
|
69
|
|
70 String output = "";
|
|
71 Node n = graphDb.getNodeById(1);
|
|
72 Traverser hasLinkTraverser = ltn.getInHasLink( n );
|
|
73 System.out.println(n.getProperty(LinkToNode.PAGE_TITLE));
|
|
74 for (Path hasLinkPath : hasLinkTraverser) {
|
|
75 output = "At depth" + hasLinkPath.length() + " => "
|
|
76 + hasLinkPath.endNode().getProperty(LinkToNode.PAGE_TITLE) + "\n";
|
|
77 System.out.println(output);
|
20
|
78 }
|
|
79
|
21
|
80 System.out.println(output);
|
|
81
|
|
82 } catch (Exception e) {
|
|
83 e.printStackTrace();
|
|
84 } finally {
|
|
85 tx.success();
|
|
86 tx.finish();
|
|
87 graphDb.shutdown();
|
|
88 }
|
20
|
89
|
|
90 }
|
|
91
|
|
92 }
|