如何在node.js中使用neo4j

本章中你将会学到如何在node.js中使用neo4j图形数据库。

当你想存储或者查询和数据紧密关联的数据的时候,图形数据库很有用。

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);
});

上面我们使用 db.insertNode 来帮助我们创建一个特殊的节点。

存在一些可以用来更新,读取和删除的方法,你可使用这些方法和neo4j数据库进行交互,而不需要处理密码查询。你可以在API文档中查看更多。

 

转载于:https://www.cnblogs.com/RachelChen/p/6633462.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值