Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SPARQL入門 / SPARQL primer

fumi
December 19, 2016

SPARQL入門 / SPARQL primer

https://doi.org/10.6084/m9.figshare.5947678

2016-12-19 JSTリンクト・データ(大規模文献情報・シソーラス)を使い倒そう!-LODC2016×J-GLOBAL knowledge連携イベント- の講演スライド http://lodc2016-jst.peatix.com

fumi

December 19, 2016
Tweet

More Decks by fumi

Other Decks in Technology

Transcript

  1. άϥϑͷΫΤϦ • άϥϑͷ࠷খ୯Ґͭ૊ τϦϓϧ  • άϥϑύλʔϯ 1. ׬શҰக ECS౦ژ౎ECPDPVOUSZECS೔ຊ

    2. ม਺ʹΑΔύλʔϯϚον ECS౦ژ౎ECPDPVOUSZ DPVOUSZ 3. ͱͷ૊Έ߹Θͤ dbr:東京都 dbr:日本 dbo:country 
  2. :Amy :Bob :Cathy :Dan :Jim :Ed :Kate :Gil :Flo :Hal

    :Ian :Lee :May :like :like :like :like :like :like :like :dislike :dislike :dislike :dislike :dislike :dislike :dislike :dislike ޷͖ݏ͍άϥϑ 
  3. :Amy :Bob :Cathy :Dan :Jim :Ed :Kate :Gil :Flo :Hal

    :Ian :Lee :May :like :like :like :like :like :like :like :dislike :dislike :dislike :dislike :dislike :dislike :dislike :dislike "NZ͕޷͖ͳਓ  άϥϑύλʔϯ "NZMJLF QFSTPO
  4. :Amy :Bob :Cathy :Dan :Jim :Ed :Kate :Gil :Flo :Hal

    :Ian :Lee :May :like :like :like :like :like :like :like :dislike :dislike :dislike :dislike :dislike :dislike :dislike :dislike l"NZ͕޷͖ͳਓz͕޷͖ͳਓ  άϥϑύλʔϯ "NZMJLF QFSTPO QFSTPOMJLF QFSTPO
  5. dbr:AdministraDveRegion dbr:サイボーグ009 dbo: ComicsCreator dbr:宮城県 dbr:石ノ森章太郎 rdfs:label rdf:type rdfs:label dbo:notableWork

    dbo:award dbo:birthPlace rdf:type サイボーグ009 ٶ৓ݝ foaf:Person 石ノ森章太郎 rdf:type rdfs:label dbr:村井嘉浩 dbo:leaderName %#QFEJB+BQBOFTFͷάϥϑྫ dbr:手塚治虫 文化賞 dbo:Cartoon rdf:type  13&'*9ECSIUUQKBECQFEJBPSHSFTPVSDF 13&'*9ECPIUUQECQFEJBPSHPOUPMPHZ
  6. dbo:AdministraDveRegion dbr:サイボーグ009 dbo: ComicsCreator dbr:宮城県 dbr:石ノ森章太郎 rdfs:label rdf:type rdfs:label dbo:notableWork

    dbo:award dbo:birthPlace rdf:type サイボーグ009 宮城県 foaf:Person 石ノ森章太郎 rdf:type rdfs:label dbr:村井嘉浩 dbo:leaderName dbr:手塚治虫 文化賞 dbo:Cartoon rdf:type  ΫΤϦੴϊ৿ষଠ࿠ͷग़਎஍ 13&'*9ECSIUUQKBECQFEJBPSHSFTPVSDF 13&'*9ECPIUUQECQFEJBPSHPOUPMPHZ 4&-&$5 CJSUI1MBDF 8)&3&\ ECSੴϊ৿ষଠ࿠ECPCJSUI1MBDF CJSUI1MBDF ^
  7.  • SELECT: औಘ͍ͨ͠ม਺Λࢦఆ • SELECT * ͸શͯͷม਺ࢦఆͱಉ౳ • ݁Ռܗࣜ:

    XML, JSON, CSV, TSV, ... • WHERE: {}಺ʹάϥϑύλʔϯͷ૊Έ߹ΘͤΛهड़ • LIMIT: ݅਺ͷ੍ݶ • 100ͩͱ1-100·Ͱऔಘ • LIMIT͕ͳ͍৔߹͸શ݅औಘ • ྔ͕ଟ͍৔߹͸αʔόʹෛՙΛ͔͚ΔͷͰ஫ҙ • PREFIX: URIΛ୹ॖͯ͠هड़͢ΔͨΊͷ࢓૊Έ • dbr:౦ژ౎ = <h^p://ja.dbpedia.org/resource/౦ژ౎> 13&'*9ECSIUUQKBECQFEJBPSHSFTPVSDF 13&'*9ECPIUUQECQFEJBPSHPOUPMPHZ 4&-&$5 CJSUI1MBDF 8)&3&\ ECSੴϊ৿ষଠ࿠ECPCJSUI1MBDF CJSUI1MBDF ^
  8. 

  9. dbo:AdministraDveRegion dbr:サイボーグ009 dbo: ComicsCreator dbr:宮城県 dbr:石ノ森章太郎 rdfs:label rdf:type rdfs:label dbo:notableWork

    dbo:award dbo:birthPlace rdf:type サイボーグ009 宮城県 foaf:Person 石ノ森章太郎 rdf:type rdfs:label dbr:村井嘉浩 dbo:leaderName dbr:手塚治虫 文化賞 dbo:Cartoon rdf:type  ΫΤϦख௩࣏஬จԽ৆Λड৆ͨ͠ອըՈ 13&'*9ECSIUUQKBECQFEJBPSHSFTPVSDF 13&'*9ECPIUUQECQFEJBPSHPOUPMPHZ  4&-&$5 DSFBUPS 8)&3&\  DSFBUPSSEGUZQFECP$PNJDT$SFBUPS ECPBXBSEECSख௩࣏஬จԽ৆ ^
  10. 

  11. dbo:AdministraDveRegion dbr:サイボーグ009 dbo: ComicsCreator dbr:宮城県 dbr:石ノ森章太郎 rdfs:label rdf:type rdfs:label dbo:notableWork

    dbo:award dbo:birthPlace rdf:type サイボーグ009 宮城県 foaf:Person rdf:type rdfs:label dbr:村井嘉浩 dbo:leaderName dbp:手塚治虫 文化賞 dbo:Cartoon rdf:type  ΫΤϦख௩࣏஬จԽ৆Λड৆ͨ͠ອըՈͷ୅දతͳອը 13&'*9ECSIUUQKBECQFEJBPSHSFTPVSDF 13&'*9ECPIUUQECQFEJBPSHPOUPMPHZ 4&-&$5 DSFBUPS/BNF DPNJD/BNF 8)&3&\  DSFBUPSBECP$PNJDT$SFBUPSECPBXBSEECSख௩࣏஬จԽ৆ ECPOPUBCMF8PSL DPNJDSEGTMBCFM DSFBUPS/BNF  DPNJDBECP$BSUPPOSEGTMBCFM DPNJD/BNF ^ 石ノ森章太郎
  12. 

  13. 41"32- w σʔλऔಘ w 4&-&$5 w "4, w %&4$3*#& w

    $0/4536$5 w ΄΅Ͳ͜Ͱ΋࢖͑Δ 
  14. %&4$3*#&  • %&4$3*#&Ϧιʔεʹؔ͢ΔάϥϑΛऔಘ • άϥϑͱͯ͠Կ͕ฦͬͯ͘Δ͔͸࣮૷ґଘ • ର৅ϦιʔεΛTVCKFDU͔PCKFDUʹؚΉͭ૊ͷू߹ • ݁Ռܗࣜ5VSUMF

    3%'9.- +40/-%౳ ྫ Պֶٕज़ৼڵػߏʹؔ͢ΔάϥϑΛऔಘ DESCRIBE <h^p://ja.dbpedia.org/resource/Պֶٕज़ৼڵػߏ>
  15.  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX orth: <http://purl.jp/bio/11/orth#> PREFIX protid: <http://identifiers.org/ncbiprotein/>

    SELECT DISTINCT ?protein_id FROM <http://mbgd.genome.ad.jp/rdf/resource/default> FROM <http://mbgd.genome.ad.jp/rdf/resource/gene> WHERE { ?group a orth:OrthologGroup ; orth:member+/orth:gene?/orth:protein?/rdfs:seeAlso protid:NP_563687.1 ; orth:member+/orth:gene?/orth:protein?/rdfs:seeAlso ?protein_id . FILTER regex(?protein_id, "identifiers.org/ncbiprotein") }
  16. ϓϩύςΟύε w ௨ৗͷͭ૊ύλʔϯ T Q P w Pͷলུ΍ Qͷ܁Γฦ͠ɼ Qͷٯ޲͖

    ౳Λهड़Մೳ PREFIX dbr: <h^p://ja.dbpedia.org/resource/> PREFIX dbo: <h^p://dbpedia.org/ontology/> PREFIX rdfs: <h^p://www.w3.org/2000/01/rdf-schema#> SELECT ?creator ?comicName WHERE { ?creator dbo:notableWork/rdfs:label ?comicName . } 
  17. ू໿ w $06/5ݸ਺ w 46.߹ܭ w "7(ฏۉ w .*/࠷খ஋ w

    ."9࠷େ஋ w (3061#:ू໿͢Δม਺ͷࢦఆ 
  18. $06/5  # ৿܆ϦιʔεͷτϦϓϧ਺ SELECT (COUNT(?o) AS ?count) WHERE {

    <h^p://ja.dbpedia.org/resource/৿܆> ?p ?o . }
  19. (3061#:  ྫ) ΫϥεຖͷΠϯελϯεͷ਺্Ґ100݅Λ߱ॱ SELECT ?class (COUNT(?instance) AS ?noOfInstances) WHERE

    { ?instance a ?class . } GROUP BY ?class ORDER BY DESC(?noOfInstances) LIMIT 100
  20. αϒΫΤϦ  ྫ) ΫϥεຖͷΠϯελϯεͷ਺ͷฏۉ SELECT (AVG(?noOfInstances) AS ?avg) WHERE {

    { SELECT ?class (COUNT(?instance) AS ?noOfInstances) WHERE { ?instance a ?class . } GROUP BY ?class } }
  21. 41"32-ͷՄࢹԽ ࣗલ w 41"32-݁Ռ͸ܾ·ͬͨߏ଄ͷ+40/΍9.-ͳͷͰɺ ௚઀)551Ͱ໰͍߹Θͤͯ࢖͑͹ྑ͍ w ଟ͘ͷݴޠͰ͸41"32-ॲཧ༻ͷϥΠϒϥϦ΋͋Δ w දࣔ෦෼͸ࣗલͰ࡞Δඞཁ͕͋Δ 

    function query(endpoint, sparql, callback) { var url = endpoint + "?query=" + encodeURIComponent(sparql); $.getJSON(url, function(json){ callback(json) } }
  22. ରԠܗࣜ w $IBSU w #BSDIBSU w 4DBUUFSQMPU w 1JFDIBSU w

    (SBQI w 'PSDFHSBQ I w 4BOLFZ w 5SFF w %FOESPHSB N w 5SFFNBQ w 5SFFNBQ;P PN w 3PVOE5SFF w $JSDMFQBDL w 4VOCVSTU w .BQ w $PPSENBQ w /BNFE NBQ w 5BCMF w )5.- 5BCMF w )5.- )BTI 
  23. 

  24.  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX yago: <http://dbpedia.org/class/yago/> PREFIX dbo: <http://dbpedia.org/ontology/>

    SELECT ?label ?size WHERE { ?s a yago:WikicatPrefecturesOfJapan ; rdfs:label ?label; dbo:areaTotal ?area_total . FILTER (lang(?label) = 'ja') BIND ((?area_total / 1000000) AS ?size) FILTER (str(?s) != 'http://dbpedia.org/resource/Onjuku') } ORDER BY DESC(?size)
  25. 

  26. OBNFENBQΫΤϦ  function exec() { var endpoint = d3.select("#endpoint").property("value") var

    sparql = d3.select("#sparql").property("value") d3sparql.query(endpoint, sparql, render) } function render(json) { var config = { "label_x": "౎ಓ෎ݝ", "label_y": "໘ੵ", "var_x": "label", "var_y": "size", "width": 800, "height": 400, "margin": 100, "selector": "#result" } d3sparql.barchart(json, config) } ର৅ͷม਺໊ΛDPOpHͰࢦఆ #BSDIBSUͷݺͼग़͠
  27. OBNFENBQΫΤϦ  function render(json) { var config = { "label":

    "label", "size": "size", "width": 600, "height": 600, "margin": 10, "hole": 200, "selector": "#result" } d3sparql.piechart(json, config) }
  28.  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX yago: <http://dbpedia.org/class/yago/> PREFIX dbo: <http://dbpedia.org/ontology/>

    SELECT DISTINCT ?s ?label ?population ?area ?density WHERE { ?s a yago:WikicatPrefecturesOfJapan ; rdfs:label ?label ; dbo:populationTotal ?population ; dbo:areaTotal ?area . BIND (xsd:float(?population)/xsd:float(?area/1000000) AS ?density) FILTER (lang(?label) = 'ja' ) FILTER (str(?s) != 'http://dbpedia.org/resource/Onjuku') } ORDER BY DESC(?density) ಛఆͷม਺໊ΛOBNFENBQͷDPOpHͰࢦఆ
  29. OBNFENBQΫΤϦ  function exec() { var endpoint = d3.select("#endpoint").property("value") var

    sparql = d3.select("#sparql").property("value") d3sparql.query(endpoint, sparql, render) } function render(json) { var config = { "label": "label", "value": "density", "topojson": "https://gist.githubusercontent.com/fumi/ 521c759efe0648b3c1296c5a869b3406/raw/ 39e94dae1c46ed71d88529038f3ff45e681b257c/japan.topojson", "mapname": "japan", "color_max": "red", "color_min": "white", "center_lat": 34, "center_lng": 137, "scale": 1500, "selector": "#result" } d3sparql.namedmap(json, config) }
  30. OBNFENBQΫΤϦ  PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX dbpedia-owl:

    <http://dbpedia.org/ontology/> PREFIX prop-ja: <http://ja.dbpedia.org/property/> SELECT ?root_name ?parent_name ?child_name WHERE { VALUES ?root_name { "౦ژӺ"@ja } ?root rdfs:label ?root_name . ?root prop-ja:࣍ͷӺ{1,5} ?child . ?parent prop-ja:࣍ͷӺ ?child . MINUS { ?child prop-ja:࣍ͷӺ ?parent . } ?parent rdfs:label ?parent_name . ?child rdfs:label ?child_name . FILTER (LANG(?parent_name) = 'ja') FILTER (LANG(?child_name) = 'ja') } GROUP BY ?root_name ?parent_name ?child_name ஫ҙ͜ͷ෦෼͸ ඪ४࢓༷Ͱ͸ͳ͍
  31.