简体   繁体   English

以编程方式查询芝麻时,如何获得一致的结果序列?

[英]How to get a consistent sequence of results when querying Sesame programmatically?

I have some data on a Sesame triplestore. 我在芝麻三元组上有一些数据。 When I query it using the GUI, the sequence of triples returned remains same irrespective of how many times I query it. 当我使用GUI查询它时,无论查询多少次,返回的三元组序列都相同。 When I try the same thing programmatically, the sequence keeps changing (although the results are the same). 当我以编程方式尝试相同的操作时,顺序会不断变化(尽管结果是相同的)。 Can someone please explain why this is the case and what I can do to ensure that the results are returned in the same order? 有人可以解释为什么会这样吗,我该怎么做才能确保以相同的顺序返回结果?

This is my code: 这是我的代码:

sesameSparqlEndpoint = 'http://my.ip.ad.here:8080/openrdf-sesame/repositories/rep_name'
sparql = SPARQLWrapper(sesameSparqlEndpoint)
queryStringDownload = 'SELECT * WHERE {?s ?p ?o} LIMIT 10 OFFSET 1000'
dataGraph = Graph()

sparql.setQuery(queryStringDownload)
sparql.method = 'GET'
sparql.setReturnFormat(JSON)
output = sparql.query().convert()
print output

The order in which a SPARQL query returns its results is undefined, and any SPARQL engine is completely free to return results in any order it sees fit. SPARQL查询返回结果的顺序是不确定的,并且任何SPARQL引擎完全可以自由选择以其认为合适的顺序返回结果。 Depending on the database implementation, and what techniques it uses for query optimisation, serialization, indexing, compression, etc the result for the exact same query can be in a different order each time you execute the query. 取决于数据库的实现及其用于查询优化,序列化,索引编制,压缩等的技术,每次执行查询时,完全相同的查询的结果可能会以不同的顺序排列。

The above is true for any SPARQL engine, by the way, not just Sesame. 顺便说一下,以上内容适用于所有 SPARQL引擎,而不仅仅是芝麻。 Even if you find a database that seems to return the results in the same order every time, this is not behaviour that you should rely on, since it will not be guaranteed behaviour and whenever that database releases a new version, it may suddenly change. 即使您发现每次看起来似乎都以相同顺序返回结果的数据库,也不是您应该依靠的行为,因为这不能保证行为,并且只要该数据库发布新版本,它就可能突然改变。

However, SPARQL has a built-in operator to influence the order in which results are returned: ORDER BY . 但是,SPARQL具有内置的运算符来影响返回结果的顺序: ORDER BY If you wish to execute a query and be certain that the results are returned in a certain fixed order, you need to use this. 如果您希望执行查询并确定结果以固定的顺序返回,则需要使用它。

TL;DR: adapt your SPARQL query, like this: TL; DR:调整您的SPARQL查询,如下所示:

SELECT * WHERE {?s ?p ?o} ORDER BY ?s LIMIT 10 OFFSET 1000

NB this particular query is potentially very expensive. 注意,此特定查询可能非常昂贵。 You are asking for all triples in the database - and even though you are limiting the eventual result to 10, it may still need to range over a large part of the complete database to be able to properly order the result. 您正在要求数据库中的所有三元组-即使将最终结果限制为10,它可能仍需要在整个数据库的很大一部分范围内才能正确排序结果。

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

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