我正在尝试查询dataset 使用Ruby RDF。它应该返回20条记录,而不是1条记录。我的查询或迭代有什么问题?
require 'rdf'
require 'linkeddata'
include RDF
graph = RDF::Graph.load("http://opendatacommunities.org/resources_in_dataset.jsonld?dataset=planning-inspectorate-casework&page=1&per_page=20&type_uri=http%3A%2F%2Fopendatacommunities.org%2Fdef%2Fontology%2Fplanning%2Fpins%2FAppeal")
query = RDF::Query.new({
:appeal => {
RDF::URI("http://opendatacommunities.org/def/ontology/planning/pins-appeals/Agent") => :Agent,
RDF::URI("http://opendatacommunities.org/def/ontology/planning/pins-appeals/CaseRef") => :CaseRef,
RDF::URI("http://opendatacommunities.org/def/ontology/planning/pins-appeals/Address1") => :Address1,
RDF::URI("http://opendatacommunities.org/def/ontology/planning/pins-appeals/Address2") => :Address2,
RDF::URI("http://opendatacommunities.org/def/ontology/planning/pins-appeals/County") => :County,
RDF::URI("http://opendatacommunities.org/def/ontology/planning/pins-appeals/Postcode") => :Postcode,
RDF::URI("http://opendatacommunities.org/def/ontology/planning/pins-appeals/Town") => :Town
}
})
count = 0
query.execute(graph) do |appeal|
puts "Record #{count += 1}"
puts appeal[:Agent]
puts appeal[:CaseRef]
puts appeal[:Address1]
puts appeal[:Address2]
puts appeal[:County]
puts appeal[:Postcode]
puts appeal[:Town]
end
答案 0 :(得分:3)
您有三种选择:
- 使用显式模式:
,而不是使用哈希来构造查询 醇>
appeals = RDF::URI("http://opendatacommunities.org/def/ontology/planning/pins-appeals/")
query = RDF::Query.new do
pattern [:appeal, appeals/"CaseRef", :CaseRef]
pattern [:appeal, appeals/"Address1", :Address1]
pattern [:appeal, appeals/"Town", :Town]
pattern [:appeal, appeals/"Agent", :Agent], optional: true
pattern [:appeal, appeals/"Address2", :Address2], optional: true
pattern [:appeal, appeals/"County", :County], optional: true
pattern [:appeal, appeals/"Postcode", :Postcode], optional: true
end
query.execute(graph) {|appeal| ...}
- 使用SPARQL客户端:
醇>
require 'sparql/client'
appeals = RDF::URI("http://opendatacommunities.org/def/ontology/planning/pins-appeals/")
sparql = SPARQL::Client.new(graph)
query = sparql.select.where(
[:appeal, appeals/"CaseRef", :CaseRef],
[:appeal, appeals/"Address1", :Address1],
[:appeal, appeals/"Town", :Town]).
optional([:appeal, appeals/"Agent", :Agent]).
optional([:appeal, appeals/"Address2", :Address2]).
optional([:appeal, appeals/"County", :County]).
optional([:appeal, appeals/"Postcode", :Postcode])
query.each_solution {|appeal| puts appeal.inspect}
使用SPARQL:
q = %(
PREFIX appeals: <http://opendatacommunities.org/def/ontology/planning/pins-appeals/>
SELECT * WHERE {
?appeal appeals:CaseRef ?CaseRef ;
appeals:Address1 ?Address1 ;
appeals:Town ?Town .
OPTIONAL {?appeal appeals:Agent ?Agent}
OPTIONAL {?appeal appeals:Address2 ?Address2}
OPTIONAL {?appeal appeals:County ?County}
OPTIONAL {?appeal appeals:Postcode ?Postcode}
}
)
SPARQL.execute(q, graph).each_solution {|appeal| puts appeal.inspect}