You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Jit K <jk...@gmail.com> on 2021/02/05 05:46:41 UTC
What is a better strategy to store data in triple store?
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.
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?
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?
Thanks,
JK
Re: What is a better strategy to store data in triple store?
Posted by Jit K <jk...@gmail.com>.
On 2021/02/05 06:09:27, Lorenz Buehmann <bu...@informatik.uni-leipzig.de> wrote:
Thank you Lorenz. This helped me.
My comments inline:
> 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?
Sorry if I was not clear in explaining the problem. I am new to RDF/SPARQL. I am in the intial phase of my work. Since I was not able to write the sparql to get all triples from all graphs, I thought my data storing approach may prove inefficient in future.
As you said "SELECT ?s" would be sufficient. It gives me list of entities - to be more correct "entity URIs". I am talking all this from my client code perspective. With the URIs I thought I will have to resend another query to get the triples. But it is my lack of SPARQL knowledge. As you explained below, I have modified my query and getting all the entities with their triples in one go. Thank you.
> >
> > 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.
I agree. Can you point some good references for writing SPARQL queries?
> > 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.
Thank you. This helped me.
I have used string literals just for describing the example. I am using URIs for referencing other entities.
> >
> > Thanks,
> > JK
> >
>
>
Re: What is a better strategy to store data in triple store?
Posted by Lorenz Buehmann <bu...@informatik.uni-leipzig.de>.
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
>