-
Notifications
You must be signed in to change notification settings - Fork 2
Spark Usage
youngnh edited this page Aug 10, 2011
·
6 revisions
This is an example using the SPARQL protocol implementation (spark-protocol) of the Spark API (spark-api) showcasing the normal flow of execution (sans error handling):
DataSource myDS = new ProtocolDataSource("http://DBpedia.org/sparql");
Connection conn = myDS.getConnection(NoCredentials.INSTANCE);
Command query = conn.createCommand("SELECT ?p ?o WHERE { ?s ?p ?o }");
Solutions solutions = query.executeQuery();
System.out.println("vars = " + solutions.getVariables());
int row = 0;
for(Map solution : solutions) {
System.out.println("Row " + (row++) + ": " + solution);
}
solutions.close();
query.close();
conn.close();
Very similar to JDBC, a DataSource is used to represent a remote SPARQL processor. Connections are created by the DataSource. Commands representing a SPARQL execcution are created from the Connection. Commands when executed return a Result, most often a Solutions object, which is an iterable cursor over the series of solutions.
This is an overview of the main packages and classes in the Spark API:
spark.api:
- DataSource - the provider implements this class and provides JavaBean style getters and setters to configure the Datasource. A factory for Connections.
- Connection - a Connection to a remote SPARQL processor (the underlying implementation may not necessarily maintain a persistent connection). A factory for Commands and access to the ServiceDescription for the Connection.
- Command - a particular SPARQL command for execution. Executing it produces a Result.
- Result - the super-interface for any kind of result from a command execution.
- Cursored Result - a Result that represents a sequence of result items. The cursor can only be traversed as an iterator from beginning to end in one pass.
- Solutions - a cursored series of solutions from a SPARQL SELECT query
spark.api.rdf:
- RDFNode - super-interface of all RDF data nodes
- Resource - super-interface of all RDF resources
- NamedNode - represents a named resource (usually represented by an IRI or URI)
- BlankNode - represents an anonymous resource, which may have a label
- Literal - super-interface of all RDF literal values. All literal values have a lexical representation.
- PlainLiteral - an untyped plain literal with an optional language tag
- TypedLiteral - a typed literal with an XSD type
- Triple - an RDF Triple consisting of a subject, predicate, and object