RDF triples to a table

594 views Asked by At

I have a triple store with RDF triples and I want to export the data to a table where columns represent the predicates. For example, if I have the following triples

:s1 :p1 "v11"
:s1 :p2 "v12"
:s2 :p2 "v22"
:s2 :p3 "v23"

I want it to be as follows

----|  p1    | p2  | p3
s1  |  v11   | v12 | (null)
s2  | (null) | v22 | v23

This might seems a little odd, as in most cases we need to export the other way but here I want to feed this data into a data mining software.

1

There are 1 answers

3
René On BEST ANSWER

USE OPTIONAL

If you know the predicates in advance, you can wrap each predicate in OPTIONAL to get all predicates in the same row for all subjects - even if some are missing. Here is an example:

SELECT ?name ?birth ?death
WHERE {
?person foaf:name ?name .
?person dbo:birthPlace :Berlin .
OPTIONAL { ?person dbo:birthDate ?birth . }
OPTIONAL { ?person dbo:deathDate ?death .}
}
ORDER BY ?name
LIMIT 1000

I keep my original answer for reference:

Use UNION (creates separate rows for each predicate)

If you know the predicates in advance, you can use UNION to get all predicates for all subjects - even if some are missing. Here is an example:

SELECT ?name ?birth ?field
WHERE {
?person foaf:name ?name .
?person dbo:birthPlace :Berlin .
{
?person dbo:birthDate ?birth .
} UNION {
?person dbo:field ?field .
}}
ORDER BY ?name
LIMIT 100