neo4j
Connecting to a Neo4j database:
using Neo4j.Driver;
await using var driver = GraphDatabase.Driver("bolt://localhost:7687", AuthTokens.Basic("neo4j", "password"));
There are a few points to highlight when adding the driver to your project:
在将驱动程序添加到项目中时,有几个要点需要强调:
- Each
IDriver
instance maintains a pool of connections inside; as a result, use a single driver instance per application. - 每个IDriver实例都在内部维护一个连接池;因此,每个应用程序只使用一个驱动程序实例。
- Sessions and transactions do not open new connections if a free one is in the driver's connection pool; this makes both resources cheap to create and close.
- 如果驱动程序的连接池中有空闲连接,会话和事务不会打开新连接;这使得创建和关闭两种资源都很便宜。
- The driver is thread-safe and made to be used across an application. Sessions and transactions are not thread-safe; using a session or transaction concurrently will result in undefined behavior.
- 驱动程序是线程安全的,可以在应用程序中跨线程使用。会话和事务不是线程安全的;同时使用一个会话或事务会导致行为不确定。
Verifying connectivity:
await driver.VerifyConnectivityAsync();
To ensure the credentials and URLs specified when creating the driver, you can call VerifyConnectivityAsync
on the driver instance. If either configuration is wrong, the Task will result in an exception.
Executing a single query transaction:
await driver.ExecutableQuery("CREATE (:Node{id: 0})")
.WithConfig(new QueryConfig(database:"neo4j"))
.ExecuteAsync();
As of version 5.10, The .NET driver includes a fluent querying API on the driver's IDriver interface. The fluent API is the most concise API for executing single query transactions. It avoids the boilerplate that comes with handling complex problems, such as results that exceed memory or multi-query transactions.
从版本 5.10 开始,.NET 驱动程序在 IDriver 接口上包含了一个fluent 查询 API。fluent 的 API 是执行单查询事务的最简洁的 API。它避免了处理复杂问题(如超出内存的结果或多查询事务)时出现的样板代码。
Remember to specify a database记得指定一个数据库。.
.WithConfig(new QueryConfig(database:"neo4j"))
Always specify the database when you know which database the transaction should execute against. By setting the database parameter, the driver avoids a roundtrip and concurrency machinery associated with negotiating a home database.
在知道事务应该针对哪个数据库执行时,始终指定数据库。通过设置数据库参数,驱动程序可以避免与协商主数据库相关的往返和并发机制。
Getting Results
var response = await driver.ExecutableQuery("MATCH (n:Node) RETURN n.id as id")
.WithConfig(dbConfig)
.ExecuteAsync();
The response from the fluent APIs is an EagerResult<IReadOnlyList<IRecord>> unless we use other APIs; more on that later. EagerResult comprises of the following:
从fluent 的API得到的响应是一个EagerResult<IReadOnlyList<IRecord>>,除非我们使用其他API;更多关于这个的内容我们稍后讨论。EagerResult包含以下内容:
- All records materialized(
Result
).所有记录已实现(结果)。 - keys returned from the query(
Keys
). - a query summary(
Summary
).
Decomposing EagerResult 分解EagerResult
var (result, _, _) = await driver.ExecutableQuery(query)
.WithConfig(dbConfig)
.ExecuteAsync();
foreach (var record in result)
Console.WriteLine($"node: {record["id"]}")
EagerResult allows you to discard unneeded values with decomposition for an expressive API.
EagerResult允许您通过分解来丢弃不需要的值,从而实现表达式API。
Mapping
var (result, _, _) = await driver.ExecutableQuery(query)
.WithConfig(dbConfig)
.WithMap(record => new EntityDTO { id = record["id"].As<long>() })
.ExecuteAsync();
Types
Values in a record are currently exposed as of object
type. The underlying types of these values are determined by their Cypher types.
The mapping between driver types and Cypher types are listed in the table bellow:
记录中的值目前以对象类型公开。这些值的底层类型由它们的Cypher类型决定。 驱动程序类型和Cypher类型之间的映射在下面的表格中列出:
Cypher Type | Driver Type |
---|---|
null | null |
List | IList< object > |
Map | IDictionary<string, object> |
Boolean | boolean |
Integer | long |
Float | float |
String | string |
ByteArray | byte[] |
Point | Point |
Node | INode |
Relationship | IRelationship |
Path | IPath |
To convert from object
to the driver type, a helper method ValueExtensions#As<T>
can be used:
IRecord record = await result.SingleAsync();
string name = record["name"].As<string>();
Temporal 时间上的 Types - Date and Time
The mapping among the Cypher temporal types, driver types, and convertible CLR temporal types - DateTime, TimeSpan and DateTimeOffset - (via IConvertible
interface) are as follows:
Cypher时间类型、驱动程序类型和可转换的CLR时间类型(通过IConvertible接口)之间的映射关系如下:
Cypher Type | Driver Type | Convertible CLR Type |
---|---|---|
Date | LocalDate | DateTime, DateOnly(.NET6+) |
Time | OffsetTime | TimeOnly(.NET6+) |
LocalTime | LocalTime | TimeSpan, DateTime |
DateTime | ZonedDateTime | DateTimeOffset |
LocalDateTime | LocalDateTime | DateTime |
Duration | Duration | --- |