如何在RDF4J控制台中使用PREFIX输入查询?

时间:2017-12-05 19:44:50

标签: rdf4j

sparql> PREFIX ab: <http://learningsparql.com/ns/addressbook#>
Executing update...
Update executed in 160 ms
sparql> SELECT ?craigEmail WHERE { ?person ab:firstName "Craig" }
Malformed query: org.eclipse.rdf4j.query.parser.sparql.ast.VisitorException: 
QName 'ab:firstName' uses an undefined prefix

sparql> SELECT ?craigEmail WHERE { ?person 
<http://learningsparql.com/ns/addressbook/firstName> "Craig" }
Evaluating SPARQL query...
+-----------------------------------------------------------------------------+
| craigEmail                                                                  |
+-----------------------------------------------------------------------------+
+-----------------------------------------------------------------------------+
0 result(s) (64 ms)

2 个答案:

答案 0 :(得分:1)

尝试将前缀放在与查询相同的行上(所以一切都在一行上)。

答案 1 :(得分:1)

默认情况下,RDF4J控制台只接受单行SPARQL查询,因此James在答案中建议:将查询作为单行输入。

或者,您可以使用sparql命令进入多行模式:

memory> sparql
enter multi-line SPARQL query (terminate with line containing single '.')
PREFIX ab: <http://learningsparql.com/ns/addressbook#>
SELECT ?craigEmail WHERE { ?person ab:firstName "Craig" }
.
Evaluating SPARQL query...
+-----------------------------------------------------------------------------+
| craigEmail                                                                  |
+-----------------------------------------------------------------------------+
+-----------------------------------------------------------------------------+
0 result(s) (93 ms)
memory>

当您复制粘贴较大的查询时,这尤其有用。

顺便说一句,你要做的第一个“查询”:

sparql> PREFIX ab: <http://learningsparql.com/ns/addressbook#>
Executing update...
Update executed in 160 ms

输出具有误导性,因为“已执行更新”表明您刚刚添加了新的命名空间前缀。但那不是正在发生的事情。相反,控制台将此解释为缺少“正文”的SPARQL更新,并将其作为空更新执行。前缀被忽略。可以说这是SPARQL解析器中的错误,因为严格来说,没有正文的前缀声明不是合法查询。

没有“官方”方法通过控制台向RDF4J存储添加新的名称空间前缀,但是有一个技巧可以使用(尽管只有在商店或SPARQL端点上执行它时才会有效)你有写访问权限):

PREFIX ab: <http://learningsparql.com/ns/addressbook#> INSERT DATA {}

这将执行SPARQL更新以插入新的三元组,但是使用空块(因此不插入实际的三元组)。但是,此将名称空间前缀添加到存储库,之后您不再需要为进入控制台的每个查询声明它:

memory> show n
--no namespaces found--
memory> PREFIX ab: <http://learningsparql.com/ns/addressbook#> INSERT DATA {}
Executing update...
Update executed in 3 ms
memory> show n
+----------
|ab  http://learningsparql.com/ns/addressbook#
|rdf  http://www.w3.org/1999/02/22-rdf-syntax-ns#
|owl  http://www.w3.org/2002/07/owl#
|xsd  http://www.w3.org/2001/XMLSchema#
|fn  http://www.w3.org/2005/xpath-functions#
|rdfs  http://www.w3.org/2000/01/rdf-schema#
|sesame  http://www.openrdf.org/schema/sesame#
+----------
memory> SELECT ?craigEmail WHERE { ?person ab:firstName "Craig" }
Evaluating SPARQL query...
+-----------------------------------------------------------------------------+
| craigEmail                                                                  |
+-----------------------------------------------------------------------------+
+-----------------------------------------------------------------------------+
0 result(s) (47 ms)
memory>