【问题标题】:Can't connect to a SPARQLRepository unsing openrdf (sesame), in the mapper class of a Hadoop/Mapreduce job无法在 Hadoop/Mapreduce 作业的映射器类中连接到 SPARQLRepository unsing openrdf(芝麻)
【发布时间】:2017-08-24 21:35:39
【问题描述】:

我确实使用 Sesame (RDF4j) API 编写了一个 Java 应用程序来测试 >700 个 SPARQL 端点的可用性,但它需要几个小时才能完成,所以我正在尝试使用 Hadoop/MapReduce 框架分发这个应用程序。

现在的问题是,在映射器类中,应该测试可用性的方法不起作用,我认为无法连接到端点。

这里是我使用的代码:

public class DMap extends Mapper<LongWritable, Text, Text, Text> {

protected boolean isActive(String sourceURL)
        throws RepositoryException, MalformedQueryException, QueryEvaluationException {
    boolean t = true;
    SPARQLRepository repo = new SPARQLRepository(sourceURL);
    repo.initialize();
    RepositoryConnection con = repo.getConnection();
    TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT * WHERE{ ?s ?p ?o . } LIMIT 1");
    tupleQuery.setMaxExecutionTime(120);
    TupleQueryResult result = tupleQuery.evaluate();
    if (!result.hasNext()) {
        t = false;
    }
    con.close();
    result.close();
    repo.shutDown();
    return t;
}

public void map(LongWritable key, Text value, Context context) throws InterruptedException, IOException {
    String src = value.toString();
    String val = "null";
    try {
        boolean b = isActive(src); 
        if (b) {
            val = "active";
        } else {
            val = "inactive";
        }
    } catch (MalformedQueryException e) {
        e.printStackTrace();
    } catch (RepositoryException e) {
        e.printStackTrace();
    } catch (QueryEvaluationException e) {
        e.printStackTrace();
    }
    context.write(new Text(src), new Text(val));
}
}

输入是一个 TextInputFormat,它看起来像这样:
http://visualdataweb.infor.uva.es/sparql
...

输出是 TextOutputFormat,我得到这个:
http://visualdataweb.infor.uva.es/sparql null
...

Edit1:正如@james-leigh 和@ChristophE 所建议的,我使用了try-with-resource 语句,但还没有结果:

public class DMap extends Mapper<LongWritable, Text, Text, Text> {

    public void map(LongWritable key, Text value, Context context) throws InterruptedException, IOException {
        String src = value.toString(), val = "";
        SPARQLRepository repo = new SPARQLRepository(src);
        repo.initialize();
        try (RepositoryConnection con = repo.getConnection()) {
            TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, "SELECT * WHERE { ?s ?p ?o . } LIMIT 1");
            tupleQuery.setMaxExecutionTime(120);
            try (TupleQueryResult result = tupleQuery.evaluate()) {
                if (!result.hasNext()) {
                    val = "inactive";
                } else {
                    val = "active";
                }
            }

        }
        repo.shutDown();
        context.write(new Text(src), new Text(val));

    }

}  

谢谢

【问题讨论】:

  • 呃...有什么问题?
  • 我应该怎么做才能做到这一点,因为正如您在输出中看到的那样,我得到了空值,它应该是“活动”或“非活动”。

标签: java mapreduce hadoop2 sesame rdf4j


【解决方案1】:

使用 try-with-resource 语句。 SPRAQLRepository 使用必须正确清理的后台线程。

【讨论】:

  • 对不起,这是我第一次阅读有关 try-resource statments docs.oracle.com/javase/tutorial/essential/exceptions/…> 的内容。我会读这个,看看我能做什么。非常感谢
  • 您好,抱歉,我没说对,SPARQLRepository 没有实现 AutoCloseable,所以我尝试了 RepositoryConnection 和 TupleQueryResult,它说它们没有实现 AutoCloseable。但他们扩展了它。
  • 嗨@ChristophE,我确实迁移到了最新的RDF4J API 2.2 并使用了try-with-resource 语句,但也没有帮助,现在我遇到了一些我无法使用的Hadoop 错误不明白,所以我确实问了new question,看看是什么导致了这个错误。
猜你喜欢
  • 2012-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-08
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多