incubator-hugegraph icon indicating copy to clipboard operation
incubator-hugegraph copied to clipboard

[Question] valueMap()参数binding行为与预期不符

Open movee8 opened this issue 5 years ago • 7 comments

当前java client执行一下语句会报错:

List<String> ret = Lists.newArrayList("name", "age");

ResultSet rs = gremlin.gremlin("g.V().has('name', 'marko').valueMap(ret)").binding("ret", ret).execute();

报错信息为:

com.baidu.hugegraph.exception.ServerException: [Ljava.lang.String; cannot be cast to java.util.List

这与binding的使用预期有点不相符。这个地方的binding是有什么考虑特意设计成这样的吗?还是一个缺陷?

movee8 avatar Jun 03 '21 07:06 movee8

这是由于 groovy 和 java 的类型系统有些不同,List 和 Array 转换时需要额外做一些工作。

可以这么写

GremlinManager gremlin = hugeClient.gremlin();
List<String> ids = Lists.newArrayList("1", "2");
List<String> properties = Lists.newArrayList("演员");
ResultSet resultSet = gremlin.gremlin("def props = properties as String[]; g.V(ids).valueMap(props)")
                             .binding("ids", ids)
                             .binding("properties", properties)
                             .execute();

Linary avatar Jun 03 '21 07:06 Linary

上面的方式能正常work,但是跟g.V(), with()中的binding参数使用方式不一致,且相对复杂些了,不知道这个地方是否有优化的空间哈?

movee8 avatar Jun 03 '21 08:06 movee8

Due to the lack of activity, the current issue is marked as stale and will be closed after 20 days, any update will remove the stale label

github-actions[bot] avatar Jun 18 '21 21:06 github-actions[bot]

这个地方,我们的huge可以列入improvement么?我觉得这样对用户使用比较友好些

movee8 avatar Jul 05 '21 07:07 movee8

这个问题不是 huge 兼不兼容的问题,是 List ret = Lists.newArrayList() 生成的本身就是 java 里面的 List 类型,java 的 List 和 groovy 里面的 list 和 array 本来就不是能直接无缝替换的。你如果想用这种风格的语法写,在创建 list 的时候就应该创建出groovy 里面的 list 而非 java 的。

Linary avatar Jul 05 '21 10:07 Linary

我理解需要在两个数据类型之间转换一下,这个功能可以在huge内部做兼容处理吗?对用户屏蔽差异。

movee8 avatar Jul 06 '21 12:07 movee8

Due to the lack of activity, the current issue is marked as stale and will be closed after 20 days, any update will remove the stale label

github-actions[bot] avatar Jul 21 '21 21:07 github-actions[bot]