private Model constructWithReplacement(SparqlEndpoint endpoint, String query) throws Exception{ QueryEngineHTTP qe = new QueryEngineHTTP(endpoint.getURL().toString(), query); qe.setDefaultGraphURIs(endpoint.getDefaultGraphURIs()); String request = qe.toString().replace("GET ", ""); URL url = new URL(request); java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.addRequestProperty(HttpNames.hAccept, WebContent.contentTypeRDFXML); try(BufferedReader rdr = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { Model model = ModelFactory.createDefaultModel(); String buf = null; StringBuilder doc = new StringBuilder(); while ((buf = rdr.readLine()) != null) { // Apply regex on buf if(buf.contains("&#")) { buf = buf.replace("&#", ""); } // build output doc.append(buf); } try(InputStream is = new ByteArrayInputStream(doc.toString().getBytes(StandardCharsets.UTF_8))) { model.read(is, null); } return model; } }
public QueryEngineHTTP createRemoteQueryExecution( String service, Query query, List<String> defaultGraphURIs, List<String> namedGraphURIs, String user, String password) { HttpClient httpClient = buildHttpClient(service, user, password); QueryEngineHTTP qexec = (QueryEngineHTTP) QueryExecutionFactoryFilter.get().sparqlService(service, query, httpClient); if( defaultGraphURIs.size() > 0 ) { qexec.setDefaultGraphURIs(defaultGraphURIs); } if( namedGraphURIs.size() > 0 ) { qexec.setNamedGraphURIs(namedGraphURIs); } return qexec; }
public QueryEngineHTTP createRemoteQueryExecution( String service, Query query, List<String> defaultGraphURIs, List<String> namedGraphURIs, String user, String password) { HttpClient httpClient = buildHttpClient(service, user, password); QueryEngineHTTP qexec = (QueryEngineHTTP) QueryExecutionFactoryFilter.get().sparqlService(service, query, httpClient); if( defaultGraphURIs.size() > 0 ) { qexec.setDefaultGraphURIs(defaultGraphURIs); } if( namedGraphURIs.size() > 0 ) { qexec.setNamedGraphURIs(namedGraphURIs); } return qexec; }
public QueryExecution postProcesss(QueryEngineHTTP qe) { qe.setDefaultGraphURIs(datasetDescription.getDefaultGraphURIs()); qe.setNamedGraphURIs(datasetDescription.getNamedGraphURIs()); QueryExecution result = new QueryExecutionHttpWrapper(qe); return result; }
public static void main(String[] args) { String query = "SELECT ?dt (count(distinct ?o) AS ?cnt)\n" + " WHERE\n" + " { ?s <http://dbpedia.org/ontology/birthDate> ?o }\n" + " GROUP BY (datatype(?o) AS ?dt)"; QueryEngineHTTP qe = new QueryEngineHTTP("http://dbpedia.org/sparql", query); qe.setDefaultGraphURIs(Collections.singletonList("http://dbpedia.org")); ResultSet rs = qe.execSelect(); System.out.println(rs.next()); }
/** Create a QueryExecution that will access a SPARQL service over HTTP * @param service URL of the remote service * @param query Query to execute * @param defaultGraphURIs List of URIs to make up the default graph * @param namedGraphURIs List of URIs to make up the named graphs * @param client HTTP client * @param httpContext HTTP Context * @return QueryExecution */ static public QueryExecution sparqlService(String service, Query query, List<String> defaultGraphURIs, List<String> namedGraphURIs, HttpClient client, HttpContext httpContext) { checkNotNull(service, "URL for service is null") ; // checkNotNull(defaultGraphURIs, "List of default graph URIs is null") ; // checkNotNull(namedGraphURIs, "List of named graph URIs is null") ; checkArg(query) ; QueryEngineHTTP qe = createServiceRequest(service, query, client) ; if ( defaultGraphURIs != null ) qe.setDefaultGraphURIs(defaultGraphURIs) ; if ( namedGraphURIs != null ) qe.setNamedGraphURIs(namedGraphURIs) ; return qe ; }
@Override protected QueryExecution createQueryExecution(Query q) throws SQLException { if (this.remoteConn.getQueryEndpoint() == null) throw new SQLException("This statement is backed by a write-only connection, read operations are not supported"); // Create basic execution QueryEngineHTTP exec = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(this.remoteConn.getQueryEndpoint(), q); // Apply HTTP settings if (this.client != null) { exec.setClient(client); } // Apply default and named graphs if appropriate if (this.remoteConn.getDefaultGraphURIs() != null) { exec.setDefaultGraphURIs(this.remoteConn.getDefaultGraphURIs()); } if (this.remoteConn.getNamedGraphURIs() != null) { exec.setNamedGraphURIs(this.remoteConn.getNamedGraphURIs()); } // Set result types if (this.remoteConn.getSelectResultsType() != null) { exec.setSelectContentType(this.remoteConn.getSelectResultsType()); } if (this.remoteConn.getModelResultsType() != null) { exec.setModelContentType(this.remoteConn.getModelResultsType()); } // Return execution return exec; }
@Override protected QueryExecution createQueryExecution(Query q) throws SQLException { if (this.remoteConn.getQueryEndpoint() == null) throw new SQLException("This statement is backed by a write-only connection, read operations are not supported"); // Create basic execution QueryEngineHTTP exec = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(this.remoteConn.getQueryEndpoint(), q); // Apply HTTP settings if (this.client != null) { exec.setClient(this.client); } // Apply default and named graphs if appropriate if (this.remoteConn.getDefaultGraphURIs() != null) { exec.setDefaultGraphURIs(this.remoteConn.getDefaultGraphURIs()); } if (this.remoteConn.getNamedGraphURIs() != null) { exec.setNamedGraphURIs(this.remoteConn.getNamedGraphURIs()); } // Set result types if (this.remoteConn.getSelectResultsType() != null) { exec.setSelectContentType(this.remoteConn.getSelectResultsType()); } if (this.remoteConn.getModelResultsType() != null) { exec.setModelContentType(this.remoteConn.getModelResultsType()); } // Return execution return exec; }