从.NET连接
Gremlin遍历可以使用Gremlin.Net构建,就像在Gremlin-Java或Gremlin-Groovy中一样。有关Gremlin的介绍和进一步资源的指引,请参考Gremlin查询语言。Gremlin.Net的主要语法差异在于它遵循.NET命名约定,例如,方法名使用PascalCase而不是camelCase。
JanusGraph 与 Gremlin.Net 入门指南
开始使用 Gremlin.Net:
-
创建一个控制台应用程序:
dotnet new console -o GremlinExample -
添加 Gremlin.Net:
dotnet add package Gremlin.Net -v 3.7.3 -
创建一个
GraphTraversalSource,它是所有Gremlin遍历的基础:using static Gremlin.Net.Process.Traversal.AnonymousTraversalSource; var client = new GremlinClient(new GremlinServer("localhost", 8182)); // 客户端应在关闭时释放资源 // 并通过 client.Dispose() 关闭打开的连接 var g = Traversal().WithRemote(new DriverRemoteConnection(client)); // 在整个应用程序中重用 'g' -
执行一个简单的遍历:
遍历也可以通过使用var herculesAge = g.V().Has("name", "hercules").Values<int>("age").Next(); Console.WriteLine($"Hercules is {herculesAge} years old.");Promise()异步执行,这是推荐的方式,因为Gremlin.Net中的底层驱动程序也是异步工作的:var herculesAge = await g.V().Has("name", "hercules").Values<int>("age").Promise(t => t.Next());
JanusGraph.Net 针对 JanusGraph 特定类型和谓词
JanusGraph 包含一些不属于 Apache TinkerPop 的类型和 谓词,因此 Gremlin.Net 也不支持这些类型和谓词。 JanusGraph.Net 是一个 .NET 库,用于为 Gremlin.Net 添加对这些类型和谓词的支持。
安装库后,需要为JanusGraph配置消息序列化器,对于GraphSON 3可以这样操作:
var client = new GremlinClient(new GremlinServer("localhost", 8182),
new JanusGraphGraphSONMessageSerializer());
或者对于GraphBinary来说是这样的:
var client = new GremlinClient(new GremlinServer("localhost", 8182),
new GraphBinaryMessageSerializer(JanusGraphTypeSerializerRegistry.Instance));
请参考JanusGraph.Net的文档以获取更多关于该库的信息,包括其与不同JanusGraph版本的兼容性以及不同序列化格式之间的支持差异。