Last active
December 13, 2015 20:39
-
-
Save afiore/4971901 to your computer and use it in GitHub Desktop.
SPARQL aggregate query example using Ruby SPARQL::Client gem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'sparql/client' | |
ENDPOINT_URL = "http://localhost:3030/data/query" | |
query = | |
SPARQL::Client.new(ENDPOINT_URL, :method => 'post') | |
.select(:person, :count => {:other_person => :count} ) | |
.where([:person, RDF::FOAF.knows, :other_person]) | |
.group(:person) | |
.order("DESC(?count)") | |
# => SELECT ?person ( COUNT(?other_person) AS ?count ) WHERE { ?person <http://xmlns.com/foaf/0.1/knows> ?other_person . } GROUP BY ?person ORDER BY DESC(?count) | |
query.solutions.map(&:to_hash) | |
# => [{:person=>#<RDF::URI:0xac81b8(http://example.org/alice)>, | |
# :count=> | |
# #<RDF::Literal::Integer:0xac9a7c("3"^^<http://www.w3.org/2001/XMLSchema#integer>)>}, | |
# {:person=>#<RDF::URI:0xa99340(http://example.org/bob)>, | |
# :count=> | |
# #<RDF::Literal::Integer:0xa97edc("1"^^<http://www.w3.org/2001/XMLSchema#integer>)>}, | |
# {:person=>#<RDF::URI:0x98c4fc(http://example.org/charlie)>, | |
# :count=> | |
# #<RDF::Literal::Integer:0x98fee0("1"^^<http://www.w3.org/2001/XMLSchema#integer>)>}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment