简体   繁体   English

加入SPARQL查询

[英]Join SPARQL Query

I have multiple graphs in RDF Knowledgebase 我在RDF知识库中有多个图形

for example
<123> <hasValue> "23" <graph1>
<234> <hasValue> "47" <graph1>
<374> <hasValue> "23" <graph1>
-----------
----------
<456> <hasFeature> "50" <graph2>
<244> <hasFeature> "23" <graph2>
<123> <hasFeature> "23" <graph2>
---------------------

Now I wan to run SPARQL query to get the results which is common in both graphs. 现在,我要运行SPARQL查询以获取两个图中都常见的结果。

suppose If I run the following query for one graph I get the following result 假设如果对一个图形运行以下查询,则会得到以下结果

SELECT ?subject 
FROM Named <http://www.xyz.com/namespace/graph1>
WHERE {GRAPH ?graph
   {?subject prefix:hasValue "23" .}} 

<123>
<374>
---
---
---

if run the following second query for graph2 I get the following 如果运行下面的第二个查询graph2我得到以下内容

SELECT ?subject 
FROM Named <http://www.xyz.com/namespace/graph2>
WHERE {GRAPH ?graph
   {?subject prefix:hasFeature "23" .}} 

<244>
<123>
-----

However I want the subject <123> which is common in both queries. 但是我想要在两个查询中都通用的主题<123>。 Is any way we can combine both queries to get the only subject which is common in both queries. 无论如何,我们都可以将两个查询组合在一起以获得唯一在两个查询中都通用的主题。 Thanks in advance. 提前致谢。

Yes, joining in SPARQL is simply a case of stating multiple patterns that have common variables. 是的,加入SPARQL只是说明具有公共变量的多个模式的情况。 So we can mostly just cut and past the your two queries together: 因此,我们通常可以将两个查询一起剪切和删除:

SELECT ?subject 
FROM NAMED <http://www.xyz.com/namespace/graph1>
FROM NAMED <http://www.xyz.com/namespace/graph2>
WHERE 
{
  GRAPH <http://www.xyz.com/namespace/graph1> 
  {
    ?subject prefix:hasValue "23" .
  }
  GRAPH <http://www.xyz.com/namespace/graph2> 
  {
    ?subject prefix:hasFeature "23" .
  }
} 

Note that since you're now querying the two graphs separately we need to explicitly name the graph for each of the GRAPH clauses. 请注意,由于您现在要分别查询两个图,因此我们需要为每个GRAPH子句显式命名该图。 We can't just use a single GRAPH ?graph pattern because that matches each named graph individually and unions the results together which does not have the semantics you desire. 我们不能只使用一个GRAPH ?graph模式,因为它会单独匹配每个命名的图,并将结果合并在一起,而这些语义不具有您想要的语义。

With this query the results for the two graphs will now be joined together so you'll only get results where the pattern matches in both graphs. 通过此查询,两个图形的结果现在将结合在一起,因此您将仅获得两个图形中的模式匹配的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM