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

如何使用Jena与Spring Boot在Virtuoso中创建新图

在Spring Boot中用Jena操作Virtuoso创建图并上传TTL文件

1. 依赖配置(Maven pom.xml)

首先添加Jena核心库和Virtuoso Jena驱动的依赖:

<dependencies>
    <!-- Jena核心查询与RDF处理依赖 -->
    <dependency>
        <groupId>org.apache.jena</groupId>
        <artifactId>jena-arq</artifactId>
        <version>4.9.0</version>
    </dependency>
    <!-- Virtuoso与Jena的适配驱动 -->
    <dependency>
        <groupId>org.openlinksw.virtuoso</groupId>
        <artifactId>virtuoso-jena-driver</artifactId>
        <version>4.2.1</version>
    </dependency>
    <!-- Spring Boot基础依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>

2. 核心服务实现

创建封装Virtuoso操作的服务类,包含图创建、清空、TTL上传逻辑:

import org.apache.jena.query.Dataset;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.riot.RDFDataMgr;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;

@Service
public class VirtuosoGraphService {

    // Virtuoso连接参数,可通过@Value从application.yml读取
    private static final String VIRTUOSO_URL = "jdbc:virtuoso://localhost:1111";
    private static final String USERNAME = "dba";
    private static final String PASSWORD = "dba";

    private final ResourceLoader resourceLoader;

    public VirtuosoGraphService(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    /**
     * 显式创建指定URI的图
     * @param graphUri 图的唯一标识URI,例如"http://example.com/my-custom-graph"
     */
    public void createGraph(String graphUri) {
        String createQuery = String.format("CREATE GRAPH <%s>", graphUri);
        try (Dataset dataset = DatasetFactory.createConnection(VIRTUOSO_URL, USERNAME, PASSWORD);
             QueryExecution qe = QueryExecutionFactory.create(createQuery, dataset)) {
            qe.execUpdate();
        }
    }

    /**
     * 清空指定图的所有数据
     * @param graphUri 目标图的URI
     */
    public void clearGraph(String graphUri) {
        String clearQuery = String.format("DELETE WHERE { GRAPH <%s> { ?s ?p ?o } }", graphUri);
        try (Dataset dataset = DatasetFactory.createConnection(VIRTUOSO_URL, USERNAME, PASSWORD);
             QueryExecution qe = QueryExecutionFactory.create(clearQuery, dataset)) {
            qe.execUpdate();
        }
    }

    /**
     * 上传TTL文件到指定图
     * @param graphUri 目标图的URI
     * @param ttlFilePath TTL文件路径,例如"classpath:data/sample.ttl"
     */
    public void uploadTtlToGraph(String graphUri, String ttlFilePath) throws IOException {
        // 读取classpath下的TTL文件
        Resource resource = resourceLoader.getResource(ttlFilePath);
        try (InputStream inputStream = resource.getInputStream();
             Dataset dataset = DatasetFactory.createConnection(VIRTUOSO_URL, USERNAME, PASSWORD)) {
            Model model = ModelFactory.createDefaultModel();
            RDFDataMgr.read(model, inputStream, "TTL");
            
            // 将RDF模型写入指定命名图
            dataset.addNamedModel(graphUri, model);
            dataset.commit();
        }
    }
}

3. 调用示例(启动类或测试类)

在Spring Boot启动时执行测试逻辑:

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class VirtuosoDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(VirtuosoDemoApplication.class, args);
    }

    @Bean
    CommandLineRunner run(VirtuosoGraphService graphService) {
        return args -> {
            String targetGraphUri = "http://example.com/my-new-graph";
            String ttlResourcePath = "classpath:data/sample.ttl";

            // 1. 显式创建图(可选,Virtuoso会自动创建不存在的图)
            graphService.createGraph(targetGraphUri);
            
            // 2. 清空图(如需覆盖旧数据)
            graphService.clearGraph(targetGraphUri);
            
            // 3. 上传TTL文件到目标图
            graphService.uploadTtlToGraph(targetGraphUri, ttlResourcePath);
            
            System.out.println("图创建与数据上传完成");
        };
    }
}

关键注意事项

  • Virtuoso自动建图特性:即使跳过createGraph方法,直接调用uploadTtlToGraph,Virtuoso会在插入数据时自动创建目标图,显式调用仅用于提前确保图存在。
  • 图URI规范:必须使用合法的URI格式,SPARQL语句中需用尖括号包裹。
  • TTL文件存放:将TTL文件放在src/main/resources/data/目录下,即可通过classpath:data/sample.ttl路径访问。
  • 连接参数修改:根据你的Virtuoso实例配置调整VIRTUOSO_URL、USERNAME和PASSWORD,默认端口为1111,默认账号密码为dba/dba。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 20:31:23