Graphdb.js node.js 一个 server.repository 通过 Rest API 连接多个查询
Graphdb.js node.js one server.repository connection multiple queries through Rest APIs
我正在使用 nodejs 和 graphdbjs。我的问题是我是否可以激活一个存储库并设置两个查询(在本例中为有效负载),它们将作为 REST API 按需 运行。例如
const server = new ServerClient(serverConfig);
const readTimeout = 30000;
const writeTimeout = 30000;
const repositoryClientConfig = new RepositoryClientConfig(['http://localhost:7200/repositories/V2'], {}, '', readTimeout, writeTimeout);
server.getRepository('V2',repositoryClientConfig).then(repository => {
repository.registerParser(new SparqlJsonResultParser());
const payload = new GetQueryPayload()
.setQuery('select * where {?s ?p ?o}')
.setQueryType(QueryType.SELECT)
.setResponseType(RDFMimeType.SPARQL_RESULTS_JSON)
.setLimit(100);
repository.query(payload).then((stream) => {
stream.on('data', (bindings) => {
var test = Object.values(bindings);
var element = {id: test[0].id.replace(/['"]+/g, ''), name: test[1].id.replace(/['"]+/g, '')}
});
stream.on('end', () => {
restapp.get('/getallIDs', function(req,res) {
res.send(element);
});
restapp.listen(3000, function () {
console.log('App listening on port 3000!');
});
});
});
const payload2 = new GetQueryPayload()
.setQuery('select * where {?s ?p ?o}')
.setQueryType(QueryType.SELECT)
.setResponseType(RDFMimeType.SPARQL_RESULTS_JSON)
.setLimit(100);
repository2.query(payload2).then((stream2) => {
stream2.on('data', (bindings2) => {
var test2 = Object.values(bindings2);
var i2=0;
test2.forEach(function(key,value){
if (i2%2==1){
return true;}
var element2 = {id: test2[0].id.replace(/['"]+/g, ''), name: test2[1].id.replace(/['"]+/g, '')}
i++;
head2.table.push(element2);
});
});
stream2.on('end', () => {
restapp2.get('/getidby', function(req,res) {
res.send(head2);
});
restapp2.listen(3000, function () {
console.log('App listening on port 3000!');
});
});
我只想有一个会话和多个有效负载,如果用户点击正确的 url,那么它 returns 就像 json。截至目前,我收到无法解释的错误:
{"level":"error","time":1572863035018,"name":"RDFRepositoryClient","repositoryUrl":"http://localhost:7200/repositories/V2","msg":"Cannot retry execution","v":1}
Error: Request failed with status code 400
at createError (/home/estathop/eclipse-workspace/test/node_modules/axios/lib/core/createError.js:16:15)
at settle (/home/estathop/eclipse-workspace/test/node_modules/axios/lib/core/settle.js:17:12)
at RedirectableRequest.handleResponse (/home/estathop/eclipse-workspace/test/node_modules/axios/lib/adapters/http.js:211:9)
at RedirectableRequest.emit (events.js:210:5)
at RedirectableRequest._processResponse (/home/estathop/eclipse-workspace/test/node_modules/follow-redirects/index.js:269:10)
at ClientRequest.RedirectableRequest._onNativeResponse (/home/estathop/eclipse-workspace/test/node_modules/follow-redirects/index.js:50:10)
at Object.onceWrapper (events.js:300:26)
at ClientRequest.emit (events.js:210:5)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:583:27)
at HTTPParser.parserOnHeadersComplete (_http_common.js:115:17)
对实现我想要做的事情有什么帮助吗?我的下一步是从 restful 服务获取 id 并相应地形成有效负载
谢谢,抱歉,如果我的问题是假的,但我是这个领域的新手
据我所知,您要实现的目标没有概念上的问题。
现在根据错误,我认为这可能与您对其余部分执行的 GET 请求有关 api restapp.get('/getallIDs', function(req,res) {...}
您能否确认 repository.query(payload) 调用 returns 是正确的响应?我猜你可以简单地在 stream.on('data') 之前放置一个记录器,看看它是否被打印出来,这将确认 graphdbjs 请求可能是正确的。
我可以给你一个稍微改进但仍然相似的例子,它似乎正确地流式传输查询结果:
const {ServerClient, ServerClientConfig} = require('graphdbjs').server;
const {RepositoryClientConfig} = require('graphdbjs').repository;
const {RDFMimeType} = require('graphdbjs').http;
const {SparqlJsonResultParser} = require('graphdbjs').parser;
const {GetQueryPayload, QueryType} = require('graphdbjs').query;
const serverConfig = new ServerClientConfig('http://localhost:7200/', 0, {
'Accept': RDFMimeType.SPARQL_RESULTS_JSON
});
const server = new ServerClient(serverConfig);
const readTimeout = 30000;
const writeTimeout = 30000;
const repositoryClientConfig = new RepositoryClientConfig(['http://localhost:7200/repositories/testrepo'], {}, '', readTimeout, writeTimeout);
server.getRepository('testrepo',repositoryClientConfig).then(function(repository) {
console.log('REPO:', repository);
repository.registerParser(new SparqlJsonResultParser());
const payload = new GetQueryPayload()
.setQuery('select * where {?s ?p ?o}')
.setQueryType(QueryType.SELECT)
.setResponseType(RDFMimeType.SPARQL_RESULTS_JSON)
.setLimit(100);
return repository.query(payload);
})
.then(function(stream) {
console.log('SELECT:');
stream.on('data', function(bindings) {
var test = Object.values(bindings);
console.log('DATA');
// var element = {id: test[0].id.replace(/['"]+/g, ''), name: test[1].id.replace(/['"]+/g, '')}
});
stream.on('end', function() {
console.log('END');
// restapp.get('/getallIDs', function(req,res) {
// res.send(element);
// });
// restapp.listen(3000, function () {
// console.log('App listening on port 3000!');
// });
});
})
.catch(function(error) {
console.log('ERROR', error);
});
您可以解决的另一件事是 res.send(element);
调用,正如我所看到的那样,它实际上会发送 undefined
,因为 element
的范围在 stream.on('data'
回调中。因此,您可以通过在 stream.on('data'
行之前定义 var element;
来使其可用。
最好的结构化方式是文件分明,一个是主节点app.js
var express = require('express');
var restapp = express();
var queryController = require('./controllers/queryController')
restapp.use(queryController)
restapp.listen(3001, function () {
console.log('Αpp listening on port 3001!');
});
并且暗示您导入了 queryController.js
,其中包括执行您需要的每个不同查询的代码到 graphdb 服务器
var express = require('express');
var router = express.Router()
const {ServerClient, ServerClientConfig} = require('graphdb').server;
const {RDFMimeType} = require('graphdb').http;
const {RepositoryClientConfig} = require('graphdb').repository;
const {SparqlJsonResultParser} = require('graphdb').parser;
const {GetQueryPayload} = require('graphdb').query;
const {QueryType}= require('graphdb').query;
const serverConfig = new ServerClientConfig('http://localhost:7200', 0, {
'Accept': RDFMimeType.SPARQL_RESULTS_JSON
});
router.get('/hi', (req, res) => {
res.send('hi')
})
router.get('/getallids', (req, res) => {
head = {};
table = [];
head.table = table;
const server = new ServerClient(serverConfig);
const readTimeout = 30000;
const writeTimeout = 30000;
const repositoryClientConfig = new RepositoryClientConfig(['http://localhost:7200/repositories/V2'], {}, '', readTimeout, writeTimeout);
server.getRepository('V2',repositoryClientConfig).then(repository => {
repository.registerParser(new SparqlJsonResultParser());
const payload = new GetQueryPayload()
.setQuery('select * where {?p ?o ?k}')
.setQueryType(QueryType.SELECT)
.setResponseType(RDFMimeType.SPARQL_RESULTS_JSON)
.setLimit(100);
repository.query(payload).then((stream) => {
stream.on('data', (bindings) => {
var test = Object.values(bindings);
var i=0;
test.forEach(function(key,value){
test2 = Object.values(key);
if (i%2==1){
return true;}
var element = {id: test[0].id.replace(/['"]+/g, ''), name: test[1].id.replace(/['"]+/g, '')}
i++;
head.table.push(element);
});
});
stream.on('end', () => {
res.send(head);
});
}).catch(err => console.log(err));
});
})
router.get('/getid/:id', (req, res) => {
const params = req.params;
head = {};
table = [];
head.table = table;
const server = new ServerClient(serverConfig);
const readTimeout = 30000;
const writeTimeout = 30000;
const repositoryClientConfig = new RepositoryClientConfig(['http://localhost:7200/repositories/V2'], {}, '', readTimeout, writeTimeout);
server.getRepository('V2',repositoryClientConfig).then(repository => {
repository.registerParser(new SparqlJsonResultParser());
const payload = new GetQueryPayload()
.setQuery('select'+Object.values(params)+'where {?p ?o ?k}')
.setQueryType(QueryType.SELECT)
.setResponseType(RDFMimeType.SPARQL_RESULTS_JSON)
.setLimit(100);
repository.query(payload).then((stream) => {
stream.on('data', (bindings) => {
var test = Object.values(bindings);
var i=0;
test.forEach(function(key,value){
test2 = Object.values(key);
if (i%2==1){
return true;}
var element = {key: test[0].id.replace(/['"]+/g, ''), value: test[1].id.replace(/['"]+/g, '')}
i++;
head.table.push(element);
});
});
stream.on('end', () => {
res.send(head);
});
}).catch(err2 => console.log(err2));
});
})
module.exports = router;
我正在使用 nodejs 和 graphdbjs。我的问题是我是否可以激活一个存储库并设置两个查询(在本例中为有效负载),它们将作为 REST API 按需 运行。例如
const server = new ServerClient(serverConfig);
const readTimeout = 30000;
const writeTimeout = 30000;
const repositoryClientConfig = new RepositoryClientConfig(['http://localhost:7200/repositories/V2'], {}, '', readTimeout, writeTimeout);
server.getRepository('V2',repositoryClientConfig).then(repository => {
repository.registerParser(new SparqlJsonResultParser());
const payload = new GetQueryPayload()
.setQuery('select * where {?s ?p ?o}')
.setQueryType(QueryType.SELECT)
.setResponseType(RDFMimeType.SPARQL_RESULTS_JSON)
.setLimit(100);
repository.query(payload).then((stream) => {
stream.on('data', (bindings) => {
var test = Object.values(bindings);
var element = {id: test[0].id.replace(/['"]+/g, ''), name: test[1].id.replace(/['"]+/g, '')}
});
stream.on('end', () => {
restapp.get('/getallIDs', function(req,res) {
res.send(element);
});
restapp.listen(3000, function () {
console.log('App listening on port 3000!');
});
});
});
const payload2 = new GetQueryPayload()
.setQuery('select * where {?s ?p ?o}')
.setQueryType(QueryType.SELECT)
.setResponseType(RDFMimeType.SPARQL_RESULTS_JSON)
.setLimit(100);
repository2.query(payload2).then((stream2) => {
stream2.on('data', (bindings2) => {
var test2 = Object.values(bindings2);
var i2=0;
test2.forEach(function(key,value){
if (i2%2==1){
return true;}
var element2 = {id: test2[0].id.replace(/['"]+/g, ''), name: test2[1].id.replace(/['"]+/g, '')}
i++;
head2.table.push(element2);
});
});
stream2.on('end', () => {
restapp2.get('/getidby', function(req,res) {
res.send(head2);
});
restapp2.listen(3000, function () {
console.log('App listening on port 3000!');
});
});
我只想有一个会话和多个有效负载,如果用户点击正确的 url,那么它 returns 就像 json。截至目前,我收到无法解释的错误:
{"level":"error","time":1572863035018,"name":"RDFRepositoryClient","repositoryUrl":"http://localhost:7200/repositories/V2","msg":"Cannot retry execution","v":1}
Error: Request failed with status code 400
at createError (/home/estathop/eclipse-workspace/test/node_modules/axios/lib/core/createError.js:16:15)
at settle (/home/estathop/eclipse-workspace/test/node_modules/axios/lib/core/settle.js:17:12)
at RedirectableRequest.handleResponse (/home/estathop/eclipse-workspace/test/node_modules/axios/lib/adapters/http.js:211:9)
at RedirectableRequest.emit (events.js:210:5)
at RedirectableRequest._processResponse (/home/estathop/eclipse-workspace/test/node_modules/follow-redirects/index.js:269:10)
at ClientRequest.RedirectableRequest._onNativeResponse (/home/estathop/eclipse-workspace/test/node_modules/follow-redirects/index.js:50:10)
at Object.onceWrapper (events.js:300:26)
at ClientRequest.emit (events.js:210:5)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:583:27)
at HTTPParser.parserOnHeadersComplete (_http_common.js:115:17)
对实现我想要做的事情有什么帮助吗?我的下一步是从 restful 服务获取 id 并相应地形成有效负载 谢谢,抱歉,如果我的问题是假的,但我是这个领域的新手
据我所知,您要实现的目标没有概念上的问题。
现在根据错误,我认为这可能与您对其余部分执行的 GET 请求有关 api restapp.get('/getallIDs', function(req,res) {...}
您能否确认 repository.query(payload) 调用 returns 是正确的响应?我猜你可以简单地在 stream.on('data') 之前放置一个记录器,看看它是否被打印出来,这将确认 graphdbjs 请求可能是正确的。
我可以给你一个稍微改进但仍然相似的例子,它似乎正确地流式传输查询结果:
const {ServerClient, ServerClientConfig} = require('graphdbjs').server;
const {RepositoryClientConfig} = require('graphdbjs').repository;
const {RDFMimeType} = require('graphdbjs').http;
const {SparqlJsonResultParser} = require('graphdbjs').parser;
const {GetQueryPayload, QueryType} = require('graphdbjs').query;
const serverConfig = new ServerClientConfig('http://localhost:7200/', 0, {
'Accept': RDFMimeType.SPARQL_RESULTS_JSON
});
const server = new ServerClient(serverConfig);
const readTimeout = 30000;
const writeTimeout = 30000;
const repositoryClientConfig = new RepositoryClientConfig(['http://localhost:7200/repositories/testrepo'], {}, '', readTimeout, writeTimeout);
server.getRepository('testrepo',repositoryClientConfig).then(function(repository) {
console.log('REPO:', repository);
repository.registerParser(new SparqlJsonResultParser());
const payload = new GetQueryPayload()
.setQuery('select * where {?s ?p ?o}')
.setQueryType(QueryType.SELECT)
.setResponseType(RDFMimeType.SPARQL_RESULTS_JSON)
.setLimit(100);
return repository.query(payload);
})
.then(function(stream) {
console.log('SELECT:');
stream.on('data', function(bindings) {
var test = Object.values(bindings);
console.log('DATA');
// var element = {id: test[0].id.replace(/['"]+/g, ''), name: test[1].id.replace(/['"]+/g, '')}
});
stream.on('end', function() {
console.log('END');
// restapp.get('/getallIDs', function(req,res) {
// res.send(element);
// });
// restapp.listen(3000, function () {
// console.log('App listening on port 3000!');
// });
});
})
.catch(function(error) {
console.log('ERROR', error);
});
您可以解决的另一件事是 res.send(element);
调用,正如我所看到的那样,它实际上会发送 undefined
,因为 element
的范围在 stream.on('data'
回调中。因此,您可以通过在 stream.on('data'
行之前定义 var element;
来使其可用。
最好的结构化方式是文件分明,一个是主节点app.js
var express = require('express');
var restapp = express();
var queryController = require('./controllers/queryController')
restapp.use(queryController)
restapp.listen(3001, function () {
console.log('Αpp listening on port 3001!');
});
并且暗示您导入了 queryController.js
,其中包括执行您需要的每个不同查询的代码到 graphdb 服务器
var express = require('express');
var router = express.Router()
const {ServerClient, ServerClientConfig} = require('graphdb').server;
const {RDFMimeType} = require('graphdb').http;
const {RepositoryClientConfig} = require('graphdb').repository;
const {SparqlJsonResultParser} = require('graphdb').parser;
const {GetQueryPayload} = require('graphdb').query;
const {QueryType}= require('graphdb').query;
const serverConfig = new ServerClientConfig('http://localhost:7200', 0, {
'Accept': RDFMimeType.SPARQL_RESULTS_JSON
});
router.get('/hi', (req, res) => {
res.send('hi')
})
router.get('/getallids', (req, res) => {
head = {};
table = [];
head.table = table;
const server = new ServerClient(serverConfig);
const readTimeout = 30000;
const writeTimeout = 30000;
const repositoryClientConfig = new RepositoryClientConfig(['http://localhost:7200/repositories/V2'], {}, '', readTimeout, writeTimeout);
server.getRepository('V2',repositoryClientConfig).then(repository => {
repository.registerParser(new SparqlJsonResultParser());
const payload = new GetQueryPayload()
.setQuery('select * where {?p ?o ?k}')
.setQueryType(QueryType.SELECT)
.setResponseType(RDFMimeType.SPARQL_RESULTS_JSON)
.setLimit(100);
repository.query(payload).then((stream) => {
stream.on('data', (bindings) => {
var test = Object.values(bindings);
var i=0;
test.forEach(function(key,value){
test2 = Object.values(key);
if (i%2==1){
return true;}
var element = {id: test[0].id.replace(/['"]+/g, ''), name: test[1].id.replace(/['"]+/g, '')}
i++;
head.table.push(element);
});
});
stream.on('end', () => {
res.send(head);
});
}).catch(err => console.log(err));
});
})
router.get('/getid/:id', (req, res) => {
const params = req.params;
head = {};
table = [];
head.table = table;
const server = new ServerClient(serverConfig);
const readTimeout = 30000;
const writeTimeout = 30000;
const repositoryClientConfig = new RepositoryClientConfig(['http://localhost:7200/repositories/V2'], {}, '', readTimeout, writeTimeout);
server.getRepository('V2',repositoryClientConfig).then(repository => {
repository.registerParser(new SparqlJsonResultParser());
const payload = new GetQueryPayload()
.setQuery('select'+Object.values(params)+'where {?p ?o ?k}')
.setQueryType(QueryType.SELECT)
.setResponseType(RDFMimeType.SPARQL_RESULTS_JSON)
.setLimit(100);
repository.query(payload).then((stream) => {
stream.on('data', (bindings) => {
var test = Object.values(bindings);
var i=0;
test.forEach(function(key,value){
test2 = Object.values(key);
if (i%2==1){
return true;}
var element = {key: test[0].id.replace(/['"]+/g, ''), value: test[1].id.replace(/['"]+/g, '')}
i++;
head.table.push(element);
});
});
stream.on('end', () => {
res.send(head);
});
}).catch(err2 => console.log(err2));
});
})
module.exports = router;