如何使用gremlin_python按datetime过滤顶点 不清楚gt所属引用包
解决方案
你混淆了比较谓词的适用场景,TextP是专门用于文本匹配的谓词类,数值/日期类的比较需要使用通用比较谓词类P。
1. 依赖导入
需要从gremlin python的遍历模块导入P,同时注意datetime的正确构造:
from gremlin_python.process.traversal import P import datetime
2. 代码修正
原代码有两处错误:一是误用了TextP.gt,二是直接给datetime构造方法传字符串不符合语法,修正后的代码如下:
from gremlin_python.structure.graph import Graph from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection from gremlin_python.process.traversal import P import datetime graph = Graph() remote_connection = DriverRemoteConnection(gremlin, 'g') g = graph.traversal().withRemote(remote_connection) # 可直接用fromisoformat兼容ISO标准的日期字符串 serviceAcc=g.V().hasLabel('ServiceAccount').has('creationTime', P.gt(datetime.datetime.fromisoformat('2021-10-05'))).valueMap(True).toList() remote_connection.close()
额外注意点
- 如果存储的creationTime是Unix时间戳格式,需要先把目标日期转成对应精度的时间戳再传入比较
- 部分Gremlin实现(比如JanusGraph)如果对日期字段做了索引,使用P.gt可以命中索引,TextP则无法触发索引查询
内容的提问来源于stack exchange,提问作者Gowtham Ravichandran
相关产品推荐
相关产品推荐

