首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >创建/加载本地文件

创建/加载本地文件
EN

Stack Overflow用户
提问于 2013-08-01 19:23:56
回答 2查看 1.8K关注 0票数 0

我正在构建一个小型Node应用程序,它可以查询本地n3三重文件,并使用rdfstore-js。在文档使用的示例中,一切都正常,但这是一个远程三重存储。文档混淆了哪些参数要传递给本地文件的rdfstore.create()。也许是这样的?

代码语言:javascript
复制
    rdfstore.create(function(store) {
    store.execute('LOAD /Users/Ben/Desktop/MET/triple_SPARQL/triples.n3 text/n3 ',       function() {

    });
})

有人使用rdfstore-js并加载了本地文件吗?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-01 21:33:20

从查看源代码来看,rdfstore-js似乎不支持加载SPARQL更新(例如:LOAD <file:///myfile.ttl> )中引用的本地文件。但是,您可以自己读取文件并直接传递数据:

代码语言:javascript
复制
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);
    });
  });
});
票数 0
EN

Stack Overflow用户

发布于 2015-08-13 13:04:26

下面是我的代码示例,它允许您使用文件的路径加载Node.js中的文件(在这里,options.path可以是/上载/123.owl)

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18002780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档