如何在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文檔中查看更多。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM