jpbourbon/redisgraph_php
Composer 安装命令:
composer require jpbourbon/redisgraph_php
包简介
RedisGraph php client
关键字:
README 文档
README
A client for RedisGraph aiming to track its development and implementation of OpenCypher. The goal of this library is providing a standard way of sending queries to the RedisGraph engine and encapsulate the results in a clean object.
Installation
composer require jpbourbon/redisgraph_php:"0.5.8"
Usage
There are 2 ways of using this library.
- Using the Client, an instantiable class;
- Using RedisGraph, a static wrapper that loads the Client as a singleton;
Connection
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);
Queries
This library supports only raw queries. However, we encapsulate the query in a Cypher class that can be used for validation in future iterations. This class also provides tagging and runtime changing of graphs.
...
use RedisGraphPha\Cypher;
...
$cypher = new Cypher("MATCH (f:Foo) RETURN f", "MyTast");
Running queries
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);
Results
The response from the run method will return an instance of the Result class. This class consists of a structure that holds an array of query return keys, and array of RecordSets with Records*, the Cypher object and the Statistics object.
Return keys
A Cypher query that contains a RETURN clause will always return a key:value pair, except when the query generates no returnable values. Consider the following Cypher query:
MATCH (f:Foo) RETURN f
This returns the key f. To obtain an array with the returned keys, use the getKeys method:
$result->getKeys();
Getting the RecordSet and Records
The *Result and RecordSet objects provide methods to lookup their child classes.
$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
Types of Records
The Records can be either Nodes, Relationships or Scalar values. Each of them has its own characteristics, reflected in the returned object. The distinct types of record can be easily identified using the getRecordType method:
$result->firstRecordSet()->firstRecord()->getRecordType(); // Returns the record type as a string
Scalar
Scalar values are single values. Those can be either the result of an internal cypher function (such as SUM(), COUNT() etc) or the value from a node or relationship property.
MATCH (n) RETURN COUNT(n) AS total // "total" is a scalar value
MATCH (n) RETURN n.key // "n.key" is a scalar value
To access the value you would first need to know the key, and use the getValue() method on the record:
$result->firstRecordSet()->getRecord("total")->getValue(); // Returns the single value
However, as we often need to get a list of scalar values, there is a special method for that:
$result->getAllScalar("n.key"); // Returns an array of all values from all recordsets
Nodes and Relationships
Nodes and Relationships share some common methods, since they can both hold properties, but have their specific ones. Common
$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
Nodes
$result->firstRecordSet()->firstRecord()->getLabels(); // Gets the array of labels
Relationship
$result->firstRecordSet()->firstRecord()->getType(); // Returns the type as a string
$result->firstRecordSet()->firstRecord()->getLinkedNodes(); // Gets an array with the source and target node ids
Graphs
RedisGraph supports multiple graphs running alongside. This means the client must be flexible and support the different graphs. This client allows the definition of the graph in 3 different places: 1 - connection options; 2 - cypher object; 3 - chained method before running the query; The graph defined in the connection options is the default graph, and acts as a fallback in case no graph is defined anywhere else. The graph defined on the cypher query or the chained method is always considered secondary, and must be explicitely set to be used.
1 - Connection options
simply define the graph in the array:
$options = [
"host" => "127.0.0.1",
"port" => "6379",
"graph" => "test"
];
2 - Cypher query
The graph is an optional argument when creating the Cypher object. If defined, overrides the default graph for the query execution.
$cypher = new Cypher("MATCH (f:Foo) RETURN f", "MyTast", "myOtherGraph);
3 - Chained method
This option is useful if the user intends to use the same Cypher object for different graphs, as it overrides both the default graph and the Cypher object graph:
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 graphs
Graphs can be deleted with a simple delete method:
Client
...
$result = $client)->delete("myGraph");
RedisGraph
...
$result = RedisGraph::delete("myGraph");
Explain queries
Both the RedisGraph and the Client classes feature an explain method that returns the query analysis:
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);
TBC...
jpbourbon/redisgraph_php 适用场景与选型建议
jpbourbon/redisgraph_php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 37 次下载、GitHub Stars 达 5, 最近一次更新时间为 2019 年 11 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「graph」 「cypher」 「redisgraph」 「tphp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jpbourbon/redisgraph_php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jpbourbon/redisgraph_php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jpbourbon/redisgraph_php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony integration for Neo4j
Netgen Open Graph Bundle is an Ibexa Platform bundle that allows simple integration with Open Graph protocol.
A tool for scraping URL resources (oEmbed, OpenGraph, Twitter cards, JSON-LD)
PHP Object Graph Mapper for Neo4j
PHP Object Graph Mapper for Neo4j
Neo4j-PHP-Client is the most advanced PHP Client for Neo4j
统计信息
- 总下载量: 37
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-07