neo4j的php客户端neoclient连接和使用

neo4j的php客户端neoclient连接和使用


1.下载安装

neo4j安装 neo4j 2.3.0M02 请见:http://blog.csdn.net/xubo245/article/details/50000967

neoclient的下载:https://github.com/neoxygen/neo4j-neoclient

安装:

composer require neoxygen/neoclient
需要提前安装好composer,请见:http://blog.163.com/wang_7380031@126/blog/static/55905870201503024412757/


2.运行:

将src下面的所有文件copy到/tests/Neoxygen/NeoClient下,

在test下新建1.php,写入:

<?php
echo "hello php neo4j 1</br>";
require_once 'vendor/autoload.php';

use Neoxygen\NeoClient\ClientBuilder;

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

测试是否连接上:

$version = $client->getNeo4jVersion();
echo $version;

返回结果:

2.3.0-M02

说明已经连接上


3.使用:

(1)创建节点并返回:

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

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

返回结果:

Kenneth


(2)查找

$query = 'match n return n';
$result = $client->sendCypherQuery($query)->getResult();

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

返回结果:

Kenneth


(3)创建relationship:

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

查看结果((1)创建了两个):


更多请见:http://www.sitepoint.com/discover-graph-databases-neo4j-php/?utm_source=tuicool&utm_medium=referral




【1】http://blog.csdn.net/xubo245/article/details/50000967

【2】http://blog.163.com/wang_7380031@126/blog/static/55905870201503024412757/

【3】http://www.sitepoint.com/discover-graph-databases-neo4j-php/?utm_source=tuicool&utm_medium=referral



<div class="post-text" itemprop="text"> <p>i know this topic is Repetitious but I cant do it... ir run this in my cmd</p> <p>C:\Users\Asus> composer require graphaware/neo4j-php-client</p> <p>and i use this code:</p> <pre><code> <?php /** * To install Neo4j-PHP-Client, we use Composer * * $ curl -sS https://getcomposer.org/installer | php * $ php composer.phar require graphaware/neo4j-php-client * */ require 'C:\Users\Asus\vendor\autoload.php'; use GraphAware\Neo4j\Client\ClientBuilder; // change to your hostname, port, username, password $neo4j_url = "neo4j@bolt://localhost:11004"; // setup connection $client = ClientBuilder::create() ->addConnection('default', $neo4j_url) ->build(); // setup data $insert_query = <<<EOQ UNWIND {pairs} as pair MERGE (p1:Person {name:pair[0]}) MERGE (p2:Person {name:pair[1]}) MERGE (p1)-[:KNOWS]-(p2); EOQ; // friend data to insert $data = [["Jim","Mike"],["Jim","Billy"],["Anna","Jim"], ["Anna","Mike"],["Sally","Anna"],["Joe","Sally"], ["Joe","Bob"],["Bob","Sally"]]; // insert data $client->run($insert_query, ["pairs" => $data]); // friend of friend: query $foaf_query = <<<EOQ MATCH (person:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf) WHERE person.name = {name} AND NOT (person)-[:KNOWS]-(foaf) RETURN foaf.name AS name EOQ; // friend of friend: build and execute query $params = ['name' => 'Joe']; $result = $client->run($foaf_query, $params); foreach ($result->records() as $record) { echo $record->get('name') . PHP_EOL; } // common friends: query $common_friends_query = <<<EOQ MATCH (user:Person)-[:KNOWS]-(friend)-[:KNOWS]-(foaf:Person) WHERE user.name = {user} AND foaf.name = {foaf} RETURN friend.name AS friend EOQ; // common friends: build and execute query $params = ['user' => 'Joe', 'foaf' => 'Sally']; $result = $client->run($common_friends_query, $params); foreach ($result->records() as $record) { echo $record->get('friend') . PHP_EOL; } // connecting paths: query $connecting_paths_query = <<<EOQ MATCH path = shortestPath((p1:Person)-[:KNOWS*..6]-(p2:Person)) WHERE p1.name = {name1} AND p2.name = {name2} RETURN [n IN nodes(path) | n.name] as names EOQ; // connecting paths: build and execute query $params = ['name1' => 'Joe', 'name2' => 'Billy']; $result = $client->run($connecting_paths_query, $params); foreach ($result->records() as $record) { print_r($record->get('names')); } </code></pre> <p>and i have this error Notice: Undefined index: scheme in C:\Users\Asus\vendor\graphaware eo4j-php-client\src\Connection\Connection.php on line 77</p> <p>Notice: Undefined index: host in C:\Users\Asus\vendor\graphaware eo4j-php-client\src\Connection\Connection.php on line 77</p> <p>Warning: stream_socket_client(): unable to connect to tcp://://:7687:7687 (The requested address is not valid in its context. ) in C:\Users\Asus\vendor\graphaware eo4j-bolt\src\IO\StreamSocket.php on line 167</p> <p>Fatal error: Uncaught exception 'GraphAware\Bolt\Exception\IOException' with message 'Error to connect to the server(10049) : "The requested address is not valid in its context. "' in C:\Users\Asus\vendor\graphaware eo4j-bolt\src\IO\StreamSocket.php:170 Stack trace: #0 C:\Users\Asus\vendor\graphaware eo4j-bolt\src\IO\StreamSocket.php(189): GraphAware\Bolt\IO\StreamSocket->connect() #1 C:\Users\Asus\vendor\graphaware eo4j-bolt\src\Driver.php(114): GraphAware\Bolt\IO\StreamSocket->reconnect() #2 C:\Users\Asus\vendor\graphaware eo4j-bolt\src\Driver.php(98): GraphAware\Bolt\Driver->handshake() #3 C:\Users\Asus\vendor\graphaware eo4j-php-client\src\Connection\Connection.php(164): GraphAware\Bolt\Driver->session() #4 C:\Users\Asus\vendor\graphaware eo4j-php-client\src\Connection\Connection.php(115): GraphAware\Neo4j\Client\Connection\Connection->checkSession() #5 C:\Users\Asus\vendor\graphaware eo4j-php-client\src\Client.php(45): GraphAware\Neo4j\Client\Connection\Connection->run('UNWIND {pairs} ...', Array, NULL) #6 C in C:\Users\Asus\vendor\graphaware eo4j-bolt\src\IO\StreamSocket.php on line 170</p> </div>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页