图形数据库neo4j_使用Neo4j和PHP发现图形数据库

本文介绍了图形数据库Neo4j的核心概念,包括图和图数据库的定义,以及Cypher查询语言。文章强调了图形数据库在处理高度连接数据时的重要性,并通过PHP展示了如何操作Neo4j,包括安装配置、创建关系和查询建议。
摘要由CSDN通过智能技术生成

图形数据库neo4j

In this post, we’ll be learning about Neo4j, the leading graph database, and ways to use it with PHP. In a followup post, we’ll be building a proper graph application powered by Silex.

在本文中,我们将学习领先的图形数据库Neo4j,以及将其与PHP结合使用的方法。 在后续文章中,我们将构建一个由Silex支持的适当的图形应用程序。



Graph databases are now one of the core technologies of companies dealing with highly connected data.

图形数据库现在是处理高度连接数据的公司的核心技术之一。

Business graphs, social graphs, knowledge graphs, interest graphs and media graphs are frequently in the (technology) news – and for a reason. The graph model represents a very flexible way of handling relationships in your data, and graph databases provide fast and efficient storage, retrieval and querying for it.

业务图,社会图,知识图,兴趣图和媒体图经常出现在(技术)新闻中,这是有原因的。 图模型表示一种非常灵活的方式来处理数据中的关系,图数据库提供了快速,有效的存储,检索和查询数据的关系。

Neo4j, the most popular graph database, has proven its ability to deal with massive amounts of highly connected data in many use-cases.

Neo4j是最受欢迎的图形数据库,已证明其具有在许多用例中处理大量高度连接数据的能力。

During the last GraphConnect conference, TomTom and Ebay’s Shuttle demonstrated the value a graph database adds to your company to, for instance, provide fantastic customer experiences or to enable complex route-map editing. Neo4j is developed and supported by Neo Technology – a startup which has grown into a well respected database company.

在上一次GraphConnect会议上 ,TomTom和Ebay的Shuttle演示了图形数据库为您的公司增加的价值,例如,提供出色的客户体验或实现复杂的路线图编辑。 Neo4j由Neo Technology开发和支持,Neo Technology是一家成长为备受尊敬的数据库公司的创业公司。

简短介绍 (A short Introduction)

For the newcomers, here is a short introduction to graph databases and Neo4j, apart from the theoretical glance we threw at it last year.

对于新手,除了我们去年对它的理论了解之外,这里还简要介绍了图形数据库和Neo4j。

什么是图? (What is a Graph ?)

A graph is a generic data structure, composed of of nodes (entities) connected by relationships. Sometimes, those are also called vertices and edges. In the property graph model, each node and relationship can be labeled and hold any number of properties describing it.

图是一种通用的数据结构,由通过关系连接的节点(实体)组成。 有时,这些也称为顶点和边。 在属性图模型中,可以标记每个节点和关系,并保留描述它的任意数量的属性。

what-is-a-graph

image via Wikipedia

图片来自维基百科

什么是图数据库 (What is a Graph Database)

A graph database is a database optimized for operations on connected data. Graph databases provide high performance suitable for online operations by using dedicated storage structures for both nodes and relationships. They don’t need to compute relationships (JOINS) at query time but store them efficiently as part of your data.

图形数据库是针对连接数据进行优化的数据库。 图形数据库通过为节点和关系使用专用的存储结构,提供了适合于在线操作的高性能。 他们不需要在查询时计算关系(JOINS),而是将它们有效地存储为数据的一部分。

Let’s take a simple social application as an example, where users follow other users.

让我们以一个简单的社交应用程序为例,其中用户关注其他用户。

A user will be represented as a Node and can have a label and properties. Labels depict various roles for your nodes.

用户将被表示为节点,并且可以具有标签属性 。 标签描述了节点的各种角色。

A Node

The link between these two users will be represented as a Relationship, which can also have properties and a Type to identify the nature of the relationship. Relationships add semantic meaning to your data.

这两个用户之间的链接将表示为Relationship ,它也可以具有属性和Type来标识关系的性质。 关系为数据增加了语义。

Nodes with Relationship

Looking at the graph shows how natural it is to represent data in a graph and store it in a graph database.

查看图表可以看出在图表中表示数据并将其存储在图表数据库中是多么自然。

MultipleNodesAndEdges

Cypher,Neo4j图形查询语言 (Cypher, the Neo4j Graph Query Language)

Querying a graph may not appear to be straightforward. To make it easy, Neo4j developed Cypher, a declarative graph query language, focused on readability and expressiveness for humans as developers, administrators and domain experts.

查询图形可能看起来并不简单。 为简化起见,Neo4j开发了Cypher (一种声明性图查询语言),专注于开发人员,管理员和领域专家对人类的可读性和表达性。

Being declarative, Cypher focuses on expressing what to retrieve from a graph, rather than how to retrieve it.

声明式的,Cypher专注于表达从图形中检索什么 ,而不是如何检索它。

The query language is comprised of several distinct clauses. You can read more details about them in the Neo4j manual.

查询语言由几个不同的子句组成。 您可以在Neo4j手册中阅读有关它们的更多详细信息。

Here are a few clauses used to read and update the graph:

以下是用于读取和更新图形的一些子句:

  • MATCH: Finds the “example” graph pattern you provide in the graph and returns one path per found match.

    匹配:查找您在图形中提供的“示例”图形模式,并为找到的每个匹配项返回一条路径。
  • WHERE: Filters results with predicates, much like in SQL. There are many more predicates in Cypher though, including collection operations and graph matches.

    哪里:用谓词过滤结果,就像在SQL中一样。 Cypher中还有更多谓词,包括收集操作和图形匹配。
  • RETURN: Returns your query result in the form you need, as scalar values, graph elements or paths, or collections or even documents.

    返回值:以所需的形式,以标量值,图形元素或路径,或者集合甚至文档的形式返回查询结果。
  • CREATE: Creates graph elements (nodes and relationships) with labels and properties.

    创建:创建带有标签和属性的图形元素(节点和关系)。
  • MERGE: Matches existing patterns or create them. It’s a combination of MATCH and CREATE.

    合并:匹配现有模式或创建它们。 它是MATCHCREATE的组合。

Cypher is all about patterns, it describes the visual representation you’ve already seen as textual patterns (using ASCII-art). It uses round parentheses to depict nodes (like (m:Movie) or (me:Person:Developer)) and arrows (like --> or -[:LOVES]->) for relationships.

Cypher完全是关于图案的,它描述了您已经看成是文字图案的视觉表示(使用ASCII艺术)。 它使用圆括号来描述关系的节点(如(m:Movie)(me:Person:Developer) )和箭头(如-->-[:LOVES]-> )。

Looking at our last graph of users, a query that will retrieve Hannah Hilpert and the users following her will be written like the following :

查看我们的最后一个用户图,查询将检索Hannah Hilpert及其后的用户,如下所示:

MATCH (user:User {name:'Hannah Hilpert'})<-[:FOLLOWS]-(follower) 
RETURN user, follower
HannahFriendships


Neo4j和PHP (Neo4j and PHP)

After this quick introduction to the Neo4j graph database (more here), let’s see how we can use it from PHP.

在对Neo4j图形数据库进行了快速介绍之后( 更多信息在这里 ),让我们看看如何从PHP中使用它。

Neo4j is installed as a database server. An HTTP-API is accessible for manipulating the database and issuing Cypher queries.

Neo4j被安装为数据库服务器。 可访问HTTP-API来处理数据库和发出Cypher查询。

If you want to install and run the Neo4j graph database, you can download the latest version here : http://neo4j.com/download/, extract the archive on your computer and run the ./bin/neo4j start command. Note that this is only for *nix based systems.

如果要安装和运行Neo4j图形数据库,可以在此处下载最新版本: http : ./bin/neo4j start ,在计算机上解压缩档案并运行./bin/neo4j start命令。 请注意,这仅适用于基于* nix的系统。

Neo4j comes with a cool visual interface, the Neo4j Browser available at http://localhost:7474. Just try it! There are some guides to get started within the browser, but more information can be found online.

Neo4j带有一个很酷的可视界面, Neo4j浏览器可从http:// localhost:7474获得 。 去尝试一下! 浏览器中有一些入门指南,但可以在线找到更多信息。

If you don’t want to install it on your machine, you can always create a free instance on GrapheneDB, a Neo4j As A Service provider.

如果您不想在计算机上安装它,则始终可以在GrapheneDB上创建一个免费实例,Neo4j即服务提供程序。

氧成分 (The Neoxygen Components)

Neoxygen is a set of open-source components, most of them in PHP, for the Neo4j ecosystem available on Github. Currently, I’m the main developer. If you are interested in contributing as well, just ping me.

NeoxygenGithub上的Neo4j生态系统的一组开源组件,其中大多数都是PHP的。 目前,我是主要开发人员。 如果您也有贡献,请ping me

A powerful Client for the Neo4j HTTP-API is named NeoClient, with multi-database support and built-in high availabililty management.

Neo4j HTTP-API的功能强大的客户端称为NeoClient ,具有多数据库支持和内置的高可用性管理。

安装与配置 (Installation and configuration)

The installation is trivial, just add the neoclient dependency in your composer.json file :

安装非常简单,只需在您的composer.json文件中添加neoclient依赖项:

{
  "require": {
    "neoxygen/neoclient":"~2.1"
  }
}

You configure your connection when building the client :

您在构建客户端时配置连接:

use Neoxygen\NeoClient\ClientBuilder;

$client = ClientBuilder::create()
  ->addConnection('default', 'http', 'localhost', 7474)
  ->build();

If you created an instance on GrapheneDB, you need to configure a secure connection with credentials. This is done by appending true for using the auth mode and your credentials to the addConnection method :

如果在GrapheneDB上创建了实例,则需要使用凭据配置安全连接。 这是通过将true (使用auth模式)和您的凭据附加到addConnection方法中来完成的:

<?php

use Neoxygen\NeoClient\ClientBuilder;

$connUrl = parse_url('http://master.sb02.stations.graphenedb.com:24789/db/data/');
$user = 'master';
$pwd = 's3cr3tP@ssw0rd';

$client = ClientBuilder::create()
  ->addConnection('default', $connUrl['scheme'], $connUrl['host'], $connUrl['port'], true, $user, $password)
  ->build();

You have now full access to your Neo4j database with the client connecting to the HTTP API.

现在,通过客户端连接到HTTP API,您可以完全访问Neo4j数据库。

The library provides handy methods to access the different endpoints. However, the most frequently used method is sending a Cypher query.

该库提供了方便的方法来访问不同的端点。 但是,最常用的方法是发送Cypher查询

Handling graph results in a raw json response is a bit cumbersome. That’s why the library comes with a handy result formatter that transforms the response into node and relationship objects. The formatter is disabled by default, and you can enable it by just adding a line of code into your client building process :

处理图导致原始的json响应有点麻烦。 这就是为什么该库带有一个方便的结果格式化程序,该结果格式化程序将响应转换为节点和关系对象的原因。 格式化程序默认情况下处于禁用状态,您可以通过在客户端构建过程中添加一行代码来启用它:

$client = ClientBuilder::create()
  ->addConnection('default', 'http', 'localhost', 7474)
  ->setAutoFormatResponse(true)
  ->build();

让我们建立一些很棒的东西 (Let’s build something cool)

We’re going to build a set of User nodes and FOLLOWS relationships incrementally. Then, we’ll be able to query friend-of-a-friend information to provide friendship suggestions.

我们将逐步构建一组User节点和FOLLOWS关系。 然后,我们将能够查询“ 朋友的朋友”信息以提供友谊建议。

The query to create a User is the following :

创建用户的查询如下:

CREATE (user:User {name:'Kenneth'}) RETURN user

The query is composed of 5 parts :

查询由5部分组成:

query-anatomy
  • The CREATE clause (in blue), indicating we want to create a new element.

    CREATE子句(蓝色),表明我们要创建一个新元素。
  • The identifier (in orange), used to identify your node in the query

    标识符(橙色),用于标识查询中的节点
  • The label (in red), used to add the user to the User labelled group.

    标签(红色),用于将用户添加到“ User标签”组。

  • The node properties (in green), are specific to that node.

    节点属性(绿色)特定于该节点。
  • The RETURN clause, indicating what you want to return, here the created user.

    RETURN子句,指示要返回的内容,此处为创建的用户。

You can also try to run that query in the Neo4j Browser.

您也可以尝试在Neo4j浏览器中运行该查询。

No need to wait, let’s create this user with the client :

无需等待,让我们用客户端创建该用户:

$query = 'CREATE (user:User {name:"Kenneth"}) RETURN user';
$result = $client->sendCypherQuery($query)->getResult();

You can visualize the created node in your browser (open the starred tab and run “Get some data”), or get the graph result with the client.

您可以在浏览器中可视化创建的节点(打开加星标的选项卡并运行“获取一些数据”),或通过客户端获取图形结果。

$user = $result->getSingleNode();
$name = $user->getProperty('name');

We will do the same for another user, now with query parameters. Query parameters are passed along with the query and it allows Neo4j to cache the query execution plan, which will make your further identical queries faster :

现在,我们将使用查询参数为另一个用户执行相同的操作。 查询参数与查询一起传递,它使Neo4j可以缓存查询执行计划,这将使您进一步进行相同的查询更快:

$query = 'CREATE (user:User {name: {name} }) RETURN user';
$parameters = array('name' => 'Maxime');
$client->sendCypherQuery($query, $parameters);

As you can see, parameters are embedded in {}, and passed in an array of parameters as second argument of the sendCypherQuery method.

如您所见,参数嵌入在{} ,并作为sendCypherQuery方法的第二个参数传递给参数数组。

If you look at the graph now, you’ll see the two User nodes, but they feel quite alone :( , no ?

如果现在看图表,您将看到两个User节点,但是它们感觉很孤独:(,不?

Imgur
建立关系 (Creating relationships)

In order to create the relationships between our nodes, we’ll use Cypher again.

为了创建节点之间的关系,我们将再次使用Cypher。

$query = 'MATCH (user1:User {name:{name1}}), (user2:User {name:{name2}}) CREATE (user1)-[:FOLLOWS]->(user2)';
$params = ['user1' => 'Kenneth', 'user2' => 'Maxime'];
$client->sendCypherQuery($query, $params);

Some explanations :

一些解释:

We first match for existing users named Kenneth and Maxime (names provided as parameters), and then we create a FOLLOWS relationship between the two.

我们首先匹配名为Kenneth和Maxime的现有用户(名称作为参数提供),然后在两者之间创建FOLLOWS关系。

Kenneth will be the start node of the FOLLOWS relationship and Maxime the end node. The relationship type will be FOLLOWS.

肯尼斯(Kenneth)将是FOLLOWS关系的开始节点 ,并FOLLOWS 结束节点 。 关系类型将为FOLLOWS

Looking at the graph again shows that the relationship has been created.

再次查看该图,表明关系已创建。

Imgur
创建一群用户 (Creating a bunch of users)

Manually writing all the creation statements for a set of 100 users and the relationships would be boring. I want to introduce a very useful tool called Graphgen (one of the Neoxygen components) for generating graph data with ease.

手动为一组100个用户编写所有创建语句,这种关系很无聊。 我想介绍一个非常有用的工具,称为Graphgen (Neoxygen组件之一),可轻松生成图形数据。

It uses a specification that is very close to Cypher to describe the graph you want. Here we’re going to create a set of 50 users and the corresponding FOLLOWS relationships.

它使用非常接近Cypher的规范来描述所需的图形。 在这里,我们将创建一组50个用户以及相应的FOLLOWS关系。

Go to http://graphgen.neoxygen.io , copy and paste the following pattern in the editor area, and click on Generate :

转到http://graphgen.neoxygen.io ,将以下模式复制并粘贴到编辑器区域,然后单击Generate

(user:User {login: userName, firstname: firstName, lastname: lastName} *50)-[:FOLLOWS *n..n]->(user)
Imgur

You can see that it automatically generates a graph with 50 users, the relationships, and realistic values for login, firstname and lastname. Impressive, no?

您会看到它会自动生成一个包含50个用户,关系以及登录名,名字和姓氏的实际值的图形。 印象深刻,不是吗?

Let’s import this graph into our local graph database, click on Populate your database and use the default settings.

让我们将此图导入到本地图数据库中,单击“ 填充数据库”并使用默认设置。

Imgur

In no time, the database will be populated with the data.

很快,数据库中就会填充数据。

If you open the Neo4j browser, and run “Get some data” again, you can see all the user nodes and their relationships.

如果打开Neo4j浏览器,然后再次运行“获取一些数据”,则可以看到所有用户节点及其关系。

Imgur
获得建议 (Getting suggestions)

Getting suggestions with Neo4j is simple, you just need to match one user, follow the FOLLOWS relationships to the other users, then for each found user, find the users they follow and return those that you do not follow already. The suggestion also must not be the user for whom we are looking for suggestions.

使用Neo4j获得建议很简单,您只需要匹配一个用户,将FOLLOWS关系与其他用户联系,然后为每个找到的用户找到他们关注的用户并返回您尚未关注的用户。 该建议也一定不能是我们正在寻找建议的用户。

In a common application, there will be a login system and the user will be only allowed to see the users he is following. For the sake of this post which is introducing you Neo4j, you’ll be able to play with all the users.

在一个常见的应用程序中,将有一个登录系统,并且只允许该用户看到他关注的用户。 为了使这篇文章向您介绍Neo4j,您将可以与所有用户一起玩。

Let’s write it in Cypher :

让我们用Cypher编写它:

$query = 'MATCH (user:User {firstname: {firstname}})-[:FOLLOWS]->(followed)-[:FOLLOWS]->(suggestion)
WHERE user <> suggestion 
  AND NOT (user)-[:FOLLOWS]->(suggestion)
RETURN user, suggestion, count(*) as occurrence
ORDER BY occurrence DESC
LIMIT 10';
$params = ['firstname' => 'Francisco'];
$result = $client->sendCypherQuery($query, $params)->getResult();

$suggestions = $result->get('suggestion'); // Returns a set of nodes

If you run this query in the neo4j browser, you’ll get your first matched user and the suggestions :

如果在neo4j浏览器中运行此查询,您将获得第一个匹配的用户和建议:

Imgur

结论 (Conclusion)

In this part:

在这一部分:

  • You’ve discovered graph databases and Neo4j

    您已经找到了图形数据库和Neo4j
  • You learned the basics of the Cypher Query Language

    您学习了Cypher查询语言的基础知识
  • You’ve seen how to connect to and run queries on a Neo4j database with PHP

    您已经了解了如何使用PHP连接和运行Neo4j数据库上的查询

In a followup article we’ll use everything we’ve learned so far and make a real Neo4j powere Silex PHP application.

在后续文章中,我们将使用到目前为止所学的知识,并制作一个真正的Neo4j powere Silex PHP应用程序。

翻译自: https://www.sitepoint.com/discover-graph-databases-neo4j-php/

图形数据库neo4j

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值