neo4j php client和驱动程序
控制世界最强大的图形数据库
- 通过简单的配置选择并选择驱动程序
- 直观的API
- 可扩展
- 在官方的Neo4J驾驶员团队的密切监督下设计,建造和测试
- 用TestKit验证
- 用诗篇完全键入
- 可用的螺栓,HTTP和自动路由驱动器
看到驾驶员在行动
- 类电影数据库的实现。它使用Slim和Neo4J-PHP-Client为经典电影的Neo4J示例构建API。
- 完整的现实示例实现。它使用Laravel实施现实世界示例项目,即所有演示应用程序的母亲。
- Friends API为世界上最老套的示例项目提供了NEO4J的力量。
有关一些更详细的文章,您可以参考这些博客文章:
- 如何使用Neo4J,PHP和Open API构建JSON RESTFUL API
- 使用Neo4J,AuradB和PHP构建网络应用
- 使用PHP连接到Neo4J
- 企业级PHP和NEO4J
或观看这些视频中的任何一个。
以三个简单的步骤开始您的驾驶体验
步骤1:通过作曲家安装
composer require laudis/neo4j-php-client在此处找到更多详细信息
步骤2:创建客户
use Laudis Neo4j Authentication Authenticate ;
use Laudis Neo4j ClientBuilder ;
$ client = ClientBuilder:: create ()
-> withDriver ( ' bolt ' , ' bolt+s://user:password@localhost ' ) // creates a bolt driver
-> withDriver ( ' https ' , ' https://t**e*st.com ' , Authenticate:: basic ( ' user ' , ' password ' )) // creates an http driver
-> withDriver ( ' neo4j ' , ' neo4j://neo4j.test.com?database=my-database ' , Authenticate:: oidc ( ' token ' )) // creates an auto routed driver with an OpenID Connect token
-> withDefaultDriver ( ' bolt ' )
-> build ();您现在已经使用Bolt,HTTPS和Neo4J驱动程序创建了一个客户端。客户端将使用的默认驱动程序是螺栓。
阅读有关URL以及如何使用它们在此处配置驱动程序的更多信息。
步骤3:运行交易
use Laudis Neo4j Contracts TransactionInterface ;
$ result = $ client -> writeTransaction ( static function ( TransactionInterface $ tsx ) {
$ result = $ tsx -> run ( ' MERGE (x {y: "z"}:X) return x ' );
return $ result -> first ()-> get ( ' x ' )[ ' y ' ];
});
echo $ result ; // echos 'z' 决定如何发送您的电视机查询
您可以使用三种不同的方法控制驱动程序:
- 自动构成的查询(最简单,最直观)
- 事务功能(最便携)
- 不受管理的交易(最高控制程度)
自动提交的查询
自动犯罪查询是最直接,最直观的问题,但是在运行复杂的业务逻辑或在高可用性环境中时具有许多缺点。
运行一个简单的密码查询
$ client -> run (
' MERGE (user {email: $email}) ' , //The query is a required parameter
[ ' email ' => ' abc@hotmail.com ' ], //Requests can be optionally added
' backup ' //The default connection can be overridden
);运行语句对象:
use Laudis Neo4j Databags Statement ;
$ statement = new Statement ( ' MERGE (user {email: $email}) ' , [ ' email ' => ' abc@hotmail.com ' ]);
$ client -> runStatement ( $ statement , ' default ' );一次运行多个查询
runStatements方法将一次运行所有语句。此方法是减少数据库调用数量的必不可少的工具,尤其是在使用HTTP协议时。
use Laudis Neo4j Databags Statement ;
$ results = $ client -> runStatements ([
Statement:: create ( ' MATCH (x) RETURN x LIMIT 100 ' ),
Statement:: create ( ' MERGE (x:Person {email: $email}) ' , [ ' email ' => ' abc@hotmail.com ' ])
]);交易功能
交易功能是使用驱动程序时事实上的标准。它是最容易发生的,因为它首先使用高可用性解决方案(例如Neo4J Aura或cluster)进行了抗性。
驾驶员管理事务功能:
- 在瞬态误差的情况下,它重新执行该功能。
- 它在成功执行时承担交易
- 在超时的情况下,它会回滚交易。
- 当启用NEO4J协议时,它将执行路由到相关的追随者或领导者服务器。
注意:由于具有自动重试功能,该功能应在随后的召回中产生相同的结果,或者以更多的技术方式:应该是愿意的。在函数中设计执行逻辑时,请务必记住这一点。
一些例子:
use Laudis Neo4j Contracts TransactionInterface ;
// Do a simple merge and return the result
$ result = $ client -> writeTransaction ( static function ( TransactionInterface $ tsx ) {
$ result = $ tsx -> run ( ' MERGE (x {y: "z"}:X) return x ' );
return $ result -> first ()-> get ( ' x ' )[ ' y ' ];
});
// Will result in an error
$ client -> readTransaction ( static function ( TransactionInterface $ tsx ) {
$ tsx -> run ( ' MERGE (x {y: "z"}:X) return x ' );
});
// This is a poorly designed transaction function
$ client -> writeTransaction ( static function ( TransactionInterface $ tsx ) use ( $ externalCounter ) {
$ externalCounter -> incrementNodesCreated ();
$ tsx -> run ( ' MERGE (x {y: $id}:X) return x ' , [ ' id ' => Uuid:: v4 ()]);
});
// This achieves the same effect but is safe in case it should be retried. The function is now idempotent.
$ id = Uuid:: v4 ();
$ client -> writeTransaction ( static function ( TransactionInterface $ tsx ) use ( $ id ) {
$ tsx -> run ( ' MERGE (x {y: $id}:X) return x ' , [ ' id ' => $ id ]);
});
$ externalCounter -> incrementNodesCreated ();不受管理的交易
如果您需要低级访问驾驶员的功能,则需要非托管的交易。它们允许完全可控的提交和回滚。
打开交易
beginTransaction方法将与相关驱动程序启动交易。
use Laudis Neo4j Databags Statement ;
$ tsx = $ client -> beginTransaction (
// This is an optional set of statements to execute while opening the transaction
[Statement:: create ( ' MERGE (x:Person({email: $email}) ' , [ ' email ' => ' abc@hotmail.com ' ])],
' backup ' // This is the optional connection alias
);请注意,
beginTransaction仅返回交易对象,而不返回所提供的语句的结果。
交易中的运行语句
只要客户对象仍然打开,该事务就可以像客户端对象一样运行语句。
$ result = $ tsx -> run ( ' MATCH (x) RETURN x LIMIT 100 ' );
$ result = $ tsx -> runStatement (Statement:: create ( ' MATCH (x) RETURN x LIMIT 100 ' ));
$ results = $ tsx -> runStatements ([Statement:: create ( ' MATCH (x) RETURN x LIMIT 100 ' )]);完成交易
回滚交易:
$ tsx -> rollback ();进行交易:
$ tsx -> commit ([Statement:: create ( ' MATCH (x) RETURN x LIMIT 100 ' )]);访问结果
结果以行和列的标准格式返回:
// Results are a CypherList
$ results = $ client -> run ( ' MATCH (node:Node) RETURN node, node.id AS id ' );
// A row is a CypherMap
foreach ( $ results as $ result ) {
// Returns a Node
$ node = $ result -> get ( ' node ' );
echo $ node -> getProperty ( ' id ' );
echo $ result -> get ( ' id ' );
}Cypher值和类型映射到这些PHP类型和类:
| 暗号 | php |
|---|---|
| 无效的 | * null
|
| 细绳 | * string
|
| 整数 | * int
|
| 漂浮 | * float
|
| 布尔 | * bool
|
| 地图 | * LaudisNeo4jTypesCypherMap
|
| 列表 | * LaudisNeo4jTypesCypherList
|
| 观点 | * LaudisNeo4jContractsPointInterface ** |
| 日期 | * LaudisNeo4jTypesDate
|
| 时间 | * LaudisNeo4jTypesTime
|
| 本地时间 | * LaudisNeo4jTypesLocalTime
|
| DateTime | * LaudisNeo4jTypesDateTime
|
| DateTimeZoneID | * LaudisNeo4jTypesDateTimeZoneId
|
| LocalDateTime | * LaudisNeo4jTypesLocalDateTime
|
| 期间 | * LaudisNeo4jTypesDuration
|
| 节点 | LaudisNeo4jTypesNode |
| 关系 | LaudisNeo4jTypesRelationship |
| 小路 | LaudisNeo4jTypesPath |
(*)这些项目也可以用作螺栓协议中的参数,并将自动由驱动程序转换,因此可以在Cypher中使用。
除这些示例外, DateTimeInterface还将在Cypher中映射到DateTimeZoneId 。空或列表类型array将转换为Cypher List ,并将associative array转换为map 。
(**)一个点可以是实现点接口的四个类型之一: LaudisNeo4jTypesCartesianPoint LaudisNeo4jTypesCartesian3DPoint LaudisNeo4jTypesWGS84Point LaudisNeo4jTypesWGS843DPoint
潜水更深
区分参数类型
Cypher具有列表和地图。由于标准PHP阵列都封装了这两者,因此这个概念可能是有问题的。当您提供一个空数组作为参数时,将不可能确定一个空列表或映射。
ParameterHelper类是这样的理想伴侣:
use Laudis Neo4j ParameterHelper ;
$ client -> run ( ' MATCH (x) WHERE x.slug in $listOrMap RETURN x ' , [ ' listOrMap ' => ParameterHelper:: asList ([])]); // will return an empty CypherList
$ client -> run ( ' MATCH (x) WHERE x.slug in $listOrMap RETURN x ' , [ ' listOrMap ' => ParameterHelper:: asMap ([])]); // will error
$ client -> run ( ' MATCH (x) WHERE x.slug in $listOrMap RETURN x ' , [ ' listOrMap ' => []]); // will return an empty CypherList版本兼容性矩阵
| 驱动程序版本 | PHP版本 | neo4j版本 |
|---|---|---|
| ^2.8 | 7.4, ^8.0 | ^3.5, ^4.0 |
| ^3.0 | ^8.0 | ^4.0, ^5.0 |
NEO4J功能支持
| 特征 | 支持? |
|---|---|
| 验证 | 是的 |
| 交易 | 是的 |
| HTTP协议 | 是的 |
| 螺栓协议 | 是的 |
| 簇 | 是的 |
| 光环 | 是的 |
| 震动协议 | 是的 |
| 书签 | 是的 |
深入要求
- php> = 7.4
- NEO4J数据库(最低版本3.5)
- EXT-BCMATH *
- ext-json **
- Ext-sockets ***
(*)需要实施螺栓协议
(**)实现HTTP协议需要
(***)可以安装以进行最佳螺栓协议性能
如果您打算使用HTTP驱动程序,请确保项目中包含的PSR-7,PSR-17和PSR-18实现。如果没有,则可以通过作曲家安装它们:
composer require nyholm/psr7 nyholm/psr7-server kriswallsmith/buzz结果格式/水合
为了制定螺栓协议和HTTP统一的结果,驾驶员提供并总结了结果。
默认格式是LaudisNeo4jFormattersSummarizedResultFormatter ,它在结果格式部分中进行了广泛的解释。
LaudisNeo4jFormatterSummarizedResultFormatter添加了广泛的结果摘要。
客户端构建器提供了一种更改格式器的简便方法:
$ client = Laudis Neo4j ClientBuilder:: create ()-> build ();
/**
* The client will now return a result, decorated with a summary.
*
* @var LaudisNeo4jDatabagsSummarizedResult $results
*/
$ summarisedResult = $ client -> run ( ' MATCH (x) RETURN x ' );
// The summary contains extensive information such as counters for changed values in the database,
// information on the database, potential notifications, timing, a (profiled) plan, the type of query
// and information on the server itself.
$ summary = $ summarisedResult -> getSummary ();
// The result is exactly the same as the default.
$ result = $ summarisedResult -> getResult ();概念
此处描述的驱动程序API是驱动程序的主要目标。因此,客户只不过是驾驶员经理。驾驶员创建会议。会话通过交易进行查询。
由于这种行为,您可以从客户端开始访问每个概念:
use Laudis Neo4j ClientBuilder ;
// A builder is responsible for configuring the client on a high level.
$ builder = ClientBuilder:: create ();
// A client manages the drivers as configured by the builder.
$ client = $ builder -> build ();
// A driver manages connections and sessions.
$ driver = $ client -> getDriver ( ' default ' );
// A session manages transactions.
$ session = $ driver -> createSession ();
// A transaction is the atomic unit of the driver where are the cypher queries are chained.
$ transaction = $ session -> beginTransaction ();
// A transaction runs the actual queries
$ transaction -> run ( ' MATCH (x) RETURN count(x) ' );如果需要完整的控制,则可以使用自定义配置对象控制每个对象。
客户
客户根据预配置的别名管理驱动程序并将查询路由到正确的驱动程序。
司机
驱动器对象是可访问neo4j的线程安全主干。它拥有一个连接池,可以产生会话以进行工作。
use Laudis Neo4j Basic Driver ;
use Laudis Neo4j Databags DriverConfiguration ;
$ driver = Driver:: create (
uri: ' neo4j://user:mypassword@Localhost:7687 ' ,
configuration: DriverConfiguration:: create ()-> withUserAgent ( ' MyApp/1.0.0 ' )
);会议
会话是用于因果链接交易序列的轻巧容器。他们根据需要从连接池借用连接,并使用书签借用链交易。
use Laudis Neo4j Databags SessionConfiguration ;
use Laudis Neo4j Enum AccessMode ;
$ session = $ driver -> createSession (SessionConfiguration:: create ()
-> withDatabase ( ' my-database ' )
-> withAccessMode (AccessMode:: READ ())
);交易
交易是可能包含一个或多个查询的工作单位。每个交易都与单个连接结合,并通过书签在因果链中表示。
陈述
查询是交易中的可执行单元,由一个Cypher String和一个键入参数集组成。每个查询都输出可能包含零或更多记录的结果。
结果
结果包含来自查询的输出,由标头元数据,内容记录和摘要元数据组成。在NEO4J 4.0及更高版本中,应用程序可以控制结果数据流。
深入配置
URL方案
URL方案是配置驱动程序的最简单方法。
配置格式:
'://:@:?database='
默认配置:
bolt://localhost:7687?database=neo4j
方案配置矩阵
该库支持三个驱动程序:Bolt,HTTP和Neo4J。 URL的方案一部分确定驱动程序。
| 司机 | 方案 | 有效证书 | 自签名证书 | 功能 |
|---|---|---|---|---|
| neo4j | neo4j | neo4j+s | neo4j+ssc | 客户侧路由螺栓 |
| 螺栓 | 螺栓 | 螺栓+s | 螺栓+SSC | 单螺栓上的单个服务器 |
| http | http | https | 通过PSR客户端实现配置 | http上的单个服务器 |
配置对象
可以使用配置对象配置驱动程序,会话和事务。可以在此处找到配置选项的概述:
| 姓名 | 概念 | 描述 | 班级 |
|---|---|---|---|
| 用户代理 | 司机 | 用户代理用于将客户端识别为Neo4J服务器。 | DriverConfiguration
|
| HTTP PSR结合 | 司机 | 使用HTTP协议时,驱动程序使用的相关PSR实现。 | DriverConfiguration
|
| 数据库 | 会议 | 要连接的数据库。 | SessionConfiguration
|
| 提取大小 | 会议 | 立即获取的行量。 | SessionConfiguration
|
| 访问模式 | 会议 | 访问服务器时的默认模式。 | SessionConfiguration
|
| 书签 | 会议 | 会议中使用的书签。 (实验) | SessionConfiguration
|
| 元数据 | 交易 | 交易期间使用的元数据。 (实验) | TransactionConfiguration
|
| 暂停 | 交易 | 预时之前的最长时间。 | TransactionConfiguration
|
代码示例:
use Laudis Neo4j Databags DriverConfiguration ;
use Laudis Neo4j Databags SessionConfiguration ;
use Laudis Neo4j Databags TransactionConfiguration ;
$ client = Laudis Neo4j ClientBuilder:: create ()
-> withDefaultDriverConfiguration (DriverConfiguration:: default ()-> withUserAgent ( ' MyApp/1.0.0 ' ))
-> withDefaultSessionConfiguration (SessionConfiguration:: default ()-> withDatabase ( ' app-database ' ))
-> withDefaultTransactionConfiguration (TransactionConfiguration:: default ()-> withTimeout ( 5.0 ))
-> build ();
// The client will run the query on a driver with the provided config,
// which spawns a session with the provided session config
// and runs the query in a transaction with the provided transaction config
$ client -> run ( ' MATCH (x) RETURN count(x) AS count ' );
// More granular control can be achieved by requesting the concepts yourself:
$ tsx = $ client -> getDriver ( ' default ' )
-> createSession (SessionConfiguration:: default ()-> withDatabase ( ' management-database ' ))
-> beginTransaction ( null , TransactionConfiguration:: default ()-> withTimeout ( 200 ));
$ tsx -> run ( ' SOME REALLY LONG MANAGEMENT QUERY ' );
$ tsx -> commit ();通过命令行克隆项目: