sparql - RDF list subjects with their objects in a single line -
i have rdf file , need extract information , write file. understood how works, i'm stuck this:
string querystring = "select ?person ?children { ?person ?haschildren ?children}"; tuplequery tuplequery = conn.preparetuplequery(querylanguage.sparql, querystring); tuplequeryresult result = tuplequery.evaluate(); while (result.hasnext()) { bindingset bindingset = result.next(); value p1 = bindingset.getvalue("person"); value p2 = bindingset.getvalue("child"); println(p1 + " has children " + p2 +""); } result.close();
the output this:
http://example.org/people/person1 has children http://example.org/people/child1 http://example.org/people/person1 has children http://example.org/people/child2
i don't see how list persons objects in format:
person1 has children child1 , child2
how can done?
you may find answer, describes sparql's group_concat
, useful:
in sparql, when have result set of query solutions, can group
on 1 or more of variables, merging solutions have these variables in common. instance, consider data
@prefix : <http://example.org/people/>. :person1 :haschild :child1, :child2, :child3 . :person2 :haschild :child4, :child5 . :person3 :haschild :child6 .
if run following query on it
prefix : <http://example.org/people/> select ?person ?child { ?person :haschild ?child . }
you results this:
$ arq --data data.n3 --query query.sparql ---------------------- | person | child | ====================== | :person3 | :child6 | | :person2 | :child5 | | :person2 | :child4 | | :person1 | :child3 | | :person1 | :child2 | | :person1 | :child1 | ----------------------
iterating through results have in question produce type of output you're getting. we'd results like:
$ arq --data data.n3 --query query.sparql ---------------------------------------- | person | child | ======================================== | :person3 | :child6 | | :person2 | :child4, :child5 | | :person1 | :child1, :child2, :child3 | ----------------------------------------
and that's group_by
lets do. query this:
prefix : <http://example.org/people/> select ?person (group_concat(?child;separator=' , ') ?children) { ?person :haschild ?child . } group ?person
produces (notice variable in result ?children
, not ?child
, because we've used group_concat(...) ?children
create new variable ?children
):
$ arq --data data.n3 --query query.sparql --------------------------------------------------------------------------------------------------------------------------- | person | children | =========================================================================================================================== | :person3 | "http://example.org/people/child6" | | :person1 | "http://example.org/people/child3 , http://example.org/people/child2 , http://example.org/people/child1" | | :person2 | "http://example.org/people/child5 , http://example.org/people/child4" | ---------------------------------------------------------------------------------------------------------------------------
if use query , iterate through results, printing them have, you'll output want. if want strip leading http://example.org/people/
off persons , children, you'll need bit more string processing. instance, using strafter remove http://example.org/people/
prefix, can use query this:
prefix : <http://example.org/people/> select (strafter(str(?personx),"http://example.org/people/") ?person) (group_concat(strafter(str(?child),"http://example.org/people/");separator=' , ') ?children) { ?personx :haschild ?child . } group ?personx
to results like:
$ arq --data data.n3 --query query.sparql ---------------------------------------------- | person | children | ============================================== | "person3" | "child6" | | "person2" | "child5 , child4" | | "person1" | "child3 , child2 , child1" | ----------------------------------------------
which, when printing, give results like
person3 has children child6 person2 has children child5 , child4 person1 has children child3 , child2 , child1
Comments
Post a Comment