再来看个完整的读写例子:
输出如下:
RDFParser/RdfWriter主要针对流来操作. 而后面要介绍的sail则基于库的概念,可以查询.类似数据库的概念.
数据库的概念过于平面化.RDf的结构复杂些,能根据主谓宾中的任意元素组合查询/遍历. 其内置的索引机制负责加速查询. 开发过程中可以省去复杂的数据建模.
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的结构复杂些,能根据主谓宾中的任意元素组合查询/遍历. 其内置的索引机制负责加速查询. 开发过程中可以省去复杂的数据建模.