我正在构建一个小型Node应用程序,该应用程序查询本地n3三重文件,并使用rdfstore js。文档使用的示例一切正常,但这是一个远程三重存储。对于要将哪些参数传递给本地文件的rdfstore.create(),文档中存在混淆。也许是这样的?
rdfstore.create(function(store) {
store.execute('LOAD /Users/Ben/Desktop/MET/triple_SPARQL/triples.n3 text/n3 ', function() {
});
})
有人使用过rdfstore js并在本地文件中加载过吗?
谢谢!
从源代码来看,rdfstore-js似乎不支持加载SPARQL更新中引用的本地文件(例如:LOAD <file:///myfile.ttl>
)。但是,您可以自己读取文件并直接传入数据:
var rdfstore = require('rdfstore')
, fs = require('fs');
rdfstore.create(function(store){
var rdf = fs.readFileSync('/var/foo/bar.ttl').toString();
store.load('text/turtle', rdf, function(s,d){
console.log(s,d);
store.execute("SELECT * WHERE { ?s ?p ?o } LIMIT 10", function(success, results){
console.log(success, results);
});
});
});
这是我的代码示例,它允许您使用文件的路径在Node.js中加载文件(这里的选项.路径可以是/uploads/123.owl)
rdfstore.create(function(err, store) {
if (err)
console.log("There was an error creating the store", err);
else
{
//replace / with
var syncPath = __dirname + options.path; //local but not enough
var re = new RegExp('/', 'g');
syncPath = syncPath.replace(re, '\'); //update path
//set full path from filesystem of the ontology
var ontology = fs.readFileSync(__dirname + options.path).toString();
//LOCAL
store.load("application/ld+json" , ontology, "graph", function(err, results) {
if (err)
console.log("There was an error loading the store", err);
else
{
store.graph("graph", function(err, graph) {
if (err)
{
console.log("There was an error creating the graph", err);
}
else
{
var triples = graph.toArray();
console.log("Constructing triples sync... ");
console.log("There are ", triples.length, "triples");
if (triples.length !== 0)
{
cb(triples); }
}
}); //graph
}
});
store.close(); //done
}//err