public static StringMatrix sparql(String endpoint, String queryString) throws Exception { StringMatrix table = null; // use Apache for doing the SPARQL query DefaultHttpClient httpclient = new DefaultHttpClient(); List<NameValuePair> formparams = new ArrayList<NameValuePair>(); formparams.add(new BasicNameValuePair("query", queryString)); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8"); HttpPost httppost = new HttpPost(endpoint); httppost.setEntity(entity); HttpResponse response = httpclient.execute(httppost); HttpEntity responseEntity = response.getEntity(); InputStream in = responseEntity.getContent(); // now the Jena part ResultSet results = ResultSetFactory.fromXML(in); // also use Jena for getting the prefixes... // Query query = QueryFactory.create(queryString); // PrefixMapping prefixMap = query.getPrefixMapping(); table = convertIntoTable(null, results); in.close(); return table; }
public ResultSet importResultSet(InputStream in, String format) { ResultSet results = null; if(in != null) { if("JSON".equalsIgnoreCase(format)) { System.out.println("Processing SPARQL results set as JSON"); results = JSONInput.fromJSON(in); } else if("XML".equalsIgnoreCase(format)) { System.out.println("Processing SPARQL results set as XML"); results = ResultSetFactory.fromXML(in); } else if("RDF/XML".equalsIgnoreCase(format)) { System.out.println("Processing SPARQL results set as RDF/XML"); results = ResultSetFactory.load(in, ResultsFormat.FMT_RDF_XML); } } return results; }
public static void main(String[] args) { ParameterizedSparqlString qs = new ParameterizedSparqlString("" + "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "prefix dbpedia-owl: <http://dbpedia.org/ontology/>\n" + "prefix dbpprop: <http://dbpedia.org/property/>\n" + "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" + "SELECT DISTINCT * \n" + "WHERE { \n" + " ?resource rdfs:label ?label ;\n" + " dbpedia-owl:abstract ?abstract .\n" + "FILTER ( lang(?abstract) = 'en' ) \n" + "}"); Literal london = ResourceFactory.createLangLiteral("London", "en"); qs.setParam("label", london); System.out.println(qs); QueryExecution exec = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", qs.asQuery()); ResultSet results = ResultSetFactory.copyResults(exec.execSelect()); while (results.hasNext()) { QuerySolution sol = (QuerySolution) results.next(); RDFNode node = sol.get("resource"); System.out.println(sol.get("?abstract")); System.out.println(node); } // A simpler way of printing the results. ResultSetFormatter.out(results); }
public static ResultSet sparqlSelect(String q, String ep) { try { URL url = new URL(ep + "?query=" + URLEncoder.encode(q,"UTF-8") + "&format=xml"); return ResultSetFactory.fromXML(url.openConnection().getInputStream()); } catch (IOException e) { throw new RuntimeException(e); } }
private static void runPlan(Plan plan) { //QueryExecUtils.execute(op, dsg) List<String> vars = null ; ResultsFormat outputFormat = ResultsFormat.FMT_TEXT ; Op op = plan.getOp(); if ( op instanceof OpProject ) vars = Var.varNames(((OpProject)op).getVars()) ; else vars = Var.varNames(OpVars.visibleVars(op)) ; ResultSet results = ResultSetFactory.create(plan.iterator(), vars) ; QueryExecUtils.outputResultSet(results, null, outputFormat) ; }
private QueryInfo query(String epURL, String operation) { QueryInfo info = new QueryInfo(); info.setURL(epURL); info.setOperation(operation); String queryString = query.replaceAll("%%s", "<"+_ep.getUri()+">"); HashSet<String> voidAset= new HashSet<String>(); ArrayList<CharSequence> voidA = new ArrayList<CharSequence>(); info.setResults(voidA); // initializing queryExecution factory with remote service. QueryExecution qexec = null; try { qexec = QueryManager.getExecution(epURL, queryString); boolean results = false; ResultSet resSet = qexec.execSelect(); ResultSetRewindable reswind = ResultSetFactory.makeRewindable(resSet); while(reswind.hasNext()){ RDFNode dataset = reswind.next().get("ds"); voidAset.add(dataset.toString()); } voidA.addAll(voidAset); log.info("Found {} results",reswind.getRowNumber()); } catch (Exception e1) { info.setException(ExceptionHandler.logAndtoString(e1)); log.debug("[EXEC] SPARQL query to "+epURL+" for "+_epURI, e1); } finally { if(qexec!=null)qexec.close(); } return info; }
public ResultSetRewindable runLocalOp(Op op) { long startTime = System.currentTimeMillis(); Query q = OpAsQuery.asQuery(op); logger.debug("Running query on the local dataset" + ":" // + "\n\nORIGINAL OP:\n" // + op.toString() // + "\n\nOPTIMIZED OP\n" // + Algebra.optimize(op) + "\n\nSPARQL QUERY\n" + q.toString(Syntax.syntaxARQ)); try { Integer key = op.toString().hashCode(); if (cache.containsKey(key)) { logger.debug("The query was cached."); return cache.get(key); } ds.begin(ReadWrite.READ); QueryIterator qIter = Algebra.exec(op, this.ds); List<String> vars = new LinkedList<String>(); for (Var var : OpAsQuery.asQuery(op).getProjectVars()) { vars.add(var.getVarName()); } ResultSetRewindable results = ResultSetFactory .copyResults(ResultSetFactory.create(qIter, vars)); long endTime = System.currentTimeMillis(); String timeString = new SimpleDateFormat("mm:ss:SSS") .format(new Date(endTime - startTime)); // cache disabled // cache.put(op.toString().hashCode(), results); logger.info("The query returned after " + timeString + " with " + results.size() + " results"); return results; } finally { ds.end(); } }
public ResultSetRewindable runExternalOp(Op op) { long startTime = System.currentTimeMillis(); Query q = OpAsQuery.asQuery(op); logger.info("Running query on the external dataset:\n\n" + "SPARQL QUERY\n" + q.toString(Syntax.syntaxARQ)); QueryExecution qexec = QueryExecutionFactory.sparqlService( Constants.getSparqlService(), q); ResultSetRewindable results = ResultSetFactory.copyResults(qexec .execSelect()); long endTime = System.currentTimeMillis(); String timeString = new SimpleDateFormat("mm:ss:SSS").format(new Date( endTime - startTime)); logger.info("The query returned after " + timeString + " with " + results.size() + " results"); qexec.close(); return results; }
/** * Execute SPARQL query * @param query sparql query * @param p property * @return results map: uri-s (if uri is a subject), uri-o (if uri is an object) */ private TObjectCharHashMap<String> executeQuery(Query query, String p) { TObjectCharHashMap<String> results = new TObjectCharHashMap<String>(); QueryExecution qexec = null; if(model==null){ if(graphURI == null) qexec = QueryExecutionFactory.sparqlService(endpoint, query); // remote query else qexec = QueryExecutionFactory.sparqlService(endpoint, query, graphURI); // remote query } else qexec = QueryExecutionFactory.create(query, model); // local query try{ //ResultSet results = qexec.execSelect(); ResultSet res = ResultSetFactory.copyResults(qexec.execSelect()) ; QuerySolution qs; String n; while (res.hasNext()) { qs = res.next(); if (qs.get("o") == null) { // get subject n = qs.get("s").toString(); // consider only the type "yago" if (!p.contains("type")) results.put(n, 's'); // target as subject else { if (n.contains("yago")) results.put(n, 's'); // target as subject } } else { // get object n = qs.get("o").toString(); // consider only the type "yago" if (!p.contains("type")) results.put(n, 'o'); // target as object else { if (n.contains("yago")) results.put(n, 'o'); // target as object } } } } catch(Exception e){ e.printStackTrace(); } finally{ qexec.close(); } return results; }
/** * Execute SPARQL query * @param query sparql query * @param p property * @return results map: uri-s (if uri is a subject), uri-o (if uri is an object) */ private TObjectByteHashMap<String> executeQuery(Query query, String p) { TObjectByteHashMap<String> results = new TObjectByteHashMap<String>(); QueryExecution qexec = null; if(model==null){ if(graphURI == null) qexec = QueryExecutionFactory.sparqlService(endpoint, query); // remote query else qexec = QueryExecutionFactory.sparqlService(endpoint, query, graphURI); // remote query } else qexec = QueryExecutionFactory.create(query, model); // local query try{ //ResultSet res = qexec.execSelect(); ResultSet res = ResultSetFactory.copyResults(qexec.execSelect()) ; QuerySolution qs; String n; while (res.hasNext()) { qs = res.next(); if (qs.get("o") == null) { // get subject n = qs.get("s").toString(); // consider only the type "yago" if (!p.contains("type")) results.put(n, (byte) 1); // target as subject else { if (n.contains("yago")) results.put(n, (byte) 1); // target as subject } } else { // get object n = qs.get("o").toString(); // consider only the type "yago" if (!p.contains("type")) results.put(n, (byte) 0); // target as object else { if (n.contains("yago")) results.put(n, (byte) 0); // target as object } } } } catch(Exception e){ e.printStackTrace(); } finally{ //qexec.close(); } return results; }
/** * Execute SPARQL query * @param query sparql query * @param p property * @return results map: prop[uri-s] (if uri is a subject), prop[uri-o] (if uri is an object) */ private THashMap<String, TObjectCharHashMap<String>> executeQuery(Query query) { THashMap<String, TObjectCharHashMap<String>> results = new THashMap<String, TObjectCharHashMap<String>>(); QueryExecution qexec = null; if(model==null){ if(graphURI == null) qexec = QueryExecutionFactory.sparqlService(endpoint, query); // remote query else qexec = QueryExecutionFactory.sparqlService(endpoint, query, graphURI); // remote query } else qexec = QueryExecutionFactory.create(query, model); // local query try{ ResultSet res = ResultSetFactory.copyResults(qexec.execSelect()) ; QuerySolution qs; String n; String p; while (res.hasNext()) { qs = res.next(); p = qs.get("p").toString(); results.putIfAbsent(p, new TObjectCharHashMap<String>()); if (qs.get("o") == null) { // get subject n = qs.get("s").toString(); // consider only the type "yago" if (!p.contains("type")) results.get(p).put(n, 's'); // target as subject else { if (n.contains("yago")) results.get(p).put(n, 's'); // target as subject } } else { // get object n = qs.get("o").toString(); // consider only the type "yago" if (!p.contains("type")) results.get(p).put(n, 'o'); // target as object else { if (n.contains("yago")) results.get(p).put(n, 'o'); // target as object } } } } catch(Exception e){ e.printStackTrace(); } finally{ qexec.close(); } return results; }
/** * Sample query: "https://www.wikidata.org/wiki/Q6607" for guitar * @param id * @return DBPedia response */ public String getWikidataLabelById(String id) { String res = ""; ParameterizedSparqlString qs = new ParameterizedSparqlString( "" + "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "prefix wd: <http://www.wikidata.org/entity/>\n" + "\n" + "select *\n" + "where {\n" + " wd:Q" + id + " rdfs:label ?label .\n" + " FILTER (LANG(?label) = 'en') .\n" + " }\n" + "LIMIT 1" ); // Literal labelLiteral = ResourceFactory.createLangLiteral( label, "en" ); // qs.setParam("label", labelLiteral); log.debug( qs ); QueryExecution exec = QueryExecutionFactory.sparqlService(SPARQL_ENDPOINT, qs.asQuery()); ResultSet resultSet = exec.execSelect(); // Normally you'd just do results = exec.execSelect(), but I want to // use this ResultSet twice, so I'm making a copy of it. ResultSet results = ResultSetFactory.copyResults( resultSet ); while ( results.hasNext() ) { // As RobV pointed out, don't use the `?` in the variable // name here. Use *just* the name of the variable. // System.out.println( results.next().get( "resource" )); QuerySolution resQs = results.next(); res = res + resQs.get( "resource" ) + "#"; res = res + resQs.get( "description" ); // System.out.println( resQs.get( "resource" )); // System.out.println( resQs.get( "description" )); } // A simpler way of printing the results. ResultSetFormatter.out( results ); // return results.toString(); if (res.equals("")) res = "#"; return res; }
/** * Sample query: "http://dbpedia.org/page/Chicago_blues" * @param label * @return DBPedia response */ public String queryDBPedia(String label) { String res = ""; ParameterizedSparqlString qs = new ParameterizedSparqlString( "" + "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "\n" + "select ?resource ?description where {\n" + // " ?resource rdfs:label \"" + label + "\"@en .\n" + " ?resource rdfs:label ?label .\n" + " ?resource rdfs:comment ?description .\n" + " FILTER (LANG(?description) = 'en') .\n" + // "select ?resource where {\n" + // " ?resource rdfs:label ?label\n" + "}" ); Literal labelLiteral = ResourceFactory.createLangLiteral( label, "en" ); qs.setParam("label", labelLiteral); System.out.println( qs ); QueryExecution exec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", qs.asQuery()); // Normally you'd just do results = exec.execSelect(), but I want to // use this ResultSet twice, so I'm making a copy of it. ResultSet results = ResultSetFactory.copyResults( exec.execSelect() ); while ( results.hasNext() ) { // As RobV pointed out, don't use the `?` in the variable // name here. Use *just* the name of the variable. // System.out.println( results.next().get( "resource" )); QuerySolution resQs = results.next(); res = res + resQs.get( "resource" ) + "#"; res = res + resQs.get( "description" ); // System.out.println( resQs.get( "resource" )); // System.out.println( resQs.get( "description" )); } // A simpler way of printing the results. ResultSetFormatter.out( results ); // return results.toString(); if (res.equals("")) res = "#"; return res; }
/** * Sample query: "http://dbpedia.org/page/Chicago_blues" * @param label * @return DBPedia response */ public String queryDBPediaByLanguage(String label, String language) { String res = ""; ParameterizedSparqlString qs = new ParameterizedSparqlString( "" + "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "\n" + "select ?resource ?description where {\n" + // " ?resource rdfs:label \"" + label + "\"@en .\n" + " ?resource rdfs:label ?label .\n" + " ?resource rdfs:comment ?description .\n" + " FILTER (LANG(?description) = '" + language + "') .\n" + // "select ?resource where {\n" + // " ?resource rdfs:label ?label\n" + "}" ); Literal labelLiteral = ResourceFactory.createLangLiteral( label, language ); qs.setParam("label", labelLiteral); System.out.println( qs ); QueryExecution exec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", qs.asQuery()); // Normally you'd just do results = exec.execSelect(), but I want to // use this ResultSet twice, so I'm making a copy of it. ResultSet results = ResultSetFactory.copyResults( exec.execSelect() ); while ( results.hasNext() ) { // As RobV pointed out, don't use the `?` in the variable // name here. Use *just* the name of the variable. // System.out.println( results.next().get( "resource" )); QuerySolution resQs = results.next(); res = res + resQs.get( "resource" ) + "#"; res = res + resQs.get( "description" ); // System.out.println( resQs.get( "resource" )); // System.out.println( resQs.get( "description" )); } // A simpler way of printing the results. ResultSetFormatter.out( results ); // return results.toString(); if (res.equals("")) res = "#"; return res; }