You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Jena查询构建器实现指定的RDF-star SPARQL查询?

Java Jena Query Builder Implementation for RDF-star Query

Below is the correct Java code to construct your target SPARQL query using Jena's SelectBuilder, properly handling the RDF-star triple subject and prefix configuration:

import org.apache.jena.query.Query;
import org.apache.jena.sparql.builder.SelectBuilder;
import org.apache.jena.sparql.core.Var;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.Triple;
import org.apache.jena.rdf.model.ResourceFactory;

public class RdfStarQueryBuilder {
    public static void main(String[] args) {
        // Define query variable
        Var cityVar = Var.alloc("city");

        // Create nodes for the query elements
        Node athensNode = ResourceFactory.createResource("https://squery.org/joy/athens").asNode();
        Node connectedProp = ResourceFactory.createProperty("https://squery.org/joy/connected").asNode();
        Node distanceProp = ResourceFactory.createProperty("https://squery.org/joy/distance").asNode();
        Node fiveHundredLit = ResourceFactory.createTypedLiteral(500).asNode();

        // Create the RDF-star triple node (used as the subject in the outer triple)
        Triple innerTriple = Triple.create(athensNode, connectedProp, cityVar);
        Node tripleSubject = Node.createTripleNode(innerTriple);

        // Build the query
        Query query = new SelectBuilder()
                .addVar(cityVar)
                .addPrefix(":", "https://squery.org/joy/")
                .addWhere(tripleSubject, distanceProp, fiveHundredLit)
                .build();

        // Print the generated SPARQL query
        System.out.println(query.toString());
    }
}

内容的提问来源于stack exchange,提问作者Takis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 03:33:19