一個快速的REST例子
首先來看些基本知識。如果沒有服務(wù)API,Neo4j就不能支持其他語言。該接口提供一組基于JSON消息格式的RESTful Web服務(wù)和一個全面的發(fā)現(xiàn)機制。使用中使用這個接口的最快和最容易的方法是通過使用cURL:
$ curl http://localhost:7474/db/data/{ "extensions" : { }, "node" : "http://localhost:7474/db/data/node", "node_index" : "http://localhost:7474/db/data/index/node", "relationship_index" : "http://localhost:7474/db/data/index/relationship", "extensions_info" : "http://localhost:7474/db/data/ext", "relationship_types" : "http://localhost:7474/db/data/relationship/types", "batch" : "http://localhost:7474/db/data/batch", "cypher" : "http://localhost:7474/db/data/cypher", "transaction" : "http://localhost:7474/db/data/transaction", "neo4j_version" : "2.0.0-M03"}
從這個端點返回JSON對象包含一組資源名稱和URI下可以找到的Cypher端點。在消息載荷中接受傳送來的Cyper請求并執(zhí)行這些查詢,在HTTP響應(yīng)中返回結(jié)果。
正是這種REST API接口,使得現(xiàn)在已有的各種Neo4j驅(qū)動得以建立。py2neo提供了這些REST資源的簡單封裝,這使Python應(yīng)用程序開發(fā)者可以放心使用Neo4j而不用考慮底層的客戶機-服務(wù)器協(xié)議。
一個簡單的應(yīng)用
為實際驗證py2neo,我們將著眼于建立一個簡單的用于存儲姓名和電子郵件地址的通訊錄管理系統(tǒng)。我們自然會使用節(jié)點來模擬每一個獨立實體,但它是要記住,Neo4j沒有類型的概念。類型是從周圍的關(guān)系和屬性推斷來的。
下面的關(guān)系圖中人顯示為紅色、電子郵件地址節(jié)點顯示為藍色。這些當然是純粹的邏輯演示節(jié)點,但數(shù)據(jù)本身并沒有區(qū)別。
我們的應(yīng)用程序?qū)⑼瓿蓛蓚€功能:添加新的聯(lián)系人信息和檢索聯(lián)系人的完整列表。為此,我們將創(chuàng)建一個Person類包裝Py2neoNodeobject,這使我們有一個底層處理的實現(xiàn)且留出用戶級的功能。上圖中的ROOT節(jié)點是指上圖中一個固定的參考點,我們沿著這個點開始。
讓我們直接看看代碼。下面是一個完整的小型應(yīng)用。這個程序允許添加新的名字與一個或者更多email地址相連接的以及提供了一個容易的方式來顯示這些連接信息的一個命令行工具。沒有參數(shù)的運行是顯示使用模式,而且這個唯一的依賴只是需要一個本地未修改的Neo4j實例(instance)而已。
#!/usr/bin/env python# -*- coding: utf-8 -*- from __future__ import print_function import sysfrom py2neo import neo4j, node, rel graph_db = neo4j.GraphDatabaseService() class Person(object): _root = graph_db.get_or_create_indexed_node("reference", "contacts", "root") @classmethod def create(cls, name, *emails): person_node, _ = graph_db.create(node(name=name), rel(cls._root, "PERSON", 0)) for email in emails: graph_db.create(node(email=email), rel(cls._root, "EMAIL", 0), rel(person_node, "EMAIL", 0)) return Person(person_node) @classmethod def get_all(cls): return [Person(person.end_node) for person in cls._root.match("PERSON")] def __init__(self, node): self._node = node def __str__(self): return self.name + "/n" + "/n".join(" <{0}>" .format(email) for email in self.emails) @property def name(self): return self._node["name"] @property def emails(self): return [rel.end_node["email"] for rel in self._node.match("EMAIL")] if __name__ == "__main__": if len(sys.argv) < 2: app = sys.argv[0] print("Usage: {0} add <name> <email> [<email>...]".format(app)) print(" {0} list".format(app)) sys.exit() method = sys.argv[1] if method == "add": print(Person.create(*sys.argv[2:])) elif method == "list": for person in Person.get_all(): print(person) else:print("Unknown command")
新聞熱點
疑難解答
圖片精選