获取NoFastSuchElementException,当我在java中使用gremlin查询中的valueMap()时

时间:2021-09-22 15:36:27
GraphTraversal<Vertex, Map<String, Object>> tsList = traversalSource.V().has("request","id_key",114).valueMap();        
while(tsList.hasNext())
{
System.out.println(tsList.next().get("status")); //prints result
System.out.println(tsList.next().get("tree_id_key")); //prints result
System.out.println(tsList.next().get("username")); //here throws Exception for any vertex.
System.out.println(tsList.next().get("tree_status"));
}

when I run the above query, I am getting values for any first and second propertyKey. but after getting answers for two Keys It throws NoFastSuchElementException for any propertyKey...Kindly help me..how can I resolve this?

当我运行上面的查询时,我得到任何第一个和第二个propertyKey的值。但在获得两个Keys的答案后,它会为任何propertyKey抛出NoFastSuchElementException ...请帮助我..我可以解决这个问题吗?

but using below query I am able to get the answer:

但使用以下查询我能得到答案:

GraphTraversal<Vertex, Map<String, Object>> tsList = traversalSource.V().has("request","id_key",114).values("status","tree_id_key","username","tree_status");       
while(tsList.hasNext())
{
System.out.println(tsList.next());//prints "status" value
System.out.println(tsList.next());//prints "tree_id_key" value
System.out.println(tsList.next());//prints "username" value
System.out.println(tsList.next());//prints "tree_status" value
}

updated:

GraphTraversal<Vertex, Map<String, Object>> ts = traversalSource.V().has("request","id_key",113).valueMap();        
while(ts.hasNext())
{
Map<String, Object> tsList=ts.next();
SuiteIdKey=(long)((ArrayList)tsList.get("suiteKey")).get(0);
seqe=(int)((ArrayList)tsList.get("sequence")).get(0);
}

In the above query, ts will return many rows and for each row I will get suiteIdkey,seqe..I am able to get the answer. But tsList is Map<>, it is not directly returning answer when I use like this =(long)tsList.get("suiteKey"), without ArrayList. Is it the correct way? Kindly help me.

在上面的查询中,ts将返回许多行,对于每一行,我将获得suiteIdkey,seqe ..我能够得到答案。但tsList是Map <>,当我使用像this =(long)tsList.get(“suiteKey”),没有ArrayList时,它不会直接返回答案。这是正确的方法吗?请帮助我。

1 个解决方案

#1


2  

You are asking for two different things in those two traversals. The first one with valueMap() asks that the vertex be converted to a Map, so you don't want to keep calling next() in that loop. Just do:

你在这两次遍历中要求两个不同的东西。第一个使用valueMap()要求将顶点转换为Map,因此您不希望在该循环中继续调用next()。做就是了:

Map<String, List<Object>> m = traversalSource.V().has("request", "id_key", 114).
                                                  valueMap().next();        
System.out.println(m.get("status").get(0)); 
System.out.println(m.get("tree_id_key").get(0)); 
System.out.println(m.get("username").get(0)); 
System.out.println(m.get("tree_status").get(0));

Keep in mind above that i'm assuming your traversal always returns one vertex...account for that otherwise if that is not true.

请记住,我假设你的遍历总是返回一个顶点...如果不是这样,那就解释了。

In your second traversal where you do values() you are asking Gremlin to convert grab the values of those specific properties on that vertex and stream them out and thus you need to iterate with multiple calls to next.

在你执行values()的第二次遍历中,你要求Gremlin转换抓取那个顶点上那些特定属性的值并将它们流出来,因此你需要迭代多次调用next。

Note that typically returning the entire vertex object isn't a recommended practice. It is somewhat akin to SELECT * FROM table in SQL. It is better to get only the fields that you want to get which then gets rid of the ugly multi-property issue of Map<String, List<Object>>:

请注意,通常不会返回整个顶点对象。它有点类似于SQL中的SELECT * FROM表。最好只获取你想要获取的字段,然后摆脱Map >的丑陋多属性问题: ,list>

Map<String, Object> m = traversalSource.V().has("request", "id_key", 114).
                                            project('s`,'tik', 'u', 'ts').
                                              by('status').
                                              by('tree_id_key').
                                              by('username')
                                              by('tree_status').
                                            next();        
System.out.println(m.get("s")); 
System.out.println(m.get("tik")); 
System.out.println(m.get("u")); 
System.out.println(m.get("ts"));

#1


2  

You are asking for two different things in those two traversals. The first one with valueMap() asks that the vertex be converted to a Map, so you don't want to keep calling next() in that loop. Just do:

你在这两次遍历中要求两个不同的东西。第一个使用valueMap()要求将顶点转换为Map,因此您不希望在该循环中继续调用next()。做就是了:

Map<String, List<Object>> m = traversalSource.V().has("request", "id_key", 114).
                                                  valueMap().next();        
System.out.println(m.get("status").get(0)); 
System.out.println(m.get("tree_id_key").get(0)); 
System.out.println(m.get("username").get(0)); 
System.out.println(m.get("tree_status").get(0));

Keep in mind above that i'm assuming your traversal always returns one vertex...account for that otherwise if that is not true.

请记住,我假设你的遍历总是返回一个顶点...如果不是这样,那就解释了。

In your second traversal where you do values() you are asking Gremlin to convert grab the values of those specific properties on that vertex and stream them out and thus you need to iterate with multiple calls to next.

在你执行values()的第二次遍历中,你要求Gremlin转换抓取那个顶点上那些特定属性的值并将它们流出来,因此你需要迭代多次调用next。

Note that typically returning the entire vertex object isn't a recommended practice. It is somewhat akin to SELECT * FROM table in SQL. It is better to get only the fields that you want to get which then gets rid of the ugly multi-property issue of Map<String, List<Object>>:

请注意,通常不会返回整个顶点对象。它有点类似于SQL中的SELECT * FROM表。最好只获取你想要获取的字段,然后摆脱Map >的丑陋多属性问题: ,list>

Map<String, Object> m = traversalSource.V().has("request", "id_key", 114).
                                            project('s`,'tik', 'u', 'ts').
                                              by('status').
                                              by('tree_id_key').
                                              by('username')
                                              by('tree_status').
                                            next();        
System.out.println(m.get("s")); 
System.out.println(m.get("tik")); 
System.out.println(m.get("u")); 
System.out.println(m.get("ts"));