sesame芝麻开门operrdf研究2

再来看个完整的读写例子:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.openrdf.model.Model;
import org.openrdf.model.URI;
import org.openrdf.model.util.ModelUtil;
import org.openrdf.model.vocabulary.RDFS;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.RDFParser;
import org.openrdf.rio.RDFWriter;
import org.openrdf.rio.Rio;
import org.openrdf.rio.UnsupportedRDFormatException;
import org.openrdf.rio.helpers.StatementCollector;
import org.openrdf.sail.memory.model.MemValueFactory;

public class Test {
static String NS = "http://www.steeven.org/rdf#";

public static void main(String[] args) throws Exception {
for (RDFFormat format : RDFFormat.values()) {
String file = "test." + format.getDefaultFileExtension();
testWrite(format, file);
testRead(format, file);
}
}

private static void testRead(RDFFormat format, String file)
throws Exception {
RDFParser parser = Rio.createParser(format, MemValueFactory
.getInstance());
StatementCollector sc = new StatementCollector();
parser.setRDFHandler(sc);
parser.parse(new FileInputStream(file), NS);
Model model = sc.getModel();
System.out.println(model.contains(createNS("steeven"), RDFS.LABEL, null));
System.out.println(model.filter(null, RDFS.LABEL, null));
System.out.println(model.filter(null, createNS("desc"), null));
System.out.println(model.filter(null, null, MemValueFactory
.getInstance().createLiteral("steeven li")));
System.out.println(ModelUtil.getUniqueObjectLiteral(model,
createNS("steeven"), RDFS.LABEL));
System.out.println(model.filter(createNS("steeven"), null, null).size());
}

private static URI createNS(String name) {
return MemValueFactory.getInstance().createURI(NS, name);
}

private static void testWrite(RDFFormat format, String file)
throws RDFHandlerException, UnsupportedRDFormatException,
FileNotFoundException {
// System.out.println("=======format: " + format + "========");
RDFWriter w = Rio.createWriter(format,
new FileOutputStream(file, false));
w.setBaseURI(NS);
w.handleNamespace("myns", NS);
w.handleNamespace("w3", RDFS.NAMESPACE);
w.startRDF();
w.handleComment("my first rdf file");

URI res = createNS("steeven");
w.handleStatement(MemValueFactory.getInstance().createStatement(res,
RDFS.LABEL,
MemValueFactory.getInstance().createLiteral("steeven li")));
w.handleStatement(MemValueFactory.getInstance().createStatement(res,
createNS("desc"),
MemValueFactory.getInstance().createLiteral("good blogger")));
w.endRDF();
System.out.println("\n");
}
}

输出如下:

true
[(http://www.steeven.org/rdf#steeven, http://www.w3.org/2000/01/rdf-schema#label, "steeven li")]
[(http://www.steeven.org/rdf#steeven, http://www.steeven.org/rdf#desc, "good blogger")]
[(http://www.steeven.org/rdf#steeven, http://www.w3.org/2000/01/rdf-schema#label, "steeven li")]
"steeven li"
2


RDFParser/RdfWriter主要针对流来操作. 而后面要介绍的sail则基于库的概念,可以查询.类似数据库的概念.
数据库的概念过于平面化.RDf的结构复杂些,能根据主谓宾中的任意元素组合查询/遍历. 其内置的索引机制负责加速查询. 开发过程中可以省去复杂的数据建模.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值