/usr/share/doc/python-rdflib-doc/html/intro_to_sparql.html is in python-rdflib-doc 4.1.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Querying with SPARQL — rdflib 4.1.2 documentation</title>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
<link rel="stylesheet" href="_static/rtd.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '4.1.2',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/searchtools.js"></script>
<link rel="top" title="rdflib 4.1.2 documentation" href="index.html" />
<link rel="next" title="Utilities and convenience functions" href="utilities.html" />
<link rel="prev" title="Navigating Graphs" href="intro_to_graphs.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="utilities.html" title="Utilities and convenience functions"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="intro_to_graphs.html" title="Navigating Graphs"
accesskey="P">previous</a> |</li>
<li><a href="index.html">rdflib 4.1.2 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="querying-with-sparql">
<span id="intro-to-using-sparql"></span><h1>Querying with SPARQL<a class="headerlink" href="#querying-with-sparql" title="Permalink to this headline">¶</a></h1>
<div class="section" id="run-a-query">
<h2>Run a Query<a class="headerlink" href="#run-a-query" title="Permalink to this headline">¶</a></h2>
<p>The RDFLib comes with an implementation of the <a class="reference external" href="http://www.w3.org/TR/sparql11-query/">SPARQL 1.1 Query</a> and <a class="reference external" href="http://www.w3.org/TR/sparql11-update/">SPARQL 1.1 Update</a> languages.</p>
<p>Queries can be evaluated against a graph with the
<a class="reference internal" href="apidocs/rdflib.html#rdflib.graph.Graph.query" title="rdflib.graph.Graph.query"><tt class="xref py py-meth docutils literal"><span class="pre">rdflib.graph.Graph.query()</span></tt></a> method, and updates with
<a class="reference internal" href="apidocs/rdflib.html#rdflib.graph.Graph.update" title="rdflib.graph.Graph.update"><tt class="xref py py-meth docutils literal"><span class="pre">rdflib.graph.Graph.update()</span></tt></a>.</p>
<p>The query method returns a <a class="reference internal" href="apidocs/rdflib.html#rdflib.query.Result" title="rdflib.query.Result"><tt class="xref py py-class docutils literal"><span class="pre">rdflib.query.Result</span></tt></a> instance. For
SELECT queries, iterating over this return
<a class="reference internal" href="apidocs/rdflib.html#rdflib.query.ResultRow" title="rdflib.query.ResultRow"><tt class="xref py py-class docutils literal"><span class="pre">rdflib.query.ResultRow</span></tt></a> instances, each containing a set of
variable bindings. For CONSTRUCT/DESCRIBE queries, iterating over the
result object gives the triples. For ASK queries, iterating will yield
the single boolean answer, or evaluating the result object in a
boolean-context (i.e. <tt class="docutils literal"><span class="pre">bool(result)</span></tt>)</p>
<p>Continuing the example...</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">rdflib</span>
<span class="n">g</span> <span class="o">=</span> <span class="n">rdflib</span><span class="o">.</span><span class="n">Graph</span><span class="p">()</span>
<span class="c"># ... add some triples to g somehow ...</span>
<span class="n">g</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">"some_foaf_file.rdf"</span><span class="p">)</span>
<span class="n">qres</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="n">query</span><span class="p">(</span>
<span class="sd">"""SELECT DISTINCT ?aname ?bname</span>
<span class="sd"> WHERE {</span>
<span class="sd"> ?a foaf:knows ?b .</span>
<span class="sd"> ?a foaf:name ?aname .</span>
<span class="sd"> ?b foaf:name ?bname .</span>
<span class="sd"> }"""</span><span class="p">)</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">qres</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s">"</span><span class="si">%s</span><span class="s"> knows </span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="n">row</span><span class="p">)</span>
</pre></div>
</div>
<p>The results are tuples of values in the same order as your SELECT
arguments. Alternatively, the values can be accessed by variable
name, either as attributes, or as items: <tt class="docutils literal"><span class="pre">row.b</span></tt> and <tt class="docutils literal"><span class="pre">row["b"]</span></tt> is
equivalent.</p>
<div class="highlight-text"><div class="highlight"><pre>Timothy Berners-Lee knows Edd Dumbill
Timothy Berners-Lee knows Jennifer Golbeck
Timothy Berners-Lee knows Nicholas Gibbins
Timothy Berners-Lee knows Nigel Shadbolt
Dan Brickley knows binzac
Timothy Berners-Lee knows Eric Miller
Drew Perttula knows David McClosky
Timothy Berners-Lee knows Dan Connolly
...
</pre></div>
</div>
<p>As an alternative to using <tt class="docutils literal"><span class="pre">PREFIX</span></tt> in the SPARQL query, namespace
bindings can be passed in with the <tt class="docutils literal"><span class="pre">initNs</span></tt> kwarg, see
<tt class="xref doc docutils literal"><span class="pre">namespace_and_bindings</span></tt>.</p>
<p>Variables can also be pre-bound, using <tt class="docutils literal"><span class="pre">initBindings</span></tt> kwarg can be
used to pass in a <tt class="docutils literal"><span class="pre">dict</span></tt> of initial bindings, this is particularly
useful for prepared queries, as described below.</p>
</div>
<div class="section" id="prepared-queries">
<h2>Prepared Queries<a class="headerlink" href="#prepared-queries" title="Permalink to this headline">¶</a></h2>
<p>RDFLib lets you <em>prepare</em> queries before execution, this saves
re-parsing and translating the query into SPARQL Algebra each time.</p>
<p>The method <tt class="xref py py-meth docutils literal"><span class="pre">rdflib.plugins.sparql.prepareQuery()</span></tt> takes a query as
a string and will return a <a class="reference internal" href="apidocs/rdflib.plugins.sparql.html#rdflib.plugins.sparql.sparql.Query" title="rdflib.plugins.sparql.sparql.Query"><tt class="xref py py-class docutils literal"><span class="pre">rdflib.plugins.sparql.sparql.Query</span></tt></a>
object. This can then be passed to the
<a class="reference internal" href="apidocs/rdflib.html#rdflib.graph.Graph.query" title="rdflib.graph.Graph.query"><tt class="xref py py-meth docutils literal"><span class="pre">rdflib.graph.Graph.query()</span></tt></a> method.</p>
<p>The <tt class="docutils literal"><span class="pre">initBindings</span></tt> kwarg can be used to pass in a <tt class="docutils literal"><span class="pre">dict</span></tt> of
initial bindings:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">q</span> <span class="o">=</span> <span class="n">prepareQuery</span><span class="p">(</span>
<span class="s">'SELECT ?s WHERE { ?person foaf:knows ?s .}'</span><span class="p">,</span>
<span class="n">initNs</span> <span class="o">=</span> <span class="p">{</span> <span class="s">"foaf"</span><span class="p">:</span> <span class="n">FOAF</span> <span class="p">})</span>
<span class="n">g</span> <span class="o">=</span> <span class="n">rdflib</span><span class="o">.</span><span class="n">Graph</span><span class="p">()</span>
<span class="n">g</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="s">"foaf.rdf"</span><span class="p">)</span>
<span class="n">tim</span> <span class="o">=</span> <span class="n">rdflib</span><span class="o">.</span><span class="n">URIRef</span><span class="p">(</span><span class="s">"http://www.w3.org/People/Berners-Lee/card#i"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">g</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="n">q</span><span class="p">,</span> <span class="n">initBindings</span><span class="o">=</span><span class="p">{</span><span class="s">'person'</span><span class="p">:</span> <span class="n">tim</span><span class="p">}):</span>
<span class="k">print</span> <span class="n">row</span>
</pre></div>
</div>
</div>
<div class="section" id="custom-evaluation-functions">
<h2>Custom Evaluation Functions<a class="headerlink" href="#custom-evaluation-functions" title="Permalink to this headline">¶</a></h2>
<p>For experts, it is possible to override how bits of SPARQL algebra are
evaluated. By using the <a class="reference external" href="http://pythonhosted.org/distribute/setuptools.html#dynamic-discovery-of-services-and-plugins">setuptools entry-point</a>
<tt class="docutils literal"><span class="pre">rdf.plugins.sparqleval</span></tt>, or simply adding to an entry to
<a class="reference internal" href="apidocs/rdflib.plugins.sparql.html#rdflib.plugins.sparql.CUSTOM_EVALS" title="rdflib.plugins.sparql.CUSTOM_EVALS"><tt class="xref py py-data docutils literal"><span class="pre">rdflib.plugins.sparql.CUSTOM_EVALS</span></tt></a>, a custom function can be
registered. The function will be called for each algebra component and
may raise <tt class="docutils literal"><span class="pre">NotImplementedError</span></tt> to indicate that this part should be
handled by the default implementation.</p>
<p>See <tt class="file docutils literal"><span class="pre">examples/custom_eval.py</span></tt></p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="index.html">
<img class="logo" src="_static/logo.svg" alt="Logo"/>
</a></p>
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Querying with SPARQL</a><ul>
<li><a class="reference internal" href="#run-a-query">Run a Query</a></li>
<li><a class="reference internal" href="#prepared-queries">Prepared Queries</a></li>
<li><a class="reference internal" href="#custom-evaluation-functions">Custom Evaluation Functions</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="intro_to_graphs.html"
title="previous chapter">Navigating Graphs</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="utilities.html"
title="next chapter">Utilities and convenience functions</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/intro_to_sparql.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="utilities.html" title="Utilities and convenience functions"
>next</a> |</li>
<li class="right" >
<a href="intro_to_graphs.html" title="Navigating Graphs"
>previous</a> |</li>
<li><a href="index.html">rdflib 4.1.2 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2009 - 2013, RDFLib Team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.2.
<br />Theme based on <a href="http://readthedocs.org/">Read The Docs</a>
</div>
</body>
</html>
|