SPARQL查询结果中的列和行

时间:2012-03-20 18:58:12

标签: rdf sparql semantic-web dbpedia linked-data

我在这里遇到问题,看起来很简单,但因为我对Sparal查询并不熟悉,所以我找不到解决方案。

PREFIX onto: <http://dbpedia.org/ontology/>
PREFIX resu: <http://dbpedia.org/resource/>
PREFIX yago: <http://dbpedia.org/class/yago/>

SELECT 

count(?MontrealC) as ?Montrea
count(?TorontoC) as ?Toronto
count(?OttawaC) as ?Ottawa
count(?VancouverC) as ?Vancouver


WHERE
{
{?MontrealC rdf:type yago:HotelsInMontreal} UNION
{?TorontoC rdf:type yago:HotelsInToronto} UNION
{?OttawaC rdf:type yago:HotelsInOttawa} UNION
{?VancouverC a onto:Hotel .
{?VancouverC onto:location resu:Vancouver} UNION
{?VancouverC onto:location ?street .
?street a yago:StreetsInVancouver}} UNION
{?VancouverC a yago:HotelsInBritishColumbia .
{?VancouverC onto:location resu:Vancouver} UNION
{?VancouverC onto:location ?street.
?street a yago:StreetsInVancouver}
}}

在任何dbpedia端点中应用此查询后,您将获得两行和四列显示的结果,如下所示:

Montreal    Toronto     Ottawa    Vancouver
8           28          5         10

问题是我需要将它们拖入列和四行,如下所示:

Montreal    Toronto
8           28 
Ottawa    Vancouver
5         10

SPARQL查询中有可能吗?

1 个答案:

答案 0 :(得分:3)

对于一个SPARQL查询,这是不可能的。然而,它应该不是一个大问题,因为您已经获得了正确的数据,并且您只想对其进行不同的表示。您能否以这种方式提供有关数据需求的更多背景信息,您询问的查询数量以及手动执行或嵌入某些程序(如果是,您使用哪种编程语言)?

如果您真的想要这样,请将其拆分为两个查询并附加结果:

查询1

PREFIX onto: <http://dbpedia.org/ontology/>
PREFIX resu: <http://dbpedia.org/resource/>
PREFIX yago: <http://dbpedia.org/class/yago/>

SELECT 

count(?MontrealC) as ?Montreal
count(?TorontoC) as ?Toronto

WHERE
{
{?MontrealC rdf:type yago:HotelsInMontreal} UNION
{?TorontoC rdf:type yago:HotelsInToronto}
}

查询2

PREFIX onto: <http://dbpedia.org/ontology/>
PREFIX resu: <http://dbpedia.org/resource/>
PREFIX yago: <http://dbpedia.org/class/yago/>

SELECT 

count(?OttawaC) as ?Ottawa
count(?VancouverC) as ?Vancouver

WHERE
{
{?OttawaC rdf:type yago:HotelsInOttawa} UNION
{?VancouverC a onto:Hotel .
{?VancouverC onto:location resu:Vancouver} UNION
{?VancouverC onto:location ?street .
?street a yago:StreetsInVancouver}} UNION
{?VancouverC a yago:HotelsInBritishColumbia .
{?VancouverC onto:location resu:Vancouver} UNION
{?VancouverC onto:location ?street.
?street a yago:StreetsInVancouver}
}}

合并

  • 将结果保存为query1.csv和query2.csv
  • 在终端中输入“cat query1.csv query2.csv&gt; query.csv”

    $ cat query1.csv query2.csv
    "Montrea","Toronto"
    8,28
    "Ottawa","Vancouver"
    5,10