Apache Jena TDB 常用API

创建/连接TDB

这里是引用
The class TDBFactory contains the static factory methods for creating and connecting to a TDB-backed graph or an RDF dataset. Models and datasets should be closed after use.
->An application can specify the model or dataset by:
1.Giving a directory name
2.Giving an assembler file
->If a directory is empty, the TDB files for indexes and node table are created. If the directory contains files from a previous application run, TDB connects to the data already there. Closing the model or dataset is important. Any updates made are forced to disk if they have not been written already.

String directory = "MyDatabases/Dataset1" ;
Dataset dataset = TDBFactory.createDataset(directory) ;

如果目录为非空, 则会连接已存在的数据.

MultipartFile 导入TDB

RDFDataMgr.read(dataset, ontologyInputStream, Lang.TTL);

清除TDB数据

dataset.asDatasetGraph().clear();

这种用法会有问题"not in a transaction"

DatasetGraph datasetGraph = dataset.asDatasetGraph();
datasetGraph.beigin(ReadWrite.Write);
datasetGraph.clear();
datasetGraph.commit();
datasetGraph.end();

导出TTL文件

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
RDFDataMgr.write(byteArrayOutputStream, dataset.getDefaultModel(), Lang.TTL);

Reference

https://jena.apache.org/documentation/tdb/java_api.html

Guess you like

Origin blog.csdn.net/rosemary512/article/details/118577316