-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIBMGraphExampleJava.java
More file actions
374 lines (339 loc) · 14.7 KB
/
Copy pathIBMGraphExampleJava.java
File metadata and controls
374 lines (339 loc) · 14.7 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import java.io.FileReader;
import java.io.IOException;
import java.util.Base64;
import java.util.UUID;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.wink.json4j.JSONArray;
import org.apache.wink.json4j.JSONException;
import org.apache.wink.json4j.JSONObject;
public class IBMGraphExampleJava {
public static void main(String[] args) throws JSONException, ParseException, IOException {
String baseURI = null;
String sessionURI = null;
String basicAuth = null;
String gdsToken = null;
String gdsTokenAuth = null;
HttpClient client = new DefaultHttpClient();
// Replace your credentials here
String apiURL = null;
String username = null;
String password = null;
if (apiURL == null || username == null || password == null) {
System.out.println("Please provision your own Graph service and update your credentials in the code.");
System.exit(1);
}
// The baseURI is the same as apiURL minus the /g suffix
baseURI = apiURL.substring(0, apiURL.length() - 2);
sessionURI = baseURI + "/_session";
// Form the basic authorization string
System.out.println("Setting up basic authorization");
// Create the basic authorization string
byte[] userpass = (username + ":" + password).getBytes();
byte[] encoding = Base64.getEncoder().encode(userpass);
basicAuth = "Basic " + new String(encoding);
System.out.println("basicAuth is " + basicAuth);
System.out.println("\n\n*****************************\n");
// Get session token, for much faster authentication
gdsTokenAuth = getGDSToken(sessionURI, basicAuth, client);
// Use this to create a new graph
// This can be useful because schemas are immutable and you can begin in
// a clean environment
System.out.println("creating graph");
String graphID = UUID.randomUUID().toString().replaceAll("-", "");
String postURLGraph = baseURI + "/_graphs/g" + graphID;
HttpPost httpPostGraph = new HttpPost(postURLGraph);
httpPostGraph.setHeader("Authorization", gdsTokenAuth);
HttpResponse httpResponseGraph = client.execute(httpPostGraph);
HttpEntity httpEntityGraph = httpResponseGraph.getEntity();
String contentGraph = EntityUtils.toString(httpEntityGraph);
EntityUtils.consume(httpEntityGraph);
JSONObject jsonContentGraph = new JSONObject(contentGraph);
System.out.println("response from creating graph" + jsonContentGraph.toString());
// update apiURL
apiURL = jsonContentGraph.getString("dbUrl");
System.out.println("new apiURL " + apiURL);
System.out.println("\n\n*****************************\n");
// Define the graph schema
System.out.println("Begin defining graph schema");
String schemaFileName = "./src/main/java/sample-schema-file.json";
FileReader schemaFileReader = new FileReader(schemaFileName);
JSONObject postData = new JSONObject(schemaFileReader);
HttpPost httpPost = new HttpPost(apiURL + "/schema");
httpPost.setHeader("Authorization", gdsTokenAuth);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept", "application/json");
StringEntity strEnt = new StringEntity(postData.toString(), ContentType.APPLICATION_JSON);
httpPost.setEntity(strEnt);
HttpResponse httpResponse = client.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
String content = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
JSONObject jsonContent = new JSONObject(content);
JSONObject result = jsonContent.getJSONObject("result");
JSONArray data = result.getJSONArray("data");
if (data.length() > 0) {
JSONObject response = data.getJSONObject(0);
System.out.println("response from creating schema" + response);
}
System.out.println("\n\n*****************************\n");
// Create a vertex, v1
//Note: we will not provide a label, and it will default to "vertex"
System.out.println("creating vertex");
String v1 = null;
String postURL = apiURL + "/vertices";
postData = new JSONObject();
//we must nest our vertex/edge indexes inside a properties {} object
JSONObject vertexIndices = new JSONObject();
vertexIndices.put("name", "david");
vertexIndices.put("address", "Boston");
vertexIndices.put("phone", "888-888-8888");
postData.put("properties", vertexIndices);
httpPost = new HttpPost(postURL);
strEnt = new StringEntity(postData.toString(), ContentType.APPLICATION_JSON);
httpPost.setEntity(strEnt);
httpPost.setHeader("Authorization", gdsTokenAuth);
httpResponse = client.execute(httpPost);
httpEntity = httpResponse.getEntity();
content = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
jsonContent = new JSONObject(content);
result = jsonContent.getJSONObject("result");
data = result.getJSONArray("data");
if (data.length() > 0) {
JSONObject response = data.getJSONObject(0);
System.out.println("response from creating vertex, v1" + response);
v1 = response.getString("id");
}
System.out.println("\n\n*****************************\n");
// Get vertex by ID
System.out.println("getting vertex by id");
HttpGet httpGet = new HttpGet(apiURL + "/vertices/" + v1);
httpGet.setHeader("Authorization", gdsTokenAuth);
httpResponse = client.execute(httpGet);
httpEntity = httpResponse.getEntity();
content = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
jsonContent = new JSONObject(content);
result = jsonContent.getJSONObject("result");
data = result.getJSONArray("data");
if (data.length() > 0) {
JSONObject response = data.getJSONObject(0);
System.out.println("response from get vertex by id" + response);
}
System.out.println("\n\n*****************************\n");
// Get vertex by indexed property
System.out.println("getting vertex by indexed property");
httpGet = new HttpGet(apiURL + "/vertices?name=david");
httpGet.setHeader("Authorization", gdsTokenAuth);
httpResponse = client.execute(httpGet);
httpEntity = httpResponse.getEntity();
content = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
jsonContent = new JSONObject(content);
result = jsonContent.getJSONObject("result");
data = result.getJSONArray("data");
if (data.length() > 0) {
JSONObject response = data.getJSONObject(0);
System.out.println("response from gettinng vertex by indexed property" + response);
}
System.out.println("\n\n*****************************\n");
// Updating a vertex
System.out.println("updating a vertex");
postURL = apiURL + "/vertices/" + v1;
postData = new JSONObject();
vertexIndices = new JSONObject();
vertexIndices.put("phone", "999-999-9999");
postData.put("properties", vertexIndices);
httpPost = new HttpPost(postURL);
httpPost.setHeader("Authorization", gdsTokenAuth);
strEnt = new StringEntity(postData.toString(), ContentType.APPLICATION_JSON);
httpPost.setEntity(strEnt);
httpResponse = client.execute(httpPost);
httpEntity = httpResponse.getEntity();
content = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
jsonContent = new JSONObject(content);
result = jsonContent.getJSONObject("result");
data = result.getJSONArray("data");
if (data.length() > 0) {
JSONObject response = data.getJSONObject(0);
System.out.println("response from updating vertex is " + response);
}
System.out.println("\n\n*****************************\n");
// Get vertex by multiple indexed properties and label
System.out.println("getting vertex by multiple indexed properties and label");
httpGet = new HttpGet(apiURL + "/vertices?name=david&phone=999-999-9999&label=vertex");
httpGet.setHeader("Authorization", gdsTokenAuth);
httpResponse = client.execute(httpGet);
httpEntity = httpResponse.getEntity();
content = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
jsonContent = new JSONObject(content);
result = jsonContent.getJSONObject("result");
data = result.getJSONArray("data");
if (data.length() > 0) {
JSONObject response = data.getJSONObject(0);
System.out.println("response from gettinng vertex by multiple indexed properties and label" + response);
}
System.out.println("\n\n*****************************\n");
// Now let's do some edge operations. To create an edge, we need to
// create an additional vertex.
// Create vertex, v2, with custom label
System.out.println("creating vertex, v2");
String v2 = null;
postURL = apiURL + "/vertices";
postData = new JSONObject();
vertexIndices = new JSONObject();
vertexIndices.put("name", "ricardo");
vertexIndices.put("address", "california");
vertexIndices.put("phone", "111-111-1111");
postData.put("properties", vertexIndices);
postData.put("label", "person");
httpPost = new HttpPost(postURL);
strEnt = new StringEntity(postData.toString(), ContentType.APPLICATION_JSON);
httpPost.setEntity(strEnt);
httpPost.setHeader("Authorization", gdsTokenAuth);
httpResponse = client.execute(httpPost);
httpEntity = httpResponse.getEntity();
content = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
jsonContent = new JSONObject(content);
result = jsonContent.getJSONObject("result");
data = result.getJSONArray("data");
if (data.length() > 0) {
JSONObject response = data.getJSONObject(0);
System.out.println("response from creating vertex, v2" + response);
v2 = response.getString("id");
}
System.out.println("\n\n*****************************\n");
// Create edge
String e1 = null;
System.out.println("creating an edge between vertices v1 and v2");
postURL = apiURL + "/edges";
String vertexId1 = v1;
String vertexId2 = v2;
postData = new JSONObject();
//these edge properties are required, and do not go inside a properties {} object
//since they are not edge indexes
postData.put("outV", vertexId1);
postData.put("inV", vertexId2);
postData.put("label", "knows");
httpPost = new HttpPost(postURL);
httpPost.setHeader("Authorization", gdsTokenAuth);
strEnt = new StringEntity(postData.toString(), ContentType.APPLICATION_JSON);
httpPost.setEntity(strEnt);
httpResponse = client.execute(httpPost);
httpEntity = httpResponse.getEntity();
content = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
jsonContent = new JSONObject(content);
result = jsonContent.getJSONObject("result");
data = result.getJSONArray("data");
if (data.length() > 0) {
JSONObject response = data.getJSONObject(0);
System.out.println("response from creating an edge between vertices v1 and v2" + response);
e1 = response.getString("id");
}
System.out.println("\n\n*****************************\n");
// Get an edge by id
System.out.println("Getting an edge by id");
httpGet = new HttpGet(apiURL + "/edges/" + e1);
httpGet.setHeader("Authorization", basicAuth);
httpResponse = client.execute(httpGet);
httpEntity = httpResponse.getEntity();
content = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
jsonContent = new JSONObject(content);
result = jsonContent.getJSONObject("result");
data = result.getJSONArray("data");
if (data.length() > 0) {
JSONObject response = data.getJSONObject(0);
System.out.println("response from getting edge by id" + response);
}
System.out.println("\n\n*****************************\n");
// You can similarly update an edge using its id, or get an edge by its
// label and indexed properties.
// Will not show here since it is the same thing for vertices, except
// you use the `/edges` endpoint.
// Furthermore, I did not create any index properties for the edges
// above.
// Note: Vertex and edge labels are immutable. Edge labels are required.
// Basic gremlin query
//Note: You must begin all gremlin queries with "def"
//Note: You can use IBM Graph's `/gremlin` endpoint to create vertices and edges,
//but I recommend, especially for beginners to TinkerPOP v3, to use it mostly for
//complex querying.
System.out.println("running a basic gremlin query");
postURL = apiURL + "/gremlin";
postData = new JSONObject();
postData.put("gremlin", "def g = graph.traversal(); g.V(" + v1 + ").out()");
httpPost = new HttpPost(postURL);
httpPost.setHeader("Authorization", gdsTokenAuth);
strEnt = new StringEntity(postData.toString(), ContentType.APPLICATION_JSON);
httpPost.setEntity(strEnt);
httpResponse = client.execute(httpPost);
httpEntity = httpResponse.getEntity();
content = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
jsonContent = new JSONObject(content);
result = jsonContent.getJSONObject("result");
data = result.getJSONArray("data");
if (data.length() > 0) {
JSONObject response = data.getJSONObject(0);
System.out.println("response from basic gremlin query" + response);
}
System.out.println("\n\n*****************************\n");
// Delete an edge
System.out.println("deleting an edge");
HttpDelete httpDelete = new HttpDelete(apiURL + "/edges/" + e1);
httpDelete.setHeader("Authorization", gdsTokenAuth);
httpResponse = client.execute(httpDelete);
httpEntity = httpResponse.getEntity();
content = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
jsonContent = new JSONObject(content);
System.out.println("response from deleting an edge" + jsonContent);
System.out.println("\n\n*****************************\n");
// Delete a vertex
System.out.println("deleting a vertex");
httpDelete = new HttpDelete(apiURL + "/vertices/" + v1);
httpDelete.setHeader("Authorization", gdsTokenAuth);
httpResponse = client.execute(httpDelete);
httpEntity = httpResponse.getEntity();
content = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
jsonContent = new JSONObject(content);
System.out.println("response from deleting a vertex" + jsonContent);
}
private static String getGDSToken(String sessionURI, String basicAuth, HttpClient client)
throws ClientProtocolException, IOException, JSONException {
String gdsToken;
String gdsTokenAuth = null;
System.out.println("Getting session Authorization Token");
HttpGet httpGet = new HttpGet(sessionURI);
httpGet.setHeader("Authorization", basicAuth);
HttpResponse httpResponse = client.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String content = EntityUtils.toString(httpEntity);
System.out.println("content is " + content);
EntityUtils.consume(httpEntity);
JSONObject jsonContent = new JSONObject(content);
gdsToken = jsonContent.getString("gds-token");
System.out.println("The gdsToken value is " + gdsToken);
gdsTokenAuth = "gds-token " + gdsToken;
System.out.println("gdsTokenAuth is " + gdsTokenAuth);
System.out.println("\n\n*****************************\n");
return gdsTokenAuth;
}
}