首页 文章

Sparql 查询永远运行

提问于
浏览
6

我正在努力在 Jena 中执行 SPARQL 查询,导致我不理解的行为......

我正在尝试查询 Esco 本体(https://ec.europa.eu/esco/download),我正在使用 TDB 加载本体并创建模型(对不起,如果我使用的术语不准确,我不是很有经验)。

我的目标是在本体中找到与我之前提取的文本匹配的工作位置:ex:提取的术语:“acuponcteur” - >本体中的标签:“Acuponcteur”@fr - > uri:<http://ec.europa.eu/esco/occupation/14918>

我称之为“奇怪的行为”与我在获取(或不是)查询时得到的结果有关,即:

执行以下查询时:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#> 
PREFIX esco: <http://ec.europa.eu/esco/model#>      
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>   
SELECT ?position    
WHERE {     
    ?s rdf:type esco:Occupation. 
    { ?position skos:prefLabel ?label. } 
    UNION 
    { ?position skos:altLabel ?label. } 
    FILTER (lcase(?label)= \"acuponcteur\"@fr ) 
}
LIMIT 10

我在 1 分钟后得到这些结果:

-----------------------------------------------
| position                                    |
===============================================
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
| <http://ec.europa.eu/esco/occupation/14918> |
-----------------------------------------------

但是,当我尝试添加 DISTINCT 关键字时,因此:

PREFIX skos: <http://www.w3.org/2004/02/skos/core#> 
PREFIX esco: <http://ec.europa.eu/esco/model#>      
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>   
SELECT DISTINCT ?position   
WHERE {     
    ?s rdf:type esco:Occupation. 
    { ?position skos:prefLabel ?label. } 
    UNION 
    { ?position skos:altLabel ?label. } 
    FILTER (lcase(?label)= \"acuponcteur\"@fr ) 
}
LIMIT 10

好像查询一直在运行(我在 20 分钟后停止了执行 waiting...)

在执行与第一个查询相同的查询时(因此没有 DISTINCT),我得到相同的行为,另一个标签要匹配,我确定不在本体中的标签。期待空的结果,它(似乎它)继续运行,我必须在一段时间后杀死它(再一次,我等了 20 分钟到最多):

PREFIX skos: <http://www.w3.org/2004/02/skos/core#> 
PREFIX esco: <http://ec.europa.eu/esco/model#>      
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>   
SELECT ?position    
WHERE {     
    ?s rdf:type esco:Occupation. 
    { ?position skos:prefLabel ?label. } 
    UNION 
    { ?position skos:altLabel ?label. } 
    FILTER (lcase(?label)= \"assistante scolaire\"@fr ) 
}
LIMIT 10

可能是我正在运行的代码中的问题?它是:

public static void main(String[] args) {

    // Make a TDB-backed dataset
    String directory = "data/testtdb" ;
    Dataset dataset = TDBFactory.createDataset(directory) ;

    // transaction (protects a TDB dataset against data corruption, unexpected process termination and system crashes)
    dataset.begin( ReadWrite.WRITE );
    // assume we want the default model, or we could get a named model here
    Model model = dataset.getDefaultModel();

    try {

          // read the input file - only needs to be done once
          String source = "data/esco.rdf";
          FileManager.get().readModel(model, source, "RDF/XML-ABBREV");

          // run a query

          String queryString =
                    "PREFIX skos: <http://www.w3.org/2004/02/skos/core#> " +
                    "PREFIX esco: <http://ec.europa.eu/esco/model#> " +     
                    "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " +  
                    "SELECT ?position " +   
                    "WHERE { "  +   
                    "   ?s rdf:type esco:Occupation. " +
                    "   { ?position skos:prefLabel ?label. } " +
                    "   UNION " +
                    "   { ?position skos:altLabel ?label. }" +
                    "   FILTER (lcase(?label)= \"acuponcteur\"@fr ) " +
                    "}" +
                    "LIMIT 1 "  ;

          Query query = QueryFactory.create(queryString) ;

          // execute the query
          QueryExecution qexec = QueryExecutionFactory.create(query, model) ;
          try {
              ResultSet results = qexec.execSelect() ;
              // taken from apache Jena tutorial 
              ResultSetFormatter.out(System.out, results, query) ;

          } finally { 
              qexec.close() ; 
          }

      } finally {
          model.close() ;
          dataset.end();
      }

}

我在这做错了什么?任何的想法?

谢谢!

1 回答

  • 6

    作为可能会或可能没有太大区别的第一点,您可以使用属性路径来简化

    { ?position skos:prefLabel ?label. } 
    UNION 
    { ?position skos:altLabel ?label. }
    

    ?position skos:prefLabel|skos:altLabel ?label
    

    这使得查询:

    SELECT ?position    
    WHERE {     
        ?s rdf:type esco:Occupation.                   # (1)
        ?position skos:prefLabel|skos:altLabel ?label  # (2)
        FILTER (lcase(?label)="acuponcteur"@fr ) 
    }
    

    在这个查询中有什么意义?有一些匹配(2)的?位置/?标签对的数量 n,以及匹配(1)的某些数量 m 的?s。从查询中获得的结果数量是 m×n,但是您从不使用?s 的值。看起来你使用 DISTINCT 来摆脱一些重复的值,但是你没有看到为什么你首先得到重复的值。你应该简单地删除无用的行(1),并具有查询:

    SELECT DISTINCT ?position    
    WHERE {     
        ?position skos:prefLabel|skos:altLabel ?label
        FILTER (lcase(?label)="acuponcteur"@fr ) 
    }
    

    如果在这一点上你甚至不再需要 DISTINCT,我也不会感到惊讶。

相关问题