anshu-krishna / neo4j-bolt
PHP用于与Neo4j图数据库通过TCP通信的Bolt协议的驱动程序
2.0
2022-09-12 08:17 UTC
Requires
- php: >=8.1
- anshu-krishna/neo4j-pack-stream: ^1.0
README
PHP用于与Neo4j图数据库通过TCP通信的Bolt协议的驱动程序
安装
composer require anshu-krishna/neo4j-bolt
需求
- PHP >= 8.1
- Neo4j(带Bolt版本4.4、4.3、4.2或4.1)
注意:所有示例都使用Neo4j的“示例电影数据库”
示例
require_once "vendor/autoload.php"; use Krishna\Neo4j\{AuthToken, BoltMaker}; use Krishna\Neo4j\Protocol\Reply\Success; // Create Bolt $bolt = (new BoltMaker( auth: AuthToken::basic('neo4j', 'neo4j') ))->makeBolt(); echo 'Running Bolt version:', $bolt::VERSION, '<br>'; // Run Query $run = $bolt->query(<<<CYPHER match (p:Person)-[a:ACTED_IN]->(m:Movie) return p as person, a.roles as roles, {title: m.title, year: m.released} as movie limit 2 CYPHER); if(!$run instanceof Success) { echo 'Query Error: ', $run->message; exit(0); } echo <<<HTML <table border="1"> <tr> <th>Person</th> <th>Born</th> <th>Role(s)</th> <th>Movie</th> <th>Released</th> </tr> HTML; // Pull results foreach($bolt->pull() as $i) { $roles = implode(', ', $i->roles); echo <<<"ROW" <tr> <td>{$i->person->properties['name']}</td> <td>{$i->person->properties['born']}</td> <td>{$roles}</td> <td>{$i->movie['title']}</td> <td>{$i->movie['year']}</td> </tr> ROW; } echo '</table>';
对于特定的BOLT版本
在BoltMaker
中使用useVersion()
来按优先级设置最多4个版本。
$bolt = (new BoltMaker(auth: AuthToken::basic('neo4j', 'neo4j'))) ->useVersion(E_Version::V4_2, E_Version::V4_1) ->makeBolt(); var_dump($bolt::VERSION);
调试日志
在BoltMaker
中传递一个Logger
对象以获取协议调试日志。
$bolt = (new BoltMaker( auth: AuthToken::basic('neo4j', 'neo4j'), logger: new Logger(rowSize: 20) ))->makeBolt();
输出
Write [Handshake]:
60 60 b0 17 00 00 04 04 00 00 03 04 00 00 02 04 00 00 01 04
Read [Handshake]:
00 00 04 04
Write [Hello]:
00 01 b1 00 01 01 a4 8a 75 73 65 72 5f 61 67 65 6e 74 89 4b
42 6f 6c 74 2f 31 2e 30 86 73 63 68 65 6d 65 85 62 61 73 69
63 89 70 72 69 6e 63 69 70 61 6c 85 6e 65 6f 34 6a 8b 63 72
65 64 65 6e 74 69 61 6c 73 85 6e 65 6f 34 6a 00 00
Read [Hello = Success]:
b1 70 a3 86 73 65 72 76 65 72 8b 4e 65 6f 34 6a 2f 34 2e 34
2e 35 8d 63 6f 6e 6e 65 63 74 69 6f 6e 5f 69 64 87 62 6f 6c
74 2d 31 38 85 68 69 6e 74 73 a0
Write [Goodbye]:
00 01 b0 00 01 02 00 00
在创建Bolt
协议后开始记录日志
$bolt = (new BoltMaker( auth: AuthToken::basic('neo4j', 'neo4j') ))->makeBolt(); // Start logging $bolt->logger = new Logger; // Execute quries here // Stop logging $bolt->logger = null;
协议函数
-
运行查询
function query( string $query, array $parameters = [], bool $autoResetOnFaiure = true, array $bookmarks = [], int $tx_timeout = -1, ?array $tx_metadata = null, bool $readMode = false, ?string $db = null ): I_Reply;
-
拉取结果
function pull(int $count = -1, int $qid = -1); /* qid is query id */
-
断开连接
function disconnect(): void;
-
重置连接
function reset(): I_Reply;
-
开始事务
function beginTransaction( array $bookmarks = [], int $tx_timeout = -1, ?array $tx_metadata = null, bool $readMode = false, ?string $db = null ): I_Reply;
-
提交事务
function commit(): I_Reply;
-
回滚事务
function rollback(): I_Reply;
-
获取最后一个查询元数据
function getQueryMeta(): ?I_Reply;
-
检查最后一个查询是否有效
function queryValid(): bool;
-
检查最后一个查询是否有更多结果
function moreResults(): bool;
-
丢弃结果
function discard(int $count = -1, int $qid = -1): ?I_Reply; /* qid is query id */
仅在Bolt
>= 4.3中
- 发送路由消息
function route(array $routing, array $bookmarks, ?string $db = null): I_Reply;