Learning Sparql

From artserver wiki

(Some)RDF Languages[1]

Convert with Apache Riot

Using riot cli tool from Apache Jena, convert a .N3 data file to .ttl and rdf/xml

riot  --output=ttl TimBernersLee.n3 > TimBernersLee.ttl
riot  --output=rdf/xml TimBernersLee.n3 > TimBernersLee.rdf


run query[2]

# queryTBL.rq

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?family_name ?account
WHERE {
	?person foaf:family_name ?family_name .
	?person foaf:name ?name .
	?person foaf:account ?account .
	FILTER (?account not in (<http://www.reddit.com/user/timbl/>))
}


arq --query queryTBL.rq --data TimBernersLee.ttl


Results:

-------------------------------------------------------------------------------------
| name                  | family_name   | account                                   |
=====================================================================================
| "Timothy Berners-Lee" | "Berners-Lee" | <http://twitter.com/timberners_lee>       |
| "Timothy Berners-Lee" | "Berners-Lee" | <http://en.wikipedia.org/wiki/User:Timbl> |
-------------------------------------------------------------------------------------


Querying DBPedia

Using its SPARQL virtuoso interface

Ask for the subjects that have the

rdf:type schema:Continent

Which is visible in via http://dbpedia.org/page/Europe


All continents:

SELECT DISTINCT *
WHERE {
    ?continent rdf:type schema:Continent .
}


Which can be elaborated to also display the rdfs:label and FILTER only the ones that have Portugues as LANGUAGE

SELECT DISTINCT *
WHERE {
    ?continent rdf:type schema:Continent ;
    rdfs:label ?label .
    FILTER (LANG(?label) = "pt")
}


querying Service from file

Here is the same query, but done from a local file to dbpedia SPARQL service endpoint.

# continents.rq
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX schema: <http://schema.org/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT DISTINCT * 
WHERE { 
    SERVICE <http://dbpedia.org/sparql> 
	{ 
	SELECT ?link ?label
		WHERE {
		  ?link rdf:type schema:Continent ;
		  rdfs:label ?label .
		  FILTER (LANG(?label) = "zh")
		}
	}
}
arq --query continents_dbpedia.rq

Result:

---------------------------------------------------------------------
| link                                                | label       |
=====================================================================
| <http://dbpedia.org/resource/Eurasia>               | "歐亞大陸"@zh   |
| <http://dbpedia.org/resource/Afro-Eurasia>          | "歐亞非大陸"@zh  |
| <http://dbpedia.org/resource/Asia>                  | "亚洲"@zh     |
| <http://dbpedia.org/resource/Europe>                | "欧洲"@zh     |
| <http://dbpedia.org/resource/Latin_America>         | "拉丁美洲"@zh   |
| <http://dbpedia.org/resource/North_America>         | "北美洲"@zh    |
| <http://dbpedia.org/resource/Oceania>               | "大洋洲"@zh    |
| <http://dbpedia.org/resource/Scandinavia>           | "斯堪的纳维亚"@zh |
| <http://dbpedia.org/resource/South_America>         | "南美洲"@zh    |
| <http://dbpedia.org/resource/Anglo-America>         | "盎格鲁美洲"@zh  |
| <http://dbpedia.org/resource/Australia_(continent)> | "澳大利亞洲"@zh  |
| <http://dbpedia.org/resource/Americas>              | "美洲"@zh     |
| <http://dbpedia.org/resource/Arabian_Peninsula>     | "阿拉伯半岛"@zh  |
| <http://dbpedia.org/resource/Antarctica>            | "南极洲"@zh    |
| <http://dbpedia.org/resource/Indian_subcontinent>   | "印度次大陸"@zh  |
| <http://dbpedia.org/resource/Africa>                | "非洲"@zh     |
---------------------------------------------------------------------