The stardog-spring package presents a DataSource bean, created from a DataSourceFactory bean, to be used primarily with the SnarlTemplate. It is very similar to the JDBC data sources and JdbcTemplate in the core Spring framework. However, the Stardog classes do not implement any of the java.sql interfaces, so they are not bean drop-in replacements for any JDBC Spring capability.
Like JdbcTemplate, the SnarlTemplate removes all of the boiler plate (obtaining connections from a pool, beginning and closing out transactions, etc) and provides callback interfaces typical of the Spring Framework (e.g. RowMapper). Basically, it should feel very much like a typical Spring application with some ready made beans to get you started. These classes can be instantiated and managed elsewhere as standalone resources – e.g. The DataSource bean is just a wrapper on the ConnectionPool API, but it’d be just another resource and not something compatible with dbcp, c3p0, etc.
See below example:
|
| SnarlTemplate tmp = new SnarlTemplate();
tmp.setDataSource(dataSource); |
| String sparql = "SELECT ?a ?b WHERE { ?a <http://purl.org/dc/elements/1.1/title> ?b } LIMIT 5"; |
| |
| List<Map<String,String>> results = tmp.query(sparql, new RowMapper<Map<String,String>>() { |
|
|
| @Override |
| public Map<String,String> mapRow(BindingSet bindingSet) { |
| Map<String,String> map = new HashMap<String,String>(); |
| map.put("a", bindingSet.getValue("a").stringValue()); |
| map.put("b", bindingSet.getValue("b").stringValue()); |
| return map; |
| } |
| |
| }); |
In this example, the SnarlTemplate and DataSource would both be configured beans in your application context (e.g. [1])
The template query method will handle all the boiler plate. For any APIs not wrapped in this manner, there is an Execute method that provides full access to the connection, where you can further utilize the Stardog API [2]. The Junit is a good example of all the API usage [3].
Comments, suggestions and pull requests are always welcome if there’s other capabilities you’re looking for.
Thanks,
Al