Bulbflow:neo4jserver图和neo4jserver Neo4jclient之间的区别 [英] Bulbflow: difference between neo4jserver Graph and neo4jserver Neo4jclient

查看:107
本文介绍了Bulbflow:neo4jserver图和neo4jserver Neo4jclient之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在尝试学习如何连接到Neo4j服务器,并使用来自Python的Bulbflow在其上运行Cypher查询.我不明白的是,连接到neo4j服务器的两种可能性之间的区别:

1)图表

from bulbs.neo4jserver import Graph
g = Graph()

2) Neo4jClient

from bulbs.neo4jserver import Neo4jClient
client = Neo4jClient()

有人可以在这里解释概念上的区别吗? 最好选择哪种方式,然后再选择是否要对服务器执行(很多)Cypher查询并最终并行执行?

PS:我没有足够的声誉来创建这个问题的标签"bulbflow":)

解决方案

灯泡支持三种不同的图形数据库服务器- Neo4j服务器 Rexster ,现在是 Titan .

特定于每个后端服务器的代码包含在其自己的Python包(目录)中.您应该看到以下目录:neo4jserver,rexster,titan:

Neo4jClient是Neo4j Server的低级适配器-您通常不需要直接使用它,除非您要进行自定义操作-而是使用高级Graph类.

请参阅灯泡"文档以了解...

灯泡快速入门"指南提供了有关使用Graph界面的示例:

但是,当您需要通过_client变量访问时,您的Bulbs对象始终可以访问低级客户端.

Lightbulb是我创建的一个示例应用程序,用于展示如何使用和自定义Bulbs模型-这是一个Python博客引擎,使用Git进行源代码控制,使用图数据库进行持久化.

灯泡最初设计用于免费的 Neo4j Heroku插件,但是灯泡和灯泡都可以克里姆林宫的大量使用,而Neo4j Heroko Add On不再提供免费版的Gremlin. /p>

灯泡模型文件包含一个高度自定义的Entry模型和一个自定义的Graph类-Entry模型使用了低级客户端:

如您在Entry模型中所见,我可以通过_client var访问低级客户端,并且我使用它从scripts库中获取Gremlin脚本,然后再次执行Gremlin脚本.

这是Entry模型使用的save_blog_entry Gremlin脚本的代码:

注意:该文件中只有一个Gremlin脚本,但是很大,包含 几次操作,一切都包裹在一个事务中. 将所有操作放到一个Gremlin脚本中,您可以 在一个事务请求中完成所有操作,而不是让 向服务器发送多个请求的开销.

除非您要进行自定义模型之类的操作,否则通常会使用scripts对象和存储在graph对象上的gremlin对象:

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() 
>>> script = g.scripts.get('some_script')
>>> params = dict(name="James", city="Dallas")
>>> g.gremlin.execute(script, params)

请参阅灯泡Neo4j Gremlin文档...

同样,当您要执行Neo4j Cypher查询时,请使用存储在graph对象上的cypher对象.

有三种Cypher方法(不幸的是,这些方法尚未在网站上记录):

  1. g.cypher.query():在返回节点/关系列表时使用-它将把它们初始化为对象.
  2. g.cypher.table():在返回Cypher表数据时使用.
  3. g.cypher.exectue():在返回任意数据时使用-返回通用的Response对象.

您可以查看源代码以了解其工作原理...

以下是使用Cypher query()方法的一些示例(查询只是返回一个关系):

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() 
>>> query = "start a = relationship({eid}) return a"
>>> params = dict(eid=123) 
>>> edges = g.cypher.query(query, params)

query方法自动将元素初始化为其类型.如果您将元素创建为自定义模型,则Bulbs会尝试将其初始化为特定类型,否则将默认为通用VertexEdge.

请注意,Bulbs Cypher query()方法返回一个迭代器.

您可以遍历迭代器...

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() 
>>> query = "start a = relationship({eid}) return a"
>>> params = dict(eid=123) 
>>> edges = g.cypher.query(query, params)
>>> for edge in edges: print edge

...或将其转换为列表...

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() 
>>> query = "start a = relationship({eid}) return a"
>>> params = dict(eid=123) 
>>> edges = g.cypher.query(query, params)
>>> list(edges)

...或获取下一个项目...

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() 
>>> query = "start a = relationship({eid}) return a"
>>> params = dict(eid=123) 
>>> edges = g.cypher.query(query, params)
>>> edges.next()

如果您还有其他问题,请告诉我.

I am now trying to learn how to connect to Neo4j server and run Cypher queries on it using Bulbflow from Python. And the thing I do not understand is the difference between two possibilities to connect to the neo4j server:

1) Graph

from bulbs.neo4jserver import Graph
g = Graph()

2) Neo4jClient

from bulbs.neo4jserver import Neo4jClient
client = Neo4jClient()

Could anyone please explain the conceptual difference here? And which way is it better to choose if I then want to execute (quite a lot of) Cypher queries against the server and ultimately in parallel?

PS: I do not have enough reputation to create a tag "bulbflow" for this question :)

解决方案

Bulbs supports three different graph database servers -- Neo4j Server, Rexster, and now Titan.

Code specific to each backend server is contained within its own Python package (directory). You should see directories for: neo4jserver, rexster, titan:

Neo4jClient is the low-level adapter for Neo4j Server -- you usually don't need to use this directly unless you are doing custom stuff -- use the high-level Graph class instead.

See the Bulbs docs for...

The Bulbs Quickstart guide provides examples on using the Graph interface:

However, your Bulbs objects always have access to the low-level client when you need it via the _client var.

Lightbulb is an example app I created to show how to use and customize Bulbs models -- it's a Python blog engine that uses Git for source control and a graph database for persistence.

Lightbulb was originally designed for use with the free Neo4j Heroku Add On, but both Bulbs and Lightbulb make heavy use of Gremlin, and the Neo4j Heroko Add On no longer offers Gremlin in the free edition.

The Lightbulb model file contains a heavily customized Entry model and a custom Graph class -- the Entry model makes use of the low-level client:

As you can see in the Entry model, I have access to the low-level client via the _client var, and I use it to get a Gremlin script from the scripts library and then again to execute the Gremlin script.

Here's the code for the save_blog_entry Gremlin script used by the Entry model:

NOTE: There is only one Gremlin script in the file, but it is large, contains several operations, and everything is wrapped in a transaction. Putting all the operations into a single Gremlin script allows you to do everything in one transactional request, rather than having the overhead of sending multiple requests to the server.

Unless you are doing something like customizing a model, you would normally use the scripts object and the gremlin object stored on the graph object:

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() 
>>> script = g.scripts.get('some_script')
>>> params = dict(name="James", city="Dallas")
>>> g.gremlin.execute(script, params)

See the Bulbs Neo4j Gremlin docs...

Likewise, when you want to execute a Neo4j Cypher query, use the cypher object stored on the graph object.

There are three Cypher methods (unfortunately these aren't documented on the website yet):

  1. g.cypher.query(): Used when returning a list of nodes/relationships -- it will initialize them to objects.
  2. g.cypher.table(): Used when returning Cypher table data.
  3. g.cypher.exectue(): Used when returning arbitrary data -- it returns a generic Response object.

You can look at the source code to see how they work...

Here are some examples of using the Cypher query() method (the query simply returns a relationship):

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() 
>>> query = "start a = relationship({eid}) return a"
>>> params = dict(eid=123) 
>>> edges = g.cypher.query(query, params)

The query method automatically initializes elements to their type. If you created the element as a custom model, Bulbs will try to initialize it to the specific type, otherwise it will default to a generic Vertex or Edge.

Note that the Bulbs Cypher query() method returns an iterator.

You can loop over the iterator...

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() 
>>> query = "start a = relationship({eid}) return a"
>>> params = dict(eid=123) 
>>> edges = g.cypher.query(query, params)
>>> for edge in edges: print edge

...or convert it to a list...

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() 
>>> query = "start a = relationship({eid}) return a"
>>> params = dict(eid=123) 
>>> edges = g.cypher.query(query, params)
>>> list(edges)

...or get the next item...

>>> from bulbs.neo4jserver import Graph
>>> g = Graph() 
>>> query = "start a = relationship({eid}) return a"
>>> params = dict(eid=123) 
>>> edges = g.cypher.query(query, params)
>>> edges.next()

Please let me know if you have any more questions.

这篇关于Bulbflow:neo4jserver图和neo4jserver Neo4jclient之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆