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

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

问题描述

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

1) 图表

frombulls.neo4jserver 导入图g = 图()

2) Neo4jClient

frombulls.neo4jserver import Neo4jClient客户端 = Neo4jClient()

谁能解释一下这里的概念差异?如果我想对服务器执行(相当多)Cypher 查询并最终并行执行,那么选择哪种方式更好?

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

解决方案

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

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

Neo4jClient 是 Neo4j 服务器的低级适配器——你通常不需要直接使用它,除非你在做自定义的东西——改用高级 Graph 类.

请参阅灯泡文档...

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

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

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

Lightbulb 最初设计用于免费的 Neo4j Heroku Add On,但 Bulbs 和 Lightbulb 都可以大量使用 Gremlin,并且 Neo4j Heroko Add On 不再提供免费版的 Gremlin.

灯泡模型文件包含高度自定义的Entry 模型和自定义的Graph 类——Entry 模型利用了低级别客户端:

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

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

<块引用>

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

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

<预><代码>>>>从bulls.neo4jserver 导入图>>>g = 图()>>>script = g.scripts.get('some_script')>>>params = dict(name="James", city="Dallas")>>>g.gremlin.execute(脚本,参数)

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

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

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

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

您可以查看源代码以了解它们是如何工作的...

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

<预><代码>>>>从bulls.neo4jserver 导入图>>>g = 图()>>>查询=开始一个=关系({eid})返回一个">>>参数 = 字典(eid=123)>>>边缘 = g.cypher.query(query, params)

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

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

您可以遍历迭代器...

<预><代码>>>>从bulls.neo4jserver 导入图>>>g = 图()>>>查询=开始一个=关系({eid})返回一个">>>参数 = 字典(eid=123)>>>边缘 = g.cypher.query(query, params)>>>for edge in edge:打印边缘

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

<预><代码>>>>从bulls.neo4jserver 导入图>>>g = 图()>>>查询=开始一个=关系({eid})返回一个">>>参数 = 字典(eid=123)>>>边缘 = g.cypher.query(query, params)>>>列表(边)

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

<预><代码>>>>从bulls.neo4jserver 导入图>>>g = 图()>>>查询=开始一个=关系({eid})返回一个">>>参数 = 字典(eid=123)>>>边缘 = g.cypher.query(query, params)>>>边缘.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 Graph 和 neo4jserver Neo4jclient 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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