gremlin - gremlin中使用next()步骤时出现错误如何解决?

标签 gremlin janusgraph

之前我使用过janusgraph-0.2.0-hadoop2服务器和[email protected]用于查询的库。我的查询工作正常。但是,现在我已经更新到最新版本(janusgraph:最新,Cassandra:3,elasticsearch:6.6.0)和janusgraph-0.5.0-hadoop2服务器。 对于 janusgraph-0.5.0-hadoop2 服务器和 [email protected]库,以下查询

g.addV('Location') .property('locationId', 'afdvbd-w6POkbQTq') .property('active', false) .as('place') .V().hasLabel('user').has('userId', 'hhdfjgygkgkdgfyukdgd') .as('person') .addE('createdBy').from('place') .property('on', 1589278312249).next()

结果出现错误

Exception while adding place to graph Error: java.util.NoSuchElementException (Error 597) at GremlinClient.handleProtocolMessage (/srv/node_modules/gremlin/lib/GremlinClient.js:182:37) at WebSocketGremlinConnection.connection.on.message (/srv/node_modules/gremlin/lib/GremlinClient.js:108:46) at emitOne (events.js:116:13) at WebSocketGremlinConnection.emit (events.js:211:7) at WebSocketGremlinConnection.handleMessage (/srv/node_modules/gremlin/lib/WebSocketGremlinConnection.js:45:10) at WebSocketGremlinConnection.ws.onmessage.message (/srv/node_modules/gremlin/lib/WebSocketGremlinConnection.js:30:41) at WebSocket.onMessage (/srv/node_modules/ws/lib/EventTarget.js:103:16) at emitTwo (events.js:126:13) at WebSocket.emit (events.js:214:7) at Receiver._receiver.onmessage (/srv/node_modules/ws/lib/WebSocket.js:146:54)

当我将gremlin库更新到3.4.6来运行相同的查询时,结果如下

登录Google云平台:

RangeError: Invalid status code: undefined at ServerResponse.writeHead (_http_server.js:200:11) at ServerResponse._implicitHeader (_http_server.js:191:8) at write_ (_http_outgoing.js:632:9) at ServerResponse.end (_http_outgoing.js:751:5) at ServerResponse.send (/srv/node_modules/express/lib/response.js:221:10) at ServerResponse.json (/srv/node_modules/express/lib/response.js:267:15) at ServerResponse.send (/srv/node_modules/express/lib/response.js:158:21) at place.postPlace.then.catch.err (/srv/routes/place.js:41:35) at <anonymous> at process._tickDomainCallback (internal/process/next_tick.js:229:7)

登录 gremlin 控制台:

java.util.NoSuchElementException
Type ':help' or ':h' for help.
Display stack trace? [yN]y
java.util.NoSuchElementException
        at org.apache.tinkerpop.gremlin.process.traversal.util.DefaultTraversal.next(DefaultTraversal.java:213)
        at java_util_Iterator$next.call(Unknown Source)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:119)
        at Script17.run(Script17.groovy:1)
        at org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.eval(GremlinGroovyScriptEngine.java:674)
        at org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.eval(GremlinGroovyScriptEngine.java:376)
        at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233)
        at org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutor.lambda$eval$0(GremlinExecutor.java:267)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)

我的 javascript 片段是

/**
 * @param {Object} options
 * @param {} options.userId 
 * @param options.client // this is the gremlin client to run queries onto graph database.
 * @throws {Error}
 * @return {Promise}
 */
module.exports.addLocation = (options) => {
  const location = options.location;
  const admin = options.admin;
  const createdOn = new Date().valueOf(); // TODO: timestamp entered into Firestore. Same value should be given here

  console.log('addLocation invoked', location);
  console.log('Options obtained', options);
  return new Promise((resolve, reject) => {
    if (options.userId !== options.reqUserId) {
      console.log(`UserId: ${options.userId}, reqUserId: ${options.reqUserId}`, options);
      return reject({
        status: 401,
        body: 'Unauthorized. You can\'t impersonate other users!'
      });
    }
    console.log('validating the location');
    const errors = validate(location, 'Model');
    if (errors) {
      console.log('Error while validating the locations:', errors);
      return reject({
        status: 405,
        body: errors
      });
    }
    const locationId = `${location.name.replace(/\s+/g, '-').replace(/[^a-zA-Z0-9-_]/g, '').toLowerCase()  }-${  shortid.generate()}`; //TODO: id obtained from firestore
    console.log(`Created dynamic location id: ${  locationId}`);

    location['locationId'] = locationId;
    location['createdDate'] = new Date();
    location['lastUpdatedDate'] = new Date();
    if (location.publishedDate) story['publishedDate'] = new Date(location.publishedDate);
    if (location.unpublishedDate) story['unpublishedDate'] = new Date(location.unpublishedDate);
    console.log(`updated location: ${ JSON.stringify(location)}`);
    //TODO: get the ID of the story and use it to make entry into Graph
    const name = location.name;
    const active = false;
    const inputParams = {
      userId:options.userId,
      locationId,
      name,
      active,
      createdOn
    };
    console.log('adding location to graph');
    const query = `g.addV('Location') .property('locationId', locationId) .property('active', false) .as('place') .V().hasLabel('user').has('userId',userId ) .as('person') .addE('createdBy').from('place') .property('on', 1589278312249).next()`;
    console.log(`query: ${query}  inputParams:`, inputParams);
    options.client.executeGraph(query, inputParams).then(res => {
      if (res.length > 0) {
        console.log('Successfully added the location to graph');
        console.log('adding location to firestore');
        
        
        const createdlocation = location; // TODO: replace with actual object.
        admin.firestore().doc(`cms/ugc/locations/${  locationId}`).set(location).then(() => {
          console.log('succesfully added location to firestore ');

          return resolve({
            status: 200,
            data: createdlocation
          });
        }).catch((err) => {
          console.log('Error while adding location to firestore:', err);
          return reject({status:500, error: err});
        });
      } 
    }).catch(err => {
      console.log('Exception while adding location to graph', err);
      return reject({
        status: 500,
        error: err
      });
    });
  });
};

但是,当在 gremlin 控制台中使用静态数据运行相同的查询并删除 next() 步骤时,它工作得非常好。

不知道确切的问题,但我认为最新版本中的 next() 步骤可能无法正常工作。这是一个疯狂的猜测,但我不知道如何解决它。 任何帮助将不胜感激。预先感谢!

最佳答案

我真的不知道出了什么问题。其本身的 NoSuchElementException 通常意味着您在空的 Traversal 上调用了 next()。那么问题就变成了,为什么我的遍历没有返回数据?在这种情况下,我会对 V().hasLabel('user').has('userId','x') 表示怀疑,因为它是查询中唯一不存在的部分添加剂。它需要为 addE() 生成一个顶点才能返回任何内容。

在空图表上运行上面的查询,您可以看到会发生什么:

gremlin> g.addV('Location').property('locationId', 123).property('active', false) .as('place').
......1>   V().hasLabel('user').has('userId','x').as('person').
......2>   addE('createdBy').from('place').property('on', 1589278312249).next()
java.util.NoSuchElementException
Type ':help' or ':h' for help.
Display stack trace? [yN]

因此很容易重新创建。当然,问题是你说你的遍历在 Gremlin 控制台中工作正常。您是否有可能在两次测试中连接到同一个图表?您是否使用了完全相同的参数(即相同的“userId”)?我将从失败的 JavaScript 中验证您是否可以正确查询您尝试添加边的顶点。

关于gremlin - gremlin中使用next()步骤时出现错误如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61750578/

相关文章:

orientdb - Gremlin- 查找并连接子图

python - 如何将gremlin服务器与orientDB连接

cassandra - JanusGraph gremlin 如何知道它应该使用 Cassandra 而不是 Hadoop?

janusgraph 不允许我以 AWSKeyspace 作为后端设置 TTL

gremlin - 如何在 gremlin-javascript 中不区分大小写地查询

c# - 如何从 C#.Net 代码创建 Gremlin 图?

tinkerpop - 如何远程连接到JanusGraph服务器?

python - 有没有办法将 gremlinpython 图转换为 networkx 图

graph - 我应该使用 JanusGraph 作为主数据库来存储新项目的所有数据吗?

java - 如何在 Gremlin 中查找图形模式?