-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathGraphSPARQLExample.java
More file actions
91 lines (66 loc) · 2.92 KB
/
GraphSPARQLExample.java
File metadata and controls
91 lines (66 loc) · 2.92 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
/*
* Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
/*
* Copyright © 2024 MarkLogic Corporation. All Rights Reserved.
*/
package com.marklogic.client.example.extension;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import com.marklogic.client.DatabaseClient;
import com.marklogic.client.DatabaseClientFactory;
import com.marklogic.client.example.cookbook.Util;
import com.marklogic.client.example.cookbook.Util.ExampleProperties;
import com.marklogic.client.io.InputStreamHandle;
import com.marklogic.client.io.StringHandle;
import com.marklogic.client.semantics.GraphManager;
import com.marklogic.client.semantics.RDFMimeTypes;
import com.marklogic.client.semantics.SPARQLQueryDefinition;
import com.marklogic.client.semantics.SPARQLQueryManager;
public class GraphSPARQLExample {
private static String GRAPH_URI = "com.marklogic.client.example.extension.GraphSPARQLExample";
public static void main(String... args) throws IOException {
ExampleProperties props = Util.loadProperties();
DatabaseClient appClient = Util.newClient(props);
DatabaseClient adminClient = Util.newAdminClient(props);
run(appClient, adminClient);
appClient.release();
adminClient.release();
}
public static void run(DatabaseClient appClient, DatabaseClient adminClient) throws IOException {
insertGraph(appClient);
runQuery(appClient);
deleteGraph(appClient);
}
public static void runQuery(DatabaseClient appClient) throws IOException {
SPARQLQueryManager sparqlMgr = appClient.newSPARQLQueryManager();
InputStream queryStream = Util.openStream(
"scripts"+File.separator+"whoKnowsSwarthmore.sparql");
if (queryStream == null)
throw new RuntimeException("Could not read SPARQL query");
InputStreamHandle queryHandle = new InputStreamHandle(queryStream);
StringHandle result = new StringHandle();
System.out.println("running query");
SPARQLQueryDefinition query = sparqlMgr.newQueryDefinition(queryHandle);
query.setCollections(GRAPH_URI);
sparqlMgr.executeSelect(query, result);
System.out.println(result.get());
}
public static void insertGraph(DatabaseClient appClient) throws IOException {
InputStream tripleStream = Util.openStream(
"data"+File.separator+"foaf1.nt");
if (tripleStream == null)
throw new RuntimeException("Could not read triples");
GraphManager graphMgr = appClient.newGraphManager();
System.out.println("inserting graph");
graphMgr.write(GRAPH_URI, new InputStreamHandle(tripleStream).withMimetype(RDFMimeTypes.NQUADS));
System.out.println("inserted graph");
}
public static void deleteGraph(DatabaseClient appClient) throws IOException {
GraphManager graphMgr = appClient.newGraphManager();
System.out.println("deleting graph");
graphMgr.delete(GRAPH_URI);;
System.out.println("deleted graph");
}
}