gino-pane / neo4j-php-client
Neo4j-PHP-Client 是最先进的 Neo4j PHP 客户端
Requires
- php: ^7.1.3|^8.0
- ext-bcmath: *
- ext-mbstring: *
- autoprotect-group/neo4j-bolt: ^1.12.4
- autoprotect-group/neo4j-common: ^3.6.2
- myclabs/php-enum: ^1.4
- php-http/client-common: ^2.3
- php-http/discovery: ^1.10
- php-http/guzzle6-adapter: ^2.0.2
- php-http/httplug: ^2.2
- php-http/message: ^1.0
- php-http/message-factory: ^1.0
- symfony/event-dispatcher: ^4.0 || ^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- phpunit/phpunit: ^4.0
- symfony/stopwatch: ^3.0
- dev-master
- 5.4.4
- 5.3.4
- 5.3.3
- 5.3.2
- 5.3.1
- 5.3.0
- 5.2.0
- 5.1.1
- 5.1.0
- 5.0.0
- 4.8.5
- 4.8.4
- 4.8.3
- 4.8.2
- 4.8.1
- 4.8.0
- 4.7.0
- 4.6.4
- 4.6.3
- 4.6.2
- 4.6.1
- 4.6.0
- 4.5.5
- 4.5.4
- 4.5.3
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.7
- 4.4.6
- 4.4.5
- 4.4.4
- 4.4.3
- 4.4.2
- 4.4.1
- 4.4.0
- 4.3.1
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.1
- 4.1.0
- 4.0.x-dev
- 4.0.2
- 4.0.1
- 4.0.0
- 4.0.0-alpha15
- 4.0.0-alpha14
- 4.0.0-alpha13
- 4.0.0-alpha12
- 4.0.0-alpha11
- 4.0.0-alpha10
- 4.0.0-alpha9
- 4.0.0-alpha8
- 4.0.0-alpha7
- 4.0.0-alpha6
- 4.0.0-alpha5
- 4.0.0-alpha4
- 4.0.0-alpha3
- 4.0.0-alpha2
- 4.0.0-alpha1
- 3.4.x-dev
- 3.4.2
- 3.4.1
- 3.3.20
- 3.3.19
- 3.3.18
- 3.3.17
- 3.3.16
- 3.3.15
- 3.3.14
- 3.3.13
- 3.3.12
- 3.3.11
- 3.3.10
- 3.3.9
- 3.3.8
- 3.3.7
- 3.3.6
- 3.3.5
- 3.3.4
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.2.1
- 3.2.0
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.x-dev
- 3.0.2
- 3.0.1
- 3.0.0
- 2.2.10
- 2.2.9
- 2.2.8
- 2.2.7
- 2.2.6
- 2.2.5
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2
- 2.1.20
- 2.1.19
- 2.1.18
- 2.1.17
- 2.1.16
- 2.1.15
- 2.1.14
- 2.1.13
- 2.1.12
- 2.1.11
- 2.1.10
- 2.1.9
- 2.1.8
- 2.1.7
- 2.1.6
- 2.1.5
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1.x-dev
- 2.1.1
- 2.1.0
- 2.0.x-dev
- 2.0.11
- 2.0.10
- 2.0.9
- 2.0.8
- 2.0.7
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.7.x-dev
- 1.6.x-dev
- 1.6.9
- 1.6.8
- 1.6.7
- 1.6.6
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.6
- 1.4.5
- 1.4.4
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4
- 1.3.0
- 1.2.0
- 1.1.0
- 1.0.1
- 1.0.0
- dev-ClaasBrueggemann-feature/v4-support
- dev-issue105-bis
- dev-issue-105
- dev-fix/empty-array
- dev-tx-events
- dev-issue40
- dev-ikwattro-patch-12
- dev-ikwattro-patch-11
- dev-ikwattro-patch-10
- dev-ikwattro-patch-9
- dev-ikwattro-patch-8
- dev-ikwattro-patch-7
- dev-ikwattro-patch-6
- dev-ikwattro-patch-5
- dev-ikwattro-patch-4
- dev-ikwattro-patch-3
- dev-ikwattro-patch-2
- dev-ikwattro-patch-1
- dev-log
- dev-preview
- dev-livetx-fix
- dev-schema
- dev-issue-stefan
- dev-ha-no-yaml
- dev-connected-nodes
- dev-data-table
- dev-rows
- dev-custom-headers
- dev-travis
- dev-changePassword
- dev-3.0-dev
- dev-prepared-transaction
- dev-relProp
- dev-auth2
- dev-restformat
- dev-rest
- dev-client-changes
- dev-path
- dev-txmanager
- dev-1.6-dev
- dev-1.5-dev
- dev-1.4-dev
This package is auto-updated.
Last update: 2024-09-20 12:51:28 UTC
README
此仓库已存档。此仓库不再由 Navigate/APG 支持
GraphAware Neo4j PHP 客户端
Neo4j 的企业级客户端
简介
Neo4j-PHP-Client 是最先进且灵活的 Neo4j PHP 客户端。
什么是 Neo4j?
Neo4j 是一个事务性的开源图数据库。图数据库以连接的数据结构管理数据,能够以非常易于访问的方式表示任何类型的数据。信息存储在节点及其连接的边中,这两者都可以有任意属性。了解更多信息,请访问 什么是图数据库?
主要特性
- 支持多个连接
- 支持 Bolt 二进制协议
- 内置并自动支持 Neo4j 企业高可用主从模式,并具有自动从节点回退功能
Neo4j 版本支持
Neo4j 功能支持
要求
- PHP >= 5.6
- ext-bcmath
- ext-mbstring
- Neo4j 数据库(最低版本 2.2.6)
获取帮助
您可以
- 在 StackOverflow 上提问
- 对于错误,请随意在 GitHub 上创建 新问题
实现
安装和基本用法
安装
将库添加到您的 composer 依赖项
composer require "autoprotect-group/neo4j-php-client:^5.0"
需要 composer 自动加载器,通过提供连接别名和连接设置来配置您的连接
<?php require_once 'vendor/autoload.php'; use GraphAware\Neo4j\Client\ClientBuilder; $client = ClientBuilder::create() ->addConnection('default', 'http://neo4j:password@localhost:7474') // Example for HTTP connection configuration (port is optional) ->addConnection('bolt', 'bolt://neo4j:password@localhost:7687') // Example for BOLT connection configuration (port is optional) ->build();
现在您可以连接到数据库了。
注意:构建方法将处理配置设置并返回一个 Client
实例。
基本用法
发送 Cypher 查询
$client->run('CREATE (n:Person)');
带有参数的 Cypher 查询
$client->run('CREATE (n:Person) SET n += {infos}', ['infos' => ['name' => 'Ales', 'age' => 34]]);
读取结果
$result = $client->run('MATCH (n:Person) RETURN n'); // a result always contains a collection (array) of Record objects // get all records $records = $result->getRecords(); // get the first or (if expected only one) the only record $record = $result->getRecord();
Record
对象包含您的 Cypher 查询返回的一条记录的值
$query = 'MATCH (n:Person)-[:FOLLOWS]->(friend) RETURN n.name, collect(friend) as friends'; $result = $client->run($query); foreach ($result->getRecords() as $record) { echo sprintf('Person name is : %s and has %d number of friends', $record->value('name'), count($record->value('friends'))); }
Cypher 语句和堆栈
理想情况下,您会堆叠您的语句,并一次执行所有语句以提高性能。
您可以创建 Cypher 语句堆栈,作为包来运行此堆栈,例如
$stack = $client->stack(); $stack->push('CREATE (n:Person {uuid: {uuid} })', ['uuid' => '123-fff']); $stack->push('MATCH (n:Person {uuid: {uuid1} }), (n2:Person {uuid: {uuid2} }) MERGE (n)-[:FOLLOWS]->(n2)', ['uuid1' => '123-fff', 'uuid2' => '456-ddd']); $results = $client->runStack($stack);
标记您的 Cypher 语句
有时,您可能希望从堆栈中检索特定结果,一种简单的方法是标记您的 Cypher 语句。
标记通过 run
或 push
方法的第三个参数传递
$stack = $client->stack(); $stack->push('CREATE (n:Person {uuid: {uuid} })', ['uuid' => '123-fff'], 'user_create'); $stack->push('MATCH (n:Person {uuid: {uuid1} }), (n2:Person {uuid: {uuid2} }) MERGE (n)-[r:FOLLOWS]->(n2) RETURN id(r) as relId', ['uuid1' => '123-fff', 'uuid2' => '456-ddd'], 'user_follows'); $results = $client->runStack($stack); $followResult = $results->get('user_follows'); $followRelationshipId = $followResult->getRecord()->value('relId');
处理结果集
基本
run
方法返回一个单独的 Result
对象。其他可以预期返回多个结果的方法返回一个可遍历的 ResultCollection
对象
结果
$result->firstRecord(); // Returns the first record of the Statement Result $result->records(); // Returns all records $result->summarize(); // Returns the ResultSummary
摘要
ResultSummary
包含 Statement
、统计信息和可选的查询计划
$summary = $result->summarize(); $query = $summary->statement()->text(); $stats = $summary->updateStatistics(); $nodesUpdated = $stats->nodesUpdated(); $propertiesSet = $stats->propertiesSet(); // Does the statement affected the graph ? $affected = $stats->containsUpdates();
记录值
每个记录都包含 Cypher 查询返回的一行值
$query = 'MATCH (n:Person) n, n.name as name, n.age as age';
$result = $client->run($query);
foreach ($result->records() as $record) {
print_r($record->get('n')); // nodes returned are automatically hydrated to Node objects
echo $record->value('name') . PHP_EOL;
echo $record->value('age') . PHP_EOL;
}
客户端负责将图对象转换为 PHP 对象,因此对于节点、关系和路径都是这样
节点
labels()
: 返回一个标签(字符串)数组identity()
: 返回节点的内部IDvalues()
: 返回节点的属性(数组)value($key)
: 返回给定属性键的值hasValue($key)
: 返回节点是否具有给定键的属性keys()
: 返回节点属性键的数组hasLabel($label)
: 返回节点是否具有给定标签(布尔值)
关系
type()
: 返回关系类型identity()
: 返回关系的内部IDvalues()
: 返回关系的属性(数组)value($key)
: 返回给定属性键的值hasValue($key)
: 返回关系是否具有给定键的属性keys()
: 返回关系属性键的数组startNodeIdentity
: 返回起始节点IDendNodeIdentity
: 返回终止节点ID
路径
start()
: 返回路径的起始节点end()
: 返回路径的终止节点length()
: 返回路径长度nodes()
: 返回路径中的所有节点relationships
: 返回路径中的所有关系
处理结果(从v3到v4)
此主题有3个主要概念
- 结果
- 记录
- 记录值
让我们看看浏览器中执行的一个查询,该查询包含多种类型的可能性
MATCH (n:Address)
RETURN n.address as addr, n, collect(id(n)) as ids
LIMIT 5
结果
Result
是一组 Record
对象的集合,你在浏览器中看到的每一行都是一个 Record
,并包含 Record Value
。
- 蓝色为结果
- 橙色为记录
- 绿色为记录值
记录
与客户端的先前版本相反,不再自动将所有记录合并为一个大型记录,因此您需要从 Result
中迭代所有记录
$query = 'MATCH (n:Address) RETURN n.address as addr, n, collect(id(n)) as ids LIMIT 5'; $result = $client->run($query); foreach ($result->records() as $record) { // here we do what we want with one record (one row in the browser result) print_r($record); }
记录值
每个记录都包含一个 Record Value
集合,这些值通过一个 key
来标识,该键是您在 Cypher 查询的 RETURN
子句中给出的标识符。在我们的示例中,一个 Record
将包含以下键
- addr
- n
- ids
为了访问值,您可以在 Record
对象上使用 get()
方法
$address = $record->get('addr');
值的类型取决于 Neo4j 返回的内容,在我们的例子中,以下值将被返回
- 对于
addr
值,一个string
- 对于
n
值,一个Node
- 对于
ids
值,一个integer
的array
这意味着
$record->get('addr'); // returns a string $record->get('n'); // returns a Node object $record->get('ids'); // returns an array
Node
、Relationship
和 Path
对象有进一步的方法,所以如果你知道由标识符 n
返回的节点有一个 countries
属性,你可以这样访问它
$addressNode = $record->get('n'); $countries = $addressNode->value('countries');
Record
对象包含三个方法,以增强IDE的友好性,即
$record->nodeValue('n'); $record->relationshipValue('r'); $record->pathValue('p');
这并不提供额外的功能,只是docblocks为自动完成提供了IDE的提示。
额外:结果集合
当你使用 Stack
对象一次性发送多个语句时,它将返回一个包含多个 Result
的 ResultCollection
对象。所以你需要在访问记录之前迭代结果。
与事务一起工作
客户端提供了一个事务对象,它简化了您的工作方式。
创建一个事务
$tx = $client->transaction();
在这个阶段,还没有向服务器发送任何内容(尚未发送 BEGIN 语句),这允许在提交之前堆叠查询或 Stack 对象。
堆叠一个查询
$tx->push('CREATE (n:Person) RETURN id(n)');
同样,到目前为止还没有发送任何内容。
在事务中运行一个查询
有时您希望立即获取事务内部语句的结果,这可以通过使用 run
方法实现。
$result = $tx->run('CREATE (n:Person) SET n.name = {name} RETURN id(n)', ['name' => 'Michal']); echo $result->getRecord()->value("id(n)");
如果事务尚未开始,事务的 BEGIN 将自动执行。
您还可以推送或运行堆栈。
$stack = $client->stack(); $stack->push('CREATE (n:Person {uuid: {uuid} })', ['uuid' => '123-fff']); $stack->push('MATCH (n:Person {uuid: {uuid1} }), (n2:Person {uuid: {uuid2} }) MERGE (n)-[:FOLLOWS]->(n2)', ['uuid1' => '123-fff', 'uuid2' => '456-ddd']); $tx->pushStack($stack); // or $results = $tx->runStack($stack);
提交和回滚
如果您在事务中排好了语句(使用 push
方法添加的语句),并且您已完成工作,则可以提交事务并接收结果。
$stack = $client->stack(); $stack->push('CREATE (n:Person {uuid: {uuid} })', ['uuid' => '123-fff']); $stack->push('MATCH (n:Person {uuid: {uuid1} }), (n2:Person {uuid: {uuid2} }) MERGE (n)-[:FOLLOWS]->(n2)', ['uuid1' => '123-fff', 'uuid2' => '456-ddd']); $tx->pushStack($stack); $tx->pushQuery('MATCH (n) RETURN count(n)'); $results = $tx->commit();
提交后,您将无法在此事务中 push
或 run
语句。
处理多个连接
一般来说,您最好使用 HAProxy 在集群环境中运行 Neo4j。然而,有时您需要完全控制将语句发送到哪个实例。
假设有一个包含 3 个 neo4j 节点的环境
$client = ClientBuilder::create() ->addConnection('node1', 'bolt://10.0.0.1') ->addConnection('node2', 'bolt://10.0.0.2') ->addConnection('node3', 'bolt://10.0.0.3') ->setMaster('node1') ->build();
默认情况下,$client->run()
命令会将您的 Cypher 语句发送到列表中注册的第一个连接。
您可以通过指定别名作为运行参数的第四个参数来指定要将语句发送到哪个连接。
$result = $client->run('CREATE (n) RETURN n', null, null, 'node1');
客户端也了解手动配置的主连接,因此使用以下方法发送写入操作会更简单:
$client->runWrite('CREATE (n:User {login: 123})');
辅助方法
$client->getLabels();
返回一个 Label
对象数组。
事件分发
在 run
方法期间会分发 3 种类型的事件
PreRunEvent
:在执行语句或堆栈之前。PostRunEvent
:在执行语句或堆栈之后。FailureEvent
:在发生错误的情况下,您可以使用此事件禁用客户端抛出异常。
注册监听器
示例
$client = ClientBuilder::create() ->addConnection('default', 'bolt://') ->registerEventListener(Neo4jClientEvents::NEO4J_PRE_RUN, array($listener, 'onPreRun') ->build();
事件分发器可以通过客户端的 $client->getEventDispatcher
方法访问。
设置
超时(已弃用)
您可以配置连接的全局超时。
$client = ClientBuilder::create() ->addConnection('default', 'https://:7474') ->setDefaultTimeout(3) ->build();
默认的超时时间为 5 秒。
此功能已弃用,将在版本 5 中删除。请参阅以下 HTTP 客户端设置。
TLS
您可以通过在建立连接时传递一个 Configuration
实例来启用 Bolt 协议的 TLS 加密,以下是一个简单的示例:
$config = \GraphAware\Bolt\Configuration::newInstance()
->withCredentials('bolttest', 'L7n7SfTSj')
->withTLSMode(\GraphAware\Bolt\Configuration::TLSMODE_REQUIRED);
$client = ClientBuilder::create()
->addConnection('default', 'bolt://hodccomjfkgdenl.dbs.gdb.com:24786', config)
->build();
HTTP 客户端设置
我们使用 HTTPlug 来为您提供对 HTTP 客户端的完全控制。Neo4jClient 4 版本默认使用 Guzzle6 以保持向后兼容。版本 5 将为您提供选择任何客户端的选项。有关 HTTPlug 的更多信息,请参阅他们的文档。
要配置您的客户端,您可以将其添加到 Configuration
。以下是一个使用 php-http/curl-client
的示例。
use Http\Client\Curl\Client; $options = [ CURLOPT_CONNECTTIMEOUT => 3, // The number of seconds to wait while trying to connect. CURLOPT_SSL_VERIFYPEER => false // Stop cURL from verifying the peer's certificate ]; $httpClient = new Client(null, null, $options); $config = \GraphAware\Neo4j\Client\HttpDriver\Configuration::create($httpClient); $client = ClientBuilder::create() ->addConnection('default', 'http://neo4j:password@localhost:7474', $config) ->build();
许可
此库是根据 MIT 许可证发布的,请参阅与此包捆绑的 LICENSE 文件。