Neo4j

Neo4j - 入门学习

2019-01-08  本文已影响68人  红薯爱帅

1. 简介

Neo4j is a highly scalable, robust native graph database.


Neo4j Sample Datasets

2. 启动测试

启动neo4j

docker run \
    --publish=7474:7474 --publish=7687:7687 \
    --volume=$HOME/neo4j/data:/data \
    neo4j

测试

地址:http://localhost:7474
账号:neo4j
密码:neo4j
点击Jump into code,完成入门学习

hello neo4j

3. Neo4j Desktop

一个Neo4j Instance,在同一时间只能激活一个Graph,不过,我们可以启动多个Instance。
通过Neo4j Desktop可以创建Local Instance,也可以管理多个Instance,包含Local和Remote。

4. Neo4j Client

$ sudo add-apt-repository ppa:cleishm/neo4j
$ sudo apt-get update
$ sudo apt-get install neo4j-client libneo4j-client-dev
#include <neo4j-client.h>
#include <errno.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
    neo4j_client_init();

    /* use NEO4J_INSECURE when connecting to disable TLS */
    neo4j_connection_t *connection =
            neo4j_connect("neo4j://user:pass@localhost:7687", NULL, NEO4J_INSECURE);
    if (connection == NULL)
    {
        neo4j_perror(stderr, errno, "Connection failed");
        return EXIT_FAILURE;
    }

    neo4j_result_stream_t *results =
            neo4j_run(connection, "RETURN 'hello world'", neo4j_null);
    if (results == NULL)
    {
        neo4j_perror(stderr, errno, "Failed to run statement");
        return EXIT_FAILURE;
    }

    neo4j_result_t *result = neo4j_fetch_next(results);
    if (result == NULL)
    {
        neo4j_perror(stderr, errno, "Failed to fetch result");
        return EXIT_FAILURE;
    }

    neo4j_value_t value = neo4j_result_field(result, 0);
    char buf[128];
    printf("%s\n", neo4j_tostring(value, buf, sizeof(buf)));

    neo4j_close_results(results);
    neo4j_close(connection);
    neo4j_client_cleanup();
    return EXIT_SUCCESS;
}

5. Neo4j for Python

$ pip3.6 install neo4j
from neo4j.v1 import GraphDatabase

neo4j_uri = "bolt://localhost:7687"
auth_user = 'neo4j'
auth_psd = 'root'


class HelloWorldExample(object):

    def __init__(self, uri, user, password):
        self._driver = GraphDatabase.driver(uri, auth=(user, password))

    def close(self):
        self._driver.close()

    def print_greeting(self, message):
        with self._driver.session() as session:
            greeting = session.write_transaction(self._create_and_return_greeting, message)
            print(greeting)
            # session.run()

    @staticmethod
    def _create_and_return_greeting(tx, message):
        result = tx.run("CREATE (a:Greeting) "
                        "SET a.message = $message "
                        "RETURN a.message + ', from node ' + id(a)", message=message)
        return result.single()[0]


if __name__ == '__main__':
    print('run in main...')
    example = HelloWorldExample(neo4j_uri, auth_user, auth_psd)
    example.print_greeting('Hello Neo4j')
    example.close()
$ python3.6 hello.py
run in main...
Hello Neo4j, from node 179

6. Reference

上一篇 下一篇

猜你喜欢

热点阅读