Comments inline: On 05.02.21 06:46, Jit K wrote: > Hello, > > While storing data in TDB, I am creating a named graph per entity and keeping > all triples related to it into this graph. However, while searching data I am > finding it difficult to write queries. For example, consider the following > data: > > <person-1.ttl> { > <person-1> > <rdf:type> "person" ; > <name> "person1" ; > <like> "sports" . > } > > <person-2.ttl> { > <person-2> > <rdf:type> "person" ; > <name> "person2" ; > <like> "music" . > } > > <person-3.ttl> { > <person-3> > <rdf:type> "person" ; > <name> "person3" ; > <like> "tracking" . > } > > <person-4.ttl> { > <person-4> > <rdf:type> "person" ; > <name> "person4" ; > <like> "sports" . > } > > Now, I want to get all person entities. So I used a query > SELECT * WHERE { GRAPH ?g { ?s <rdf:type> "person"} } > > This returns me a list of all graphs (in my Java code). However, I want to > get the entities. So the option I see is to send again one query for each > graph to get all the triples and read that person entity from them. Not sure if I understand you correctly, but what does not work? I mean, you're doing "SELECT *" which means you get the bindings of all variables in your query which means, you'll also get the person entities. Indeed, doing "SELECT ?s" would be sufficient as well. But maybe I didn't get the exact problem, so in that case can you rephrase or clarify? > > I have 2 questions here: > 1. Is this a good way to store each entity in its own graph? If yes, what can > the situations where it be useful? It depends, I'd say - as usual. In the end, everything depends on how you want to use and access the data. > 2. In the above case how I can write my sparql to span across all graphs to > get all the triples from all the graphs? Just append ?s ?p ?o, or not? Like
SELECT ?s ?p ?o WHERE { GRAPH ?g { ?s <rdf:type> "person"; ?p ?o} } By the way, is there a reason for using string literals among all your data? I mean, for :name property ok. But for rdf:type which indicates a class of entities and for :like I'd rather go with proper URIs because this is one of the nice things of RDF, being able to express also data about the things that you used as values. If this was just for showing the example, ignore my comment, otherwise, you should rethink your modelling. > > Thanks, > JK >