从sparql查询返回面向对象的json [英] Returning object oriented json from sparql queries

查看:84
本文介绍了从sparql查询返回面向对象的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想在SPARQL中请求一些不同的属性A和B, 其中有一个唯一的A,并且每个A可能有多个B.

If I want to ask for some different properties A and B, in SPARQL, where there is one unique A, and possibly several B's belonging to each A.

我得到的查询形式为:

A1, B1
A1, B2
A1, B3
A1, B4
A2, B5

我希望该查询结果看起来如何,其形式更多:

How I would want this query result to look, is more of the form:

A1: B1,B2,B3,B4
A2: B5

在SPARQL中有这样做的方法吗?这叫什么?

Is there a way of doing this in SPARQL? What is this called?

推荐答案

SPARQL的JSON输出记录在 SPARQL 1.1查询结果JSON格式,特别是 3.2.2编码RDF条款描述了如何在JSON中对RDF进行编码.请注意,JSON输出实际上仅用于编码SPARQL查询的结果,而不是用于创建与特定对象模型相对应的JSON对象.最好的选择可能是取得结果并自己操作.不过,从SPARQL方面来看,还有一些事情可能会有所帮助.

The JSON output for SPARQL is documented in SPARQL 1.1 Query Results JSON Format, and specifically section 3.2.2 Encoding RDF Terms describes how the RDF is encoded in the JSON. Note that the JSON output is really just for encoding the results of a SPARQL query, not for creating JSON objects that correspond to a particular object model. Your best bet is probably to take the results you're getting and manipulate them yourself. There are still a few things that might be helpful from the SPARQL side, though.

也就是说,以下内容可能会帮助您获得一些对您而言更好的东西.如果您有这样的数据:

That said, maybe the following can help you get something that will work a bit better for you. If you have data like this:

@prefix : <http://example.org/> .

:object :hasA1 :b1, :b2, :b3, :b4 ;
        :hasA2 :b5 .

和类似的查询:

prefix : <http://example.org/>

select ?subject ?property ?object
where {
  values ?property { :hasA1 :hasA2 }
  ?subject ?property ?object .
}

您将获得如下结果:

$ arq --data data.n3 --query query.sparql
-------------------------------
| subject | property | object |
===============================
| :object | :hasA1   | :b4    |
| :object | :hasA1   | :b3    |
| :object | :hasA1   | :b2    |
| :object | :hasA1   | :b1    |
| :object | :hasA2   | :b5    |
-------------------------------

您可以使用group_concathasA1的所有值组合为单个值,并使用以下查询:

You can use group_concat to combine all the values for hasA1 into a single value with a query like:

prefix : <http://example.org/>

select ?subject ?property (group_concat(?object;separator=',') as ?cobject)
where {
  values ?property { :hasA1 :hasA2 }
  ?subject ?property ?object .
}
group by ?subject ?property 

并获得如下结果:

$ arq --data data.n3 --query query.sparql
------------------------------------------------------------------------------------------------------------------
| subject | property | cobject                                                                                   |
==================================================================================================================
| :object | :hasA2   | "http://example.org/b5"                                                                   |
| :object | :hasA1   | "http://example.org/b4,http://example.org/b3,http://example.org/b2,http://example.org/b1" |
------------------------------------------------------------------------------------------------------------------

如果您要求使用JSON格式的结果,您将获得以下输出,可能对您有用,具体取决于您的b1b4实体类型.具体来说,如果它们是字符串,则在有意义的串联中,这可能很好.如果还有别的东西,那可能就没那么有用了.

If you ask for the results in JSON format, you'll get the following output, which might work for you, depending on what kinds of entities your b1b4 are. Specifically, if they're strings, where concatenation makes sense, this could be fine. If they're something else, it's probably not so useful.

$ arq -out JSON --data data.n3 --query query.sparql
{
  "head": {
    "vars": [ "subject" , "property" , "cobject" ]
  } ,
  "results": {
    "bindings": [
      {
        "subject": { "type": "uri" , "value": "http://example.org/object" } ,
        "property": { "type": "uri" , "value": "http://example.org/hasA2" } ,
        "cobject": { "type": "literal" , "value": "http://example.org/b5" }
      } ,
      {
        "subject": { "type": "uri" , "value": "http://example.org/object" } ,
        "property": { "type": "uri" , "value": "http://example.org/hasA1" } ,
        "cobject": { "type": "literal" , "value": "http://example.org/b4,http://example.org/b3,http://example.org/b2,http://example.org/b1" }
      }
    ]
  }
}

group_concatconstruct

那里有RDF的JSON序列化,尽管SPARQL引擎可能不支持它们,但是您可以使用construct查询生成一些结构更像您期望的形式的RDF,然后使用RDF序列化转换器以转换为JSON格式.例如,Jena的rdfcat支持 RDF/JSON 输出.使用这样的construct查询:

group_concat with construct

There are JSON serializations of RDF out there, and while they may not be supported by SPARQL engines, you could use a construct query to generate some RDF that is structured more like your desired form, and then use an RDF serialization converter to convert to a JSON format. For instance, Jena's rdfcat supports RDF/JSON output. Using a construct query like this:

prefix : <http://example.org/>

construct { 
 ?subject ?property ?cobject
}
where {
  select ?subject ?property (group_concat(?object;separator=',') as ?cobject)
  where {
    values ?property { :hasA1 :hasA2 }
    ?subject ?property ?object .
  }
  group by ?subject ?property 
}

(请参阅我们可以在answer.semanticweb.com上将CONSTRUCT与SPARQL 1.1中的聚合结合在一起?,以解释此查询中为什么存在嵌套查询的原因.)这将生成以下形式的RDF:

(See Can we combine CONSTRUCT with aggregates in SPARQL 1.1? on answers.semanticweb.com for an explanation of why there's a nested query in this query.) This generates RDF of the form:

$ arq --out RDF/XML --data data.n3 --query query.sparql 
<rdf:RDF
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
    xmlns="http://example.org/">
  <rdf:Description rdf:about="http://example.org/object">
    <hasA1>http://example.org/b4,http://example.org/b3,http://example.org/b2,http://example.org/b1</hasA1>
    <hasA2>http://example.org/b5</hasA2>
  </rdf:Description>
</rdf:RDF>

我们可以通过rdfcat用管道将其导出,以获取一些RDF/JSON,最终可能会更接近您要查找的内容:

We can pipe that through rdfcat to get some RDF/JSON out, which may finally be closer to what you're looking for:

$ arq --out RDF/XML --data data.n3 --query query.sparql | rdfcat -out RDF/JSON /dev/stdin 
{ 
  "http://example.org/object" : { 
    "http://example.org/hasA2" : [ { 
      "type" : "literal" ,
      "value" : "http://example.org/b5"
    }
     ] ,
    "http://example.org/hasA1" : [ { 
      "type" : "literal" ,
      "value" : "http://example.org/b4,http://example.org/b3,http://example.org/b2,http://example.org/b1"
    }
     ]
  }
}

这篇关于从sparql查询返回面向对象的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆