【问题标题】:How to resolve the error when next() step is used in gremlin?在 gremlin 中使用 next() 步骤时如何解决错误?
【发布时间】:2020-05-12 11:29:54
【问题描述】:

以前我使用 janusgraph-0.2.0-hadoop2 服务器和 gremlin@2.6.0 库进行查询。我的查询工作正常。但是,现在我已经更新到最新版本(janusgraph:最新,Cassandra:3,弹性搜索:6.6.0)和 janusgraph-0.5.0-hadoop2 服务器。 对于 janusgraph-0.5.0-hadoop2 服务器和 gremlin@2.6.0 库,如下查询

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运行时同样的查询,那么结果如下

登录谷歌云平台:

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 sn-p 是

/**
 * @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() 步骤可能无法正常工作。这是一个疯狂的猜测,但我不知道如何解决它。 任何帮助将不胜感激。提前致谢!

【问题讨论】:

  • 也许您应该分享更多显示此故障条件的简化执行的 javascript 代码。我还想知道您是否有任何表明失败的服务器端日志。
  • @stephenmallette,我已经更新了我的问题。所以,你能帮我检查一下吗!

标签: gremlin janusgraph


【解决方案1】:

我真的不确定有什么问题。单独使用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 中验证您可以正确查询您尝试添加边的顶点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多