用户登录
用户注册

分享至

neo4j中文使用手册以及例子

  • 作者: qing零
  • 来源: 51数据库
  • 2020-09-28
neo4j是一个可有效存储,处理和查询你数据模型中紧密相连的元素的数据库。
neo4j有很强大且灵活的数据模型,你可以使用其来表示你真实的,易变结构的信息,且不失真。

使用内置的REST API来和Neo4j通讯
Neo4j数据库有内在的HTTP REST接口,我们可以使用其直接和Neo4j数据库交接(interface)。
你需要简单的使用POST向一个HTTP URL发送请求,且接受来自Neo4j的响应。
在下面的实例中你可以看见Node.js请求模块调用了REST,在请求模块(request module)中这样做很便利。
安装request模块是:
> npm install request

让我们创建一个空的文件,且在文档中写下下述代码:
//Let’s load the request module
var request = require("request");

上面我们将使用require函数来在我们的项目中加载request模块

创建一个将会触发密码查询(cypher query)的方法:
下面我们将要创建一个函数,使用密码查询(cypher query)作为输入,且使用HTTP接口在数据库中触发这个密码查询(cypher query)。我们在manual中可以详尽看到端点协议(endpoint protocol)和格式。你可以使用其做更多的事情:例如,在每个请求中发送很多标准 或者 在众多请求中保持通讯打开(keep transactions)。你可在neoe4j手册中多看看。
下面让我们定义数据库的host
//Define your host and port. This is where your database is running. Here it is defined on localhost.
var host = 'localhost',
port = 7474;

定义我们需要连接的URL,在Neo4说明文档中指明:
//This is the URL where we will POST our data to fire the cypher query. This is specified in Neo4j docs.
var httpUrlForTransaction = 'http://' + host + ':' + port + '/db/data/transaction/commit';

接下来,你需要定义可将cyper作为参数的函数,用来执行密码查询。
参数可以是任何的用来密码查询的参数,且当数据库有响应的时候要触发回调函数。

//Let’s define a function which fires the cypher query.
function runCypherQuery(query, params, callback) {
request.post({
uri: httpUrlForTransaction,
json: {statements: [{statement: query, parameters: params}]}
},
function (err, res, body) {
callback(err, body);
})
}

触发一些查询:
现在我们需要一个可以在neo4.js中触发查询的函数,让我们通过保存neo4j中一个节点的方法来使用这个函数。

/**
* Let’s fire some queries below.
* */
runCypherQuery(
'CREATE (somebody:Person { name: {name}, from: {company}, age: {age} }) RETURN somebody', {
name: 'Ghuffran',
company: 'Modulus',
age: 44
}, function (err, resp) {
if (err) {
console.log(err);
} else {
console.log(resp);
}
}
);

在上面的例子中,我们使用了一个密码查询来保存节点(node)
你可以查看更多关于密码查询语言的说明
注意:使用上述的参数来进行查询是好想法。因为Neo4j缓存查询路径,然后结合我们传递的不同参数来运行这个查询路径。这增加了查询执行的速度。
现在让我们总结上面的所有代码,我们的文件应该看上去是:

//Let’s load the request module
var request = require("request");

//Define your host and port. This is where your database is running. Here it’s on localhost.
var host = 'localhost',
port = 7474;

//This is the url where we will POST our data to fire the cypher query. This is specified in Neo4j docs.
var httpUrlForTransaction = 'http://' + host + ':' + port + '/db/data/transaction/commit';

//Let’s define a function which fires the cypher query.
function runCypherQuery(query, params, callback) {
request.post({
uri: httpUrlForTransaction,
json: {statements: [{statement: query, parameters: params}]}
},
function (err, res, body) {
callback(err, body);
})
}

/**
* Let’s fire some queries as shown below.
* */
runCypherQuery(
'CREATE (somebody:Person { name: {name}, from: {company}, age: {age} }) RETURN somebody', {
name: 'Ghuffran',
company: 'Modulus',
age: 44
}, function (err, resp) {
if (err) {
console.log(err);
} else {
console.log(resp);
}
}
);

现在我们只要使用内置的HTTP REST API和Neo4j 数据库进行交接就可。

使用Node.js 的node-neo4j(philippkueng)模块和neo4j进行通信
你可使用一些模块来和Node.js只的neo4j进行interface,但是node-neo4j(Thingdom)和node-neo4j(philipkueng)是使用最广泛的。
你可以根据个人喜好进行选择使用。
下面例子中使用的是node-neo4j()philippkueng模块,因为其利于说明:

我们可以加载这个模块:
> npm install node-neo4j

现在让我们看看如何使用node-neo4j模块来触发密码查询,就像使用其他的对象接口一样。

//Require the Neo4J module
var neo4j = require('node-neo4j');

//Create a db object. We will using this object to work on the DB.
db = new neo4j('http://localhost:7474');

//Run raw cypher with params
db.cypherQuery(
'CREATE (somebody:Person { name: {name}, from: {company}, age: {age} }) RETURN somebody',
{
name: 'Ghuffran',
company: 'Modulus',
age: ~~(Math.random() * 100) //generate random age
}, function (err, result) {
if (err) {
return console.log(err);
}
console.log(result.data); // delivers an array of query results
console.log(result.columns); // delivers an array of names of objects getting returned
}
);

上麦我们使用模块提供的db.cypherQuery(query, [params|Optional], [include_stats|Optional], callback) 方法来运行我们的密码查询。
node-neo4j模块提供了一系列的帮助方法。
让我们看看我们如何通过使用帮助函数来保存我们的节点。

//Require the Neo4J module
var neo4j = require('node-neo4j');

//Create a db object. We will using this object to work on the DB.
db = new neo4j('http://localhost:7474');

//Let’s create a node
db.insertNode({
name: 'Ghuffran',
company: 'Modulus',
age: ~~(Math.random() * 100)
}, function (err, node) {
if (err) {
return console.log(err);
}

// Output node data.
console.log(node);
});
软件
前端设计
程序设计
Java相关