Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use PHP client to operate data through Neo4j's REST interface

Shulou Source: shulou.com Published: 2022-05-31 17:45:22 09月29日 Update

This article will explain in detail how to use the PHP client to manipulate data through Neo4j's REST interface. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Neo4j is one of the most popular graph databases at present, which is based on computer graph structure and is good at dealing with complex data relations. Take, for example, the person-to-person diagram in our common social networks.

How to use PHP client to operate data through Neo4j's REST interface

We need to use the complex SQL statement above to get the list of actors who have performed with KevinBacon.

It's even more troublesome if we need to get a list of actors who have performed with people who have performed with KevinBacon. Imagine one more layer of actors who have performed with (actors who have performed with KevinBacon). It's an almost impossible task.

Let's directly use the PHP client of Neo4j to build the graph:

$client=newClient (newTransport ('localhost',7474))

/ / build actor nodes

$keanu=newNode ($client)

$keanu- > setProperty ('name','KeanuReeves')-> save ()

$laurence=newNode ($client)

$laurence- > setProperty ('name','LaurenceFishburne')-> save ()

$jennifer=newNode ($client)

$jennifer- > setProperty ('name','JenniferConnelly')-> save ()

$kevin=newNode ($client)

$kevin- > setProperty ('name','KevinBacon')-> save ()

How to use PHP client to operate data through Neo4j's REST interface

/ / build a movie node

$matrix=newNode ($client)

$matrix- > setProperty ('title','TheMatrix')-> save ()

$higherLearning=newNode ($client)

$higherLearning- > setProperty ('title','HigherLearning')-> save ()

$mysticRiver=newNode ($client)

$mysticRiver- > setProperty ('title','MysticRiver')-> save ()

/ / establish an association

$keanu- > relateTo ($matrix,'IN')-> save ()

$laurence- > relateTo ($matrix,'IN')-> save ()

$laurence- > relateTo ($higherLearning,'IN')-> save ()

$jennifer- > relateTo ($higherLearning,'IN')-> save ()

$laurence- > relateTo ($mysticRiver,'IN')-> save ()

$kevin- > relateTo ($mysticRiver,'IN')-> save ()

Then we can query the data on the graph structure that has been established. The first query is to find all other people whose distance from KevinBacon is 12 (the distance between actors and actors can only be a multiple of 2, because there is a movie between them).

$path=$keanu- > findPathsTo ($kevin)

-> setMaxDepth (12)

-> getSinglePath ()

Foreach ($pathas$i= > $node) {

If ($I% 2) {

Echo$node- > getProperty ('name')

If ($iSuppli 1 please count ($path)) {

Echo "wasin\ n"

}

} else {

Echo "\ t". $node- > getProperty ('title'). "with\ n"

}

}

You can also query all LaurenceFishburne-related movies with the following statement:

Echo$laurence- > getProperty ('name'). "wasin:\ n"

$relationships=$laurence- > getRelationships ('IN')

Foreach ($relationshipsas$relationship) {

$movie=$relationship- > getEndNode ()

Echo "\ t". $movie- > getProperty ('title'). "\ n"

}

This is the end of the article on "how to use the PHP client to manipulate data through Neo4j's REST interface". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Tags: Data actors performances clients clients interfaces movies articles queries complexity lists databases more actor lists structures nodes statements facets good practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info OPPO Reno Shulou Technology Xiaomi