jpbourbon/redisgraph_php

RedisGraph php 客户端

v0.5.8 2019-11-10 18:28 UTC

This package is auto-updated.

Last update: 2024-09-12 09:23:53 UTC


README

这是一个针对RedisGraph的客户端,旨在跟踪其OpenCypher的开发和实现。这个库的目标是提供一个将查询发送到RedisGraph引擎的标准方式,并将结果封装在一个干净的对象中。

安装

composer require jpbourbon/redisgraph_php:"0.5.8"

用法

使用此库有两种方式。

  • 使用 Client,一个可实例化的类;
  • 使用 RedisGraph,一个静态包装器,将Client作为单例加载;

连接

Client

...
use RedisGraphPhp\Client;
...
$options = [
    "host"  => "127.0.0.1",
    "port"  => "6379",
    "graph" => "test"
];
$client = new Client($options);

RedisGraph

...
use RedisGraphPhp\RedisGraph;
...
$options = [
    "host"  => "127.0.0.1",
    "port"  => "6379",
    "graph" => "test"
];
RedisGraph::setOptions($options);

查询

此库仅支持原始查询。然而,我们将查询封装在 Cypher 类中,该类可以在未来的迭代中进行验证。此类还提供了标记和图形的运行时更改。

...
use RedisGraphPha\Cypher;
...
$cypher = new Cypher("MATCH (f:Foo) RETURN f", "MyTast");

运行查询

Client

...
use RedisGraphPhp\Client;
use RedisGraphPha\Cypher;
...
$options = [
    "host"  => "127.0.0.1",
    "port"  => "6379",
    "graph" => "test"
];
$client = new Client($options);
$cypher = new Cypher("MATCH (f:Foo) RETURN f", "MyTast");
$result = $client->run($cypher);

RedisGraph

...
use RedisGraphPhp\RedisGraph;
use RedisGraphPha\Cypher;
...
$options = [
    "host"  => "127.0.0.1",
    "port"  => "6379",
    "graph" => "test"
];
RedisGraph::setOptions($options);
$cypher = new Cypher("MATCH (f:Foo) RETURN f", "MyTast");
$result = RedisGraph::run($cypher);

结果

run 方法返回的响应将返回一个 Result 类的实例。此类包含一个结构,该结构包含一个查询返回的 keys 数组,以及包含 RecordsRecordSets 数组*,Cypher 对象和 Statistics 对象。

返回键

包含 RETURN 子句的Cypher查询将始终返回一个键值对,除非查询没有可返回的值。考虑以下Cypher查询

MATCH (f:Foo) RETURN f

这返回了键 f。要获取返回键的数组,请使用 getKeys 方法

$result->getKeys();

获取RecordSet和Records

ResultRecordSet 对象提供方法来查找它们的子类。

$result->getRecordSets(); // Returns all RecordSet objects returnes (rows)
$result->getRecordSet(N); // Returns the RecordSet at position N or null
$result->firstRecordSet(); // Returns the first RecordSet or null
$result->size(); // Returns the size of the recordSets array

$result->firstRecordSet()->getRecords(); // Returns all Records from the first RecordSet
$result->firstRecordSet()->getRecord(N); // Returns the Record at position N or null
$result->firstRecordSet()->firstRecord(); // Returns the first Records from the RecordSet

记录类型

Records 可以是 NodesRelationshipsScalar values。每个都有其自己的特性,反映在返回的对象中。可以使用 getRecordType 方法轻松识别不同的记录类型

$result->firstRecordSet()->firstRecord()->getRecordType(); // Returns the record type as a string

标量

标量值是单个值。这些可以是内部Cypher函数的结果(如 SUM(), COUNT() 等)或节点或关系的属性值。

MATCH (n) RETURN COUNT(n) AS total // "total" is a scalar value
MATCH (n) RETURN n.key // "n.key" is a scalar value

要访问值,您首先需要知道键,然后在记录上使用 getValue() 方法

$result->firstRecordSet()->getRecord("total")->getValue(); // Returns the single value

但是,由于我们经常需要获取标量值的列表,因此有一个特殊的方法可以做到这一点

$result->getAllScalar("n.key"); // Returns an array of all values from all recordsets

节点和关系

NodesRelationships 具有一些共同的方法,因为它们都可以持有属性,但它们有特定的方法。共同的方法

$result->firstRecordSet()->firstRecord()->getId(); // returns the internal id of the record
$result->firstRecordSet()->firstRecord()-getProperties(); // Gets an array with existing properties;
$result->firstRecordSet()->firstRecord()->getValue($property); // Gets the value for the given property name

节点

$result->firstRecordSet()->firstRecord()->getLabels(); // Gets the array of labels

关系

$result->firstRecordSet()->firstRecord()->getType(); // Returns the type as a string
$result->firstRecordSet()->firstRecord()->getLinkedNodes(); // Gets an array with the source and target node ids

图形

RedisGraph 支持多个图形同时运行。这意味着客户端必须灵活,支持不同的图形。此客户端允许在3个不同的地方定义图形:1 - 连接选项;2 - Cypher对象;3 - 在运行查询之前的链式方法;连接选项中定义的图形是默认图形,并在其他任何地方未定义图形时作为后备。在Cypher查询或链式方法上定义的图形始终被视为次要的,并且必须明确设置才能使用。

1 - 连接选项

只需在数组中简单定义图形

 $options = [
    "host"  => "127.0.0.1",
    "port"  => "6379",
    "graph" => "test"
];

2 - Cypher查询

图形是创建Cypher对象时的可选参数。如果定义了,将覆盖查询执行时的默认图形。

$cypher = new Cypher("MATCH (f:Foo) RETURN f", "MyTast", "myOtherGraph);

3 - 链式方法

如果用户打算为不同的图使用相同的Cypher对象,此选项很有用,因为它会覆盖默认图和Cypher对象图。

Client
...
$cypher = new Cypher("MATCH (f:Foo) RETURN f", "MyTast", "myOtherGraph");
$result = $client->graph("myAlternativeGraph")->run($cypher);
RedisGraph
...
$cypher = new Cypher("MATCH (f:Foo) RETURN f", "MyTast", "myOtherGraph");
$result = RedisGraph::graph("myAlternativeGraph")::run($cypher);

删除图

可以使用简单的delete方法删除图。

Client
...
$result = $client)->delete("myGraph");
RedisGraph
...
$result = RedisGraph::delete("myGraph");

解释查询

RedisGraph类和Client类都提供了一个explain方法,该方法返回查询分析。

Client

...
use RedisGraphPhp\Client;
use RedisGraphPha\Cypher;
...
$options = [
    "host"  => "127.0.0.1",
    "port"  => "6379",
    "graph" => "test"
];
$client = new Client($options);
$cypher = new Cypher("MATCH (f:Foo) RETURN f", "MyTast");
$result = $client->explain($cypher);

RedisGraph

...
use RedisGraphPhp\RedisGraph;
use RedisGraphPha\Cypher;
...
$options = [
    "host"  => "127.0.0.1",
    "port"  => "6379",
    "graph" => "test"
];
RedisGraph::setOptions($options);
$cypher = new Cypher("MATCH (f:Foo) RETURN f", "MyTast");
$result = RedisGraph::explain($cypher);

待补充...