How to use Neo4j in Workerman for graph database storage and query
Overview:
Workerman is a high-performance PHP asynchronous network programming framework, and Neo4j is A powerful graph database. This article will introduce how to use Neo4j in Workerman to store and query graph databases, and provide specific code examples.
Step 1: Install the Neo4j extension
composer require graphaware/neo4j-php-client
extension=neo4j.so
Step 2: Connect to the graph database
In Workerman’s event callback function, use the following code to connect to the Neo4j database:
use GraphAwareBoltGraphDatabase; $driver = GraphDatabase::driver("bolt://localhost", GraphAwareBoltConfiguration::fromArray([ 'username' => 'neo4j', 'password' => 'password', ]));
Among them, bolt://localhost
is the connection address of the Neo4j database, neo4j
is the user name, and password
is the password. Modify these parameters according to actual conditions.
Step 3: Create a node
Use the following code example to create a node:
$session = $driver->session(); $session->run("CREATE (n:Person {name: 'John Doe', age: 30})");
This code will create a label Be the node of "Person", and set the name attribute to "John Doe" and the age attribute to 30.
Step 4: Query nodes
Use the following code example to query all nodes named "John Doe":
$session = $driver->session(); $result = $session->run("MATCH (n:Person {name: 'John Doe'}) RETURN n"); foreach ($result->getRecords() as $record) { $node = $record->get('n'); // 处理节点数据 echo $node->value('name'); echo $node->value('age'); }
This code will execute a Cypher query, find the node named "John Doe" in the node's attributes, and return the result set. Then, iterate through the result set and process the data of each node.
Step 5: Close the connection
In the appropriate location of Workerman's event callback function, use the following code to close the database connection:
$driver->close();
The above are the basic steps for using Neo4j to store and query graph databases in Workerman. Hope this article is helpful to you. If you have any questions, please feel free to ask.
The above is the detailed content of How to use Neo4j for graph database storage and query in Workerman. For more information, please follow other related articles on the PHP Chinese website!