forked from krlawrence/graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoteStats.java
More file actions
166 lines (122 loc) · 6.43 KB
/
Copy pathRemoteStats.java
File metadata and controls
166 lines (122 loc) · 6.43 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// RemoteStats.java
//
// Simple example of using GremlinServer from a Java client
// This code performs the same queries that GraphStats.java does
// but to a remote endpoint rather than a local in-memory graph.
//
// This example does the following:
// 1. Configure a new Cluster object
// 2. Use that cluster to connect to a Gremlin Server
// 3. Generate some interesting statistics about the data in the graph.
import org.apache.tinkerpop.gremlin.driver.Cluster;
import org.apache.tinkerpop.gremlin.driver.remote.DriverRemoteConnection;
import org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0;
import org.apache.tinkerpop.gremlin.process.traversal.*;
import org.apache.tinkerpop.gremlin.process.traversal.Path;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.io.IoCore;
import org.apache.tinkerpop.gremlin.structure.util.empty.EmptyGraph;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
// Using a static import avoids needing to use the "__." prefix as in "__.out()"
import static org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__.*;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
public class RemoteStats
{
public static void main( String[] args )
{
Cluster.Builder builder = Cluster.build();
builder.addContactPoint("localhost");
builder.port(8182);
builder.serializer(new GryoMessageSerializerV1d0());
Cluster cluster = builder.create();
GraphTraversalSource g =
EmptyGraph.instance().traversal().
withRemote(DriverRemoteConnection.using(cluster));
// Run some queries and display a few statistics
System.out.println("Collecting stats");
System.out.println("\nDistribution of vertices and edges");
System.out.println("----------------------------------");
// Display some basic demographics
// Note that label has to be prefixed by "T."
Map verts = g.V().groupCount().by(T.label).next();
Map edges = g.E().groupCount().by(T.label).next();
System.out.println("Vertices : " + verts);
System.out.println("Edges : " + edges);
// Find the airport with the most overall routes.
Map <String,?> most = g.V().hasLabel("airport").
order().by(bothE("route").count(),Order.decr).limit(1).
project("ap","num","city").by("code").by(bothE("route").count()).
by("city").next();
String s = "" + most.get("ap") + "/" + most.get("city");
Long r = (Long) most.get("num");
System.out.println("\nThe airport with the most routes is " + s + " with " + r + " routes") ;
// Find the airports with the most routes overall (incoming + outgoing)
System.out.println("\nTop 20 airports by total routes");
System.out.println("===============================");
List<Map<String,Object>> top =
g.V().hasLabel("airport").
order().by(both("route").count(),Order.decr).limit(20).
project("ap","num","city").by("code").by(both("route").count()).by("city").
toList();
// Either of these can be used
//for(Map a: top) { System.out.format("%4s %12s %4d\n",a.get("ap"),a.get("city"), a.get("num"));}
top.forEach((a) -> System.out.format("%4s %12s %4d\n",a.get("ap"),a.get("city"),a.get("num")));
// Find the airports with the most outgoing routes
System.out.println("\nTop 20 airports by outgoing routes");
System.out.println("==================================");
List<Map<String,Object>> topout =
g.V().hasLabel("airport").
order().by(out("route").count(),Order.decr).limit(20).
project("ap","num","city").by("code").by(out("route").count()).by("city").
toList();
topout.forEach((a) -> System.out.format("%4s %12s %4d\n",a.get("ap"),a.get("city"),a.get("num")));
// Find the airports with the most incoming routes
System.out.println("\nTop 20 airports by incoming routes");
System.out.println("==================================");
List<Map<String,Object>> topin =
g.V().hasLabel("airport").
order().by(in("route").count(),Order.decr).limit(20).
project("ap","num","city").by("code").by(in("route").count()).by("city").
toList();
topin.forEach((a) -> System.out.format("%4s %12s %4d\n",a.get("ap"),a.get("city"),a.get("num")));
// Find the longest route in the graph
Map <String,?> longroute =
g.E().hasLabel("route").
order().by("dist",Order.decr).limit(1).
project("from","to","num").
by(inV().values("city")).by(outV().values("city")).by("dist").next();
System.out.println("\nThe longest route in the graph is " + longroute.get("num") +
" miles between " + longroute.get("from") + " and " + longroute.get("to"));
// Find the longest runway in the graph
Map <String,?> longest =
g.V().hasLabel("airport").order().by("longest",Order.decr).limit(1).
project("ap","num","city").by("code").by("longest").by("city").next();
System.out.println("The longest runway in the graph is " + longest.get("num") + " feet at " +
longest.get("city") + "/" + longest.get("ap"));
// Find the continent with the most airports
Map <String,?> cmost;
cmost = g.V().hasLabel("continent").
order().by(out("contains").count(),Order.decr).limit(1).
project("num","cont").
by(out("contains").count()).
by("desc").next();
System.out.println("The continent with the most airports: " +
cmost.get("cont") + " (" +
cmost.get("num") +")");
// Find the country with the most airports
cmost = g.V().hasLabel("country").
order().by(out("contains").count(),Order.decr).limit(1).
project("num","country").
by(out("contains").count()).
by("desc").next();
System.out.println("The country with the most airports: " +
cmost.get("country") + " (" +
cmost.get("num") +")");
cluster.close();
}
}