按不属于 GroupBy 语句一部分的属性排序
我有一张包含一个起始节点和两个目标向量的图。有两条路通向第一个目标,另一条路通向第二个目标。 我寻找到达目标顶点的所有路径并收集边权重(sack(sum))。我通过 group().by() 添加通往同一目标的所有路径的总和。
到目前为止的查询:
g.withSack(1.0f).V('v0')
.repeat(
outE().sack(mult).by('weight')
.inV()
).until(hasLabel('goal'))
.group().by().by(sack())
.unfold()
.project('sack', 'map')
.by(select(values))
.by(select(keys).valueMap(true))
.order().by('sack', desc)
结果如下所示:
{'sack': 0.27999999999999997, 'map': {<T.id: 1>: 'v7', <T.label: 4>: 'goal', 'date': ['02-02-2022']}}
{'sack': 0.125, 'map': {<T.id: 1>: 'v3', <T.label: 4>: 'goal', 'date': ['02-02-2022']}}
现在我还想按日期排序...但是,使用 by('date', desc) 会导致错误:
“java.util.LinkedHashMap 无法转换为 java.lang.Comparable”
我可以以某种方式访问地图内的日期值吗?或者以其他方式按日期排序?
(仅供参考: by(sack(), desc) 的效果与 by('sack', desc) 一样好)
数据:(
g.addV('start').property(id, 'v0')
.addV('road').property(id, 'v1')
.addV('road').property(id, 'v2')
.addV('goal').property(id, 'v3').property('date', '02-02-2022')
.addV('road').property(id, 'v4').
.addV('road').property(id, 'v5').
.addV('road').property(id, 'v6').
.addV('goal').property(id, 'v7').property('date', '22-02-2022')
.addE('link').property('weight', 0.4).from(V('v0')).to(V('v1'))
.addE('link').property('weight', 0.4).from(V('v1')).to(V('v2'))
.addE('link').property('weight', 0.4).from(V('v2')).to(V('v3'))
.addE('link').property('weight', 0.5).from(V('v0')).to(V('v5'))
.addE('link').property('weight', 0.5).from(V('v5')).to(V('v4'))
.addE('link').property('weight', 0.5).from(V('v4')).to(V('v3'))
.addE('link').property('weight', 0.7).from(V('v0')).to(V('v6'))
.addE('link').property('weight', 0.4).from(V('v6')).to(V('v7'))
我的代码在 Neptune 上运行,带有 'gremlin' : {'版本': 'tinkerpop-3.4.11'})
I have a graph with one start-node and two goal-vetices. Two paths lead to the first goal, another path to the second.
I look for all paths to the goals vertices and collect the edge weights (sack(sum)). I add the sum of all paths leading to the same goal via group().by().
query so far:
g.withSack(1.0f).V('v0')
.repeat(
outE().sack(mult).by('weight')
.inV()
).until(hasLabel('goal'))
.group().by().by(sack())
.unfold()
.project('sack', 'map')
.by(select(values))
.by(select(keys).valueMap(true))
.order().by('sack', desc)
the result looks like this:
{'sack': 0.27999999999999997, 'map': {<T.id: 1>: 'v7', <T.label: 4>: 'goal', 'date': ['02-02-2022']}}
{'sack': 0.125, 'map': {<T.id: 1>: 'v3', <T.label: 4>: 'goal', 'date': ['02-02-2022']}}
now I want to also sort by date... However, using by('date', desc) results in an error:
"java.util.LinkedHashMap cannot be cast to java.lang.Comparable"
can I somehow access the date-value inside the map? or sort by date in some other way?
(fyi: by(sack(), desc) works just as well as by('sack', desc) )
data:
g.addV('start').property(id, 'v0')
.addV('road').property(id, 'v1')
.addV('road').property(id, 'v2')
.addV('goal').property(id, 'v3').property('date', '02-02-2022')
.addV('road').property(id, 'v4').
.addV('road').property(id, 'v5').
.addV('road').property(id, 'v6').
.addV('goal').property(id, 'v7').property('date', '22-02-2022')
.addE('link').property('weight', 0.4).from(V('v0')).to(V('v1'))
.addE('link').property('weight', 0.4).from(V('v1')).to(V('v2'))
.addE('link').property('weight', 0.4).from(V('v2')).to(V('v3'))
.addE('link').property('weight', 0.5).from(V('v0')).to(V('v5'))
.addE('link').property('weight', 0.5).from(V('v5')).to(V('v4'))
.addE('link').property('weight', 0.5).from(V('v4')).to(V('v3'))
.addE('link').property('weight', 0.7).from(V('v0')).to(V('v6'))
.addE('link').property('weight', 0.4).from(V('v6')).to(V('v7'))
(my code runs on Neptune with 'gremlin': {'version': 'tinkerpop-3.4.11'})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需从
map
地图中选择date
键即可。但请注意,为了使日期正确排序(如果将它们编码为字符串),您应该使用接近 ISO 8601 形式的格式,例如
YYYY-MM-DD
或更具体地说,2022 -02-22
。存储实际日期或纪元偏移整数可能会更有效。根据评论线程 2022-02-23 更新
好的,我想我明白您所看到的内容 - 我正在使用 3.5.2 级别的 Gremlin 控制台进行测试,看起来您正在使用 Amazon海王星。海王星目前处于 3.4.11 水平(但应该很快就会上升到 3.5.2 水平)。以下是在 TinkerPop 3.5.2 级别运行的修改后的查询。
You just need to select the
date
key from themap
map.Note however, for the dates to sort properly (if you encode them as strings), you should use something close to ISO 8601 form, such as
YYYY-MM-DD
or more concretely,2022-02-22
. It might be more efficient to store actual dates, or epoch offset integers.UPDATED based on comment thread 2022-02-23
OK, I think I figured out what you are seeing - I was testing using the Gremlin Console at the 3.5.2 level and it looks like you are using Amazon Neptune. Neptune is currently at the 3.4.11 level (but should be moving up to the 3.5.2 level fairly soon). Here is the modified query running at the TinkerPop 3.5.2 level.