<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Terence Eden</title>
    <description>The latest articles on DEV Community by Terence Eden (@edent).</description>
    <link>https://dev.to/edent</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F260705%2F13fc3135-9de9-4876-a743-26f6215401ea.jpeg</url>
      <title>DEV Community: Terence Eden</title>
      <link>https://dev.to/edent</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/edent"/>
    <language>en</language>
    <item>
      <title>SPARQL - An Absolute Beginner's Guide</title>
      <dc:creator>Terence Eden</dc:creator>
      <pubDate>Tue, 29 Oct 2019 18:00:51 +0000</pubDate>
      <link>https://dev.to/edent/sparql-an-absolute-beginner-s-guide-2c65</link>
      <guid>https://dev.to/edent/sparql-an-absolute-beginner-s-guide-2c65</guid>
      <description>&lt;p&gt;This very short tutorial will tell you:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What SPARQL is&lt;/li&gt;
&lt;li&gt;How to build a simple query&lt;/li&gt;
&lt;li&gt;Building a &lt;em&gt;useful&lt;/em&gt; query&lt;/li&gt;
&lt;li&gt;Requesting SPARQL from WikiData&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;OK? Let's go!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. WTF is SPARQL?
&lt;/h2&gt;

&lt;p&gt;SPARQL (pronounced "Sparkle") is a way of querying semantic database.&lt;/p&gt;

&lt;p&gt;Wikipedia - the free online encyclopedia - is built on Wikidata.  That's a semantic database which is built on "triples".  For example:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Jill&lt;/li&gt;
&lt;li&gt;was born in&lt;/li&gt;
&lt;li&gt;London&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;or &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Jill&lt;/li&gt;
&lt;li&gt;is the child of&lt;/li&gt;
&lt;li&gt;Jane&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This means we can ask the database "give me all the people born in London". Or, "give me all people who Jill is the child of".&lt;/p&gt;

&lt;p&gt;SPARQL lets us create queries based on data relationships.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. A Simple Query
&lt;/h2&gt;

&lt;p&gt;Let's ask WikiData to find all the people who were born in London.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT ?person WHERE {  
  ?person wdt:P19  wd:Q84
} limit 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That will select 10 people born in London.  Here's what each line does.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;SELECT ?person WHERE {&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Ask the database to assign results to a variable called &lt;code&gt;?person&lt;/code&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;?person wdt:P19  wd:Q84&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Assign to the variable, data where the property "born in" is the entity "London".&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;wdt&lt;/code&gt; means property, and &lt;code&gt;P19&lt;/code&gt; means born in.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;wd&lt;/code&gt; means entity, and &lt;code&gt;Q84&lt;/code&gt; is the ID of London.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;} limit 10&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Just get us 10 results.&lt;/p&gt;

&lt;p&gt;You can &lt;a href="https://query.wikidata.org/#SELECT%20%3Fperson%20WHERE%20%7B%20%20%0A%20%20%3Fperson%20wdt%3AP19%20%20wd%3AQ84%0A%7D%20limit%2010" rel="noopener noreferrer"&gt;run this query on WikiData&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Building a more useful query
&lt;/h2&gt;

&lt;p&gt;If you've run that query, you will see that it isn't very useful! It just gives us back a list of IDs.  We want human readable information!&lt;/p&gt;

&lt;p&gt;Here's how we do it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT ?person ?personLabel WHERE {
  ?person wdt:P19 wd:Q84;
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} limit 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I've added two new things here:&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;?personLabel&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This gives us the "Label" - or human readable name - for the result.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This ensures we are querying the English WikiData.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://query.wikidata.org/#SELECT%20%3Fperson%20%3FpersonLabel%20WHERE%20%7B%0A%20%20%3Fperson%20wdt%3AP19%20wd%3AQ84%3B%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%20limit%2010" rel="noopener noreferrer"&gt;Run the query to see the results&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Requesting SPARQL from WikiData
&lt;/h2&gt;

&lt;p&gt;As you've seen from the above links, you can use the WikiData editor to craft requests. You can then see the results visually.&lt;/p&gt;

&lt;p&gt;If you want to use the results in a program or website, it's a little more complicated.&lt;/p&gt;

&lt;h3&gt;
  
  
  The easy way
&lt;/h3&gt;

&lt;p&gt;Once you have written out your query, you can URL encode it and send it directly to a SPARQL endpoint.  For example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://query.wikidata.org/sparql?query=SELECT%20%3Fperson%20%3FpersonLabel%20WHERE%20%7B%0A%20%20%3Fperson%20wdt%3AP19%20wd%3AQ84%3B%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%20limit%2010" rel="noopener noreferrer"&gt;https://query.wikidata.org/sparql?query=SELECT%20%3Fperson%20%3FpersonLabel%20WHERE%20%7B%0A%20%20%3Fperson%20wdt%3AP19%20wd%3AQ84%3B%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22%5BAUTO_LANGUAGE%5D%2Cen%22.%20%7D%0A%7D%20limit%2010&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You will receive back the results in XML.&lt;/p&gt;

&lt;h3&gt;
  
  
  The hard - but more useful - way
&lt;/h3&gt;

&lt;p&gt;There are hundreds of different libraries for SPARQL. Whatever your favourite programming language, you will be able to find a way to interact with SPARQL.&lt;/p&gt;

&lt;p&gt;Here's a quick snippet of JavaScript which doesn't use a library.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class SPARQLQueryDispatcher {
    constructor( endpoint ) {
        this.endpoint = endpoint;
    }

    query( sparqlQuery ) {
        const fullUrl = this.endpoint + '?query=' + encodeURIComponent( sparqlQuery );
        const headers = { 'Accept': 'application/sparql-results+json' };

        return fetch( fullUrl, { headers } ).then( body =&amp;gt; body.json() );
    }
}

const endpointUrl = 'https://query.wikidata.org/sparql';
const sparqlQuery = `SELECT ?person ?personLabel WHERE {
  ?person wdt:P19 wd:Q84;
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
} limit 10`;

const queryDispatcher = new SPARQLQueryDispatcher( endpointUrl );
queryDispatcher.query( sparqlQuery ).then( console.log );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Next steps
&lt;/h2&gt;

&lt;p&gt;We've only just scratched the surface of the power of SPARQL.  If you're interested in learning more, please leave a comment. Thanks!&lt;/p&gt;

</description>
      <category>sparql</category>
      <category>wikidata</category>
      <category>database</category>
    </item>
  </channel>
</rss>
