-
Notifications
You must be signed in to change notification settings - Fork 3
FedX in Java
FedX is implemented as a RDF4J SAIL. To initialize FedX and the underlying federation SAIL, we provide the FedXFactory class, which provides various methods for intuitive configuration. In the following, we present various Java code snippets that illustrate how FedX can be used in an application.
Basically, FedX can be used and accessed using the SAIL architecture (see the RDF4J SAIL documentation for details). The (initialized) Repository can be obtained from any FedXFactory initialization method. Besides using the Repository interface for creating queries, we also provide a QueryManager class to conveniently create queries. The advantage of the QueryManager over using the RepositoryConnection to create queries, is that preconfigured PREFIX declarations are added automatically to the query, i.e. the user can use common prefixes (such as rdf, foaf, etc.) without the need to specify them in the prologue of the query. See PREFIX Declarations for a detailed documentation.
Example 1: Using a simple SPARQL Federation as a Repository
In the following example, we configure a federation with the publicly available DBpedia and SemanticWebDogFood SPARQL endpoints. Note that in the beginning the configuration is initialized with the default properties. Please refer to Configuring FedX for details.
Config.initialize();
Repository repo = FedXFactory.initializeSparqlFederation(Arrays.asList(
"http://dbpedia.org/sparql",
"http://data.semanticweb.org/sparql"));
String q = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
+ "PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>\n"
+ "SELECT ?President ?Party WHERE {\n"
+ "?President rdf:type dbpedia-owl:President .\n"
+ "?President dbpedia-owl:party ?Party . }";
TupleQuery query = repo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, q);
try (TupleQueryResult res = query.evaluate()) {
while (res.hasNext()) {
System.out.println(res.next());
}
}
repo.shutDown();
System.out.println("Done.");
System.exit(0);
Example 2: Setting up a simple SPARQL federation (With QueryManager)
Similarly to example 1, we use a federation of DBpedia and NYTimes. However, this time we use the QueryManager to prepare the query.
Config.initialize();
Repository repo = FedXFactory.initializeSparqlFederation(Arrays.asList(
"http://dbpedia.org/sparql",
"http://data.semanticweb.org/sparql"));
String q = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
+ "PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>\n"
+ "SELECT ?President ?Party WHERE {\n"
+ "?President rdf:type dbpedia-owl:President .\n"
+ "?President dbpedia-owl:party ?Party . }";
TupleQuery query = QueryManager.prepareTupleQuery(q);
try (TupleQueryResult res = query.evaluate()) {
while (res.hasNext()) {
System.out.println(res.next());
}
}
repo.shutDown();
System.out.println("Done.");
System.exit(0);
Example 3: Using a data configuration file
In this example we use a data configuration file to set up the federation members. Note that in this example we use an initialized Repository to create the query, as well as the connection.
String dataConfig = "local/dataSourceConfig.ttl";
Config.initialize();
Repository repo = FedXFactory.initializeFederation(dataConfig);
String q = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
+ "PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>\n"
+ "SELECT ?President ?Party WHERE {\n"
+ "?President rdf:type dbpedia-owl:President .\n"
+ "?President dbpedia-owl:party ?Party . }";
TupleQuery query = repo.getConnection().prepareTupleQuery(QueryLanguage.SPARQL, q);
try (TupleQueryResult res = query.evaluate()) {
while (res.hasNext()) {
System.out.println(res.next());
}
}
repo.shutDown();
System.out.println("Done.");
System.exit(0);
Example 4: Setting up FedX using the Endpoint utilities
This example shows how to setup FedX using a mechanism to include dynamic endpoints.
Config.initialize();
List<Endpoint> endpoints = new ArrayList<>();
endpoints.add( EndpointFactory.loadSPARQLEndpoint("dbpedia", "http://dbpedia.org/sparql"));
endpoints.add( EndpointFactory.loadSPARQLEndpoint("swdf", "http://data.semanticweb.org/sparql"));
Repository repo = FedXFactory.initializeSparqlFederation(endpoints);
String q = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
+ "PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>\n"
+ "SELECT ?President ?Party WHERE {\n"
+ "?President rdf:type dbpedia-owl:President .\n"
+ "?President dbpedia-owl:party ?Party . }";
TupleQuery query = QueryManager.prepareTupleQuery(q);
try (TupleQueryResult res = query.evaluate()) {
while (res.hasNext()) {
System.out.println(res.next());
}
}
repo.shutDown();
System.out.println("Done.");
System.exit(0);
FedX federations can be managed both at initialization and at runtime. This is possible since FedX is capable of on-demand federation setup, meaning that we do not require any prior knowledge about data sources.
The federation can be controlled at runtime using the FederationManager. This class provides all means for interacting with the federation at runtime, e.g. adding or removing federation members.
Endpoints can be added to the federation using the methods addEndpoint(Endpoint)
and removed with removeEndpoint(endpoint)
. Note that new endpoints can be initialized using the endpoint Management facilities.
In FedX any federation member is mapped to an Endpoint. The endpoint maintains all relevant information for a particular endpoint, e.g. how triples can be retrieved from the endpoint. Endpoints can be added to the federation at initialization time or at runtime.
In FedX we provide support methods to create Endpoints for SPARQL endpoints, RDF4J _NativeStore_s. The methods can be used to create endpoints easily.
Example: Using the endpoint Manager to create endpoints
Config.initialize();
List<Endpoint> endpoints = new ArrayList<>();
// initializing a SPARQL endpoint (with explicit name)
endpoints.add( EndpointFactory.loadSPARQLEndpoint("http://dbpedia", "http://dbpedia.org/sparql"));
// another SPARQL endpoint (name is constructed from url)
endpoints.add( EndpointFactory.loadSPARQLEndpoint("http://data.semanticweb.org/sparql"));
// load a Sesame NativeStore (path either absolute or relative to Config#getBaseDir)
endpoints.add( EndpointFactory.loadNativeEndpoint("http://mystore", "path/to/myNativeStore"));
FedXFactory.initializeFederation(endpoints);
For details about the methods please refer to the javadoc help of the class EndpointFactory
Note: With the Endpoint mechanism it is basically possible to support any kind of SAIL implementation as federation member. For documentation consider the javadoc, in particular EndpointFactory and EndpointProvider.
(c) 2011-2018 fluid Operations AG
(c) 2018-2019 Veritas Technologies LLC
http://www.veritas.com
Last updated: 08.04.2019