我已连接到 gremlin server
(版本 3.4.0
)来 self 的 java 应用程序,使用 gremlin-driver
(版本3.4.0
)。我使用以下代码从 Java 连接到服务器。
Cluster cluster = Cluster.build("localhost").port(8182).create();
Client client = cluster.connect();
GraphTraversalSource graphTraversalSource = AnonymousTraversalSource.traversal()
.withRemote(DriverRemoteConnection.using(client, "g"));
// To get the list of vertices
List<Vertex> vertices = graphTraversalSource.V().toList();
//To add a vertex
GraphTraversal newNode = graphTraversalSource.addV("Label 1");
//To add properties to the vertex
newNode.property("key1","value1");
newNode.property("key2",1002);
现在,我要求每个顶点必须具有一些预定义但动态的属性,例如名称、uuid 等。这些预定义属性可能因顶点而异(基于顶点标签),并且将来可能会更改;因此是动态的。由于这种动态,我无法使用预定义的 gremlin 模式。
现在我想我有两种选择来实现它。
Approach 1.
我可以将验证逻辑保留在我的 java 应用程序中,并仅在其有效时才传递给 gremlin。
Approach 2.
我可以实现一些遍历策略,例如 EventStrategy
第一个选项很简单,没有什么复杂的东西。对于第二个选项,我面临以下问题。
Issue 1.
我找不到他们实现的任何引用remote
和strategy
两者都具有相同的 GraphTraversalSource。
Issue 2.
如果验证失败,如何停止 Vertex 的创建。
我尝试了以下方法来实现 remote
和strategy
两者都具有相同的 GraphTraversalSource 但它给了我序列化错误。
// Here GremlinMutationListener is a class which implements MutationListener
MutationListener mutationListener = new GremlinMutationListener();
EventStrategy eventStrategy = EventStrategy.build().addListener(mutationListener).create();
GraphTraversalSource graphTraversalSource = AnonymousTraversalSource.traversal()
.withRemote(DriverRemoteConnection.using(client, "g"))
.withStrategies(eventStrategy);
我得到的错误是
Caused by: java.lang.IllegalArgumentException: Class is not registered: org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.EventStrategy
Note: To register this class use: kryo.register(org.apache.tinkerpop.gremlin.process.traversal.strategy.decoration.EventStrategy.class);
也在MutationListener
中除了抛出异常之外,我没有找到如何停止执行并返回验证错误的方法;这可能会有很多开销
public class GremlinMutationListener implements MutationListener {
private static final Logger LOGGER =
LoggerFactory.getLogger(GremlinMutationListener.class);
@Override
public void vertexAdded(Vertex vertex) {
LOGGER.info("SS: vertexAdded " + StringFactory.vertexString(vertex));
// How can I return the validation error from here besides throwing exception?
// Is there some other interface which I should implement?
}
.
.
.
.
现在的问题是,考虑到性能,这里最好的方法 1 或 2 是什么。如果是2如何解决我面临的问题(1和2)。
最佳答案
EventStrategy
不是进行验证的好方法。在基础图表中发生更改之前,您不会收到事件通知,因此验证错误来得太晚了。
我确实认为 TraversalStrategy
可以是实现验证的一种巧妙方法。我认为你会:
- 实现您自己的
ValidationTraversalStrategy
来查找任何突变步骤,然后检查其内容是否存在“不良数据”,如果出现问题,则会引发异常。由于策略应用发生在遍历迭代之前,您将在对底层图进行修改之前停止遍历。 - 在 Gremlin 服务器中配置“g”以使用策略设置服务器端,以便与其连接的所有连接自动受益于该策略。
这里的缺点是,并非所有图表都支持包含自定义遍历策略的功能,因此您需要接受采用这种方法降低代码可移植性的情况。
另一种更便携(也许更容易)的方法是构建 Gremlin DSL 。通过这种方式,您可以在构造遍历时立即实现验证客户端。例如,您可以添加如下步骤:
public default GraphTraversal<S, Vertex> person(String personId, String name) {
if (null == personId || personId.isEmpty()) throw new IllegalArgumentException("The personId must not be null or empty");
if (null == name || name.isEmpty()) throw new IllegalArgumentException("The name of the person must not be null or empty");
return coalesce(__.V().has(VERTEX_PERSON, KEY_PERSON_ID, personId),
__.addV(VERTEX_PERSON).property(KEY_PERSON_ID, personId)).
property(KEY_NAME, name);
}
该示例取自 KillrVideo example repo - 您可以在那里寻找更多灵感,还可以考虑与该存储库相关的相关博客文章:
- https://www.datastax.com/dev/blog/gremlin-dsls-in-java-with-dse-graph
- https://academy.datastax.com/content/gremlin-dsls-python-dse-graph
- https://academy.datastax.com/content/gremlin-dsls-net-dse-graph
尽管这些博客文章使用不同的编程语言,但每篇文章的内容都适用于使用任何语言的 Gremlin 的任何人。
关于java - 下面提供的 2 种验证 gremlin 节点和边的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56478329/