如何使用Python与Titan图数据库进行交互?

6

我是新手,正在尝试将Titan与Python配合使用。我已经为此苦苦思索了一天半,但仍然无法有所进展。我尝试过bulbs和rexpro-python,但似乎都无法正常工作。

rexpro-python中,以下代码:

from rexpro import RexProConnection
conn = RexProConnection('localhost', 8184, 'graph')

如果使用Titan版本0.3.2、0.3.1或0.2.1,程序可能会卡住,并出现以下服务器消息。

13/09/18 16:59:27 WARN filter.RexProMessageFilter: unsupported rexpro version: 1

Bulbs中:

from bulbs.config import Config, DEBUG
from bulbs.rexster import Graph

config = Config('http://localhost:8182/graphs/graph')
g = Graph(config)

产生以下错误:
SystemError: ({'status': '500', 'transfer-encoding': 'chunked', 'server': 'grizzly/2.2.16', 'connection': 'close', 'date': 'Wed, 18 Sep 2013 21:06:27 GMT', 'access-control-allow-origin': '*', 'content-type': 'application/json'}, '{"message":"","error":"javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: groovy.lang.MissingMethodException.idx() is applicable for argument types: () values: []\\nPossible solutions: is(java.lang.Object), any(), find(), any(groovy.lang.Closure), with(groovy.lang.Closure), _(groovy.lang.Closure)","api":{"description":"evaluate an ad-hoc Gremlin script for a graph.","parameters":{"rexster.returnKeys":"an array of element property keys to return (default is to return all element properties)","rexster.showTypes":"displays the properties of the elements with their native data type (default is false)","load":"a list of \'stored procedures\' to execute prior to the \'script\' (if \'script\' is not specified then the last script in this argument will return the values","rexster.offset.end":"end index for a paged set of data to be returned","rexster.offset.start":"start index for a paged set of data to be returned","params":"a map of parameters to bind to the script engine","language":"the gremlin language flavor to use (default to groovy)","script":"the Gremlin script to be evaluated"}},"success":false}')

在Titan服务器上也有类似的异常情况。有人成功解决了吗?

使用 bulbs.titan 而不是 bulbs.rexster - espeed
2个回答

1

从Titan 1.0.0版本开始,我们有更好的方法来连接Python。

现在Titan带有Gremlin服务器。Gremlin服务器提供了非JVM语言(例如Python、Javascript等)与TinkerPop堆栈通信的能力。

Gremlin Server是Rexster的替代品。

要启动Gremlin服务器(此脚本已打包到Titan中):

sh gremlin-server.sh 

同一目录下还有适用于Windows的类似批处理脚本。

启动后,以下Python驱动程序应该可以帮助连接到Gremlin服务器:

  • aiogremlin - 基于asyncio和aiohttp的Python 3库,使用WebSockets与Gremlin服务器通信。
  • gremlinclient - 用于Gremlin服务器的异步Python 2/3客户端,允许使用灵活的协程语法 - Trollius、Tornado、Asyncio。这是与aiogremlin相同作者的作品。我认为,aiogremlin不再受支持,这是他正在开发的最新项目。
  • gremlinrestclient - 使用HTTP与Gremlin服务器进行REST通信的Python 2/3库。

基于Python的查询语言库,可以在开发过程中提供帮助:

  • gremlin-py - 编写纯Python Gremlin,可发送到Gremlin服务器。
  • gremlin-python - 允许在遍历属性图时使用Python语法。

1
在rexpro-python的情况下,你会遇到版本问题。最新版本的RexPro Python将连接到TinkerPop/Rexster 2.4.0。Titan目前还不支持该版本。从Titan 0.3.2开始,它支持TinkerPop 2.3.x。看起来这是在rexpro-python升级到2.4.0兼容性之前的最后一次提交:

https://github.com/bdeggleston/rexpro-python/commit/3597f4ce5a4da69ec64f174aa1a064abf7524693

但您可能需要稍微查看提交历史记录,以确保获取正确的版本。

Bulbs似乎正在调用手动索引,这是Titan不支持的。在gremlin-users和/或areuliusgraphs邮件列表中有许多关于此问题的帖子。查看此帖子,其中提到了您确切的问题:

https://groups.google.com/forum/#!msg/gremlin-users/s7Ag1tjbxLs/nC5WjtHh6woJ

简短回答是Bulbs已更新以支持Titan。也许,您仍然有某个版本不兼容的地方。

我已经成功地将你提到的 RexPro 版本与 Titan 0.3.1 配合使用。我认为其他版本也应该可以工作。 - Tim Ludwinski
重新审视这个问题,我怀疑Bulbs接口无法工作是因为Titan 0.3.2(可能也包括0.3.1等版本)存在一个bug,导致REST服务器默认情况下无法启动(尽管RexPro可以)。我现在正在使用0.4.0版本。 - Tim Ludwinski
使用bubls.titan而不是bulbs.rexster。 - espeed

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接