使用tdbquery在TDB中查询命名的RDF图 [英] Querying named RDF graphs in TDB using tdbquery

查看:144
本文介绍了使用tdbquery在TDB中查询命名的RDF图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用tdbquery程序查询我新创建的TDB数据库.但是,我很难编写针对正确命名图的查询.我正在执行以下操作:

首先创建一个新的数据集并添加一个名为事实"的名称图

Dataset dataset = TDBFactory.createDataset("/tdb/");
dataset.begin(ReadWrite.WRITE) ;

try {
    Model facts = RDFDataMgr.loadModel("lineitem.ttl") ;
    dataset.addNamedModel("facts", facts);
    dataset.commit();
    TDB.sync(dataset);
    dataset.end();

} finally {
    dataset.close();
}

当我查询TDB数据库中的所有图形时,看起来不错.

./tdbquery --loc /tdb/ "SELECT * { GRAPH ?g { ?s ?p ?o } }"

--------------------------------------------------
| s         | p           | o          | g       |
==================================================
| <fact1>   | <predicate> | <nation>   | <facts> |
| <fact2>   | <predicate> | <region>   | <facts> |
--------------------------------------------------

如果我尝试查询命名图,我将找不到并增加三倍.

./tdbquery -v --loc /tdb/ "SELECT * { GRAPH <facts> { ?s ?p ?o } }"
OR
./tdbquery -v --loc /tdb/ "SELECT * FROM NAMED <facts> WHERE { ?s ?p ?o }"

-------------
| s | p | o |
=============
-------------

当我查看查询的代数版本时,我发现四元组中的上下文(图形)是错误的.

INFO  exec                 :: ALGEBRA
(quadpattern (quad <file:///usr/local/apache-jena-2.12.1/bin/facts> ?s ?p ?o))

我知道四边形图案应该是: (quad?s?p?o)

如何查询TDB数据库中的命名图?

致谢

解决方案

当我查看查询的代数版本时,我看到上下文 我的四边形中的(图形)是错误的.

INFO  exec                 :: ALGEBRA
(quadpattern (quad <file:///usr/local/apache-jena-2.12.1/bin/facts> ?s ?p ?o))

我知道四边形图案应该是:(quad ?s ?p ?o)

没有正确的答案(如果不是您期望的那样)

A quadpattern搜索四边形,因此包括4个字段,第一个是要搜索的图形的名称

这就是您的问题所在,图形名称是URI,但您仅提供facts作为名称,该名称被视为相对URI,因此受制于不同系统的解析度. >

在您的示例中,查询解析器使用工作目录作为基本URI,从而导致您在代数计划中看到的奇怪图形名称.

通过发出以下查询,您可以确切地看到TDB存储中的图形名称:

SELECT ?g WHERE { GRAPH ?g { } }

如果返回绝对URI,则可以直接在原始查询中指定它,如果没有,则无法从命令行进行查询.

解决您的问题

尽可能不要使用相对URI.如果您确实要使用它们,那么请在未显式指定基本URI的情况下使用它们

因此,在代码中要加载数据的位置,请确保为图形提供了绝对URI,例如

dataset.addNamedModel("http://example.org/facts", facts);

如果您确实希望能够使用相对URI在查询中引用图形,请使用适当的BASE声明,以便根据需要解析URI,例如

./tdbquery -v --loc /tdb/ "BASE <http://example.org/> SELECT * { GRAPH <facts> { ?s ?p ?o } }"

I am trying to query my newly created TDB database use the tdbquery program. However, I am having a hard time writing a query that targets the correct named graph. I am doing the following:

First a create a new dataset and add a name graph called "facts"

Dataset dataset = TDBFactory.createDataset("/tdb/");
dataset.begin(ReadWrite.WRITE) ;

try {
    Model facts = RDFDataMgr.loadModel("lineitem.ttl") ;
    dataset.addNamedModel("facts", facts);
    dataset.commit();
    TDB.sync(dataset);
    dataset.end();

} finally {
    dataset.close();
}

When I query all graphs in my TDB database it looks fine.

./tdbquery --loc /tdb/ "SELECT * { GRAPH ?g { ?s ?p ?o } }"

--------------------------------------------------
| s         | p           | o          | g       |
==================================================
| <fact1>   | <predicate> | <nation>   | <facts> |
| <fact2>   | <predicate> | <region>   | <facts> |
--------------------------------------------------

If I try to query the named graph I do not find and triples.

./tdbquery -v --loc /tdb/ "SELECT * { GRAPH <facts> { ?s ?p ?o } }"
OR
./tdbquery -v --loc /tdb/ "SELECT * FROM NAMED <facts> WHERE { ?s ?p ?o }"

-------------
| s | p | o |
=============
-------------

When I look at the algebra version of the query I see that the context (the graph) in my quad is wrong.

INFO  exec                 :: ALGEBRA
(quadpattern (quad <file:///usr/local/apache-jena-2.12.1/bin/facts> ?s ?p ?o))

I know that the quad pattern should be: (quad ?s ?p ?o)

How do I query a named graph in a TDB database?

Regards

解决方案

When I look at the algebra version of the query I see that the context (the graph) in my quad is wrong.

INFO  exec                 :: ALGEBRA
(quadpattern (quad <file:///usr/local/apache-jena-2.12.1/bin/facts> ?s ?p ?o))

I know that the quad pattern should be: (quad ?s ?p ?o)

No its correct (if not what you expect)

A quadpattern searches against quads and so includes 4 fields the first of which is the name of the graph to be searched

And this is where your problem lies, graph names are URIs but you only provided facts as the name which is treated as a relative URI and as such subject to resolution which may differ in different parts of the system.

In your example the query parser uses the working directory as the Base URI leading to the strange graph name you see in the algebra plan.

You can see exactly what graph names are in the TDB store by issuing the following query:

SELECT ?g WHERE { GRAPH ?g { } }

If you get an absolute URI back then you can specify that directly in your original query, if you do not then there is no way to query for it from the command line.

Fixing your Issue

Don't use relative URIs wherever possible. If you do want to use them then don't use them without specifying a base URI explicitly

So in your code where you load the data make sure you give an absolute URI to the graph e.g.

dataset.addNamedModel("http://example.org/facts", facts);

And if you do want to be able to use relative URIs to refer to your graph in your queries use an appropriate BASE declaration so the URI is resolved as you want it e.g.

./tdbquery -v --loc /tdb/ "BASE <http://example.org/> SELECT * { GRAPH <facts> { ?s ?p ?o } }"

这篇关于使用tdbquery在TDB中查询命名的RDF图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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