【问题标题】:Bulbs: How to check if two vertices are connected by an edge in Titan灯泡:如何检查两个顶点是否通过 Titan 中的边连接
【发布时间】:2014-08-28 20:59:03
【问题描述】:

我正在使用 TitanGraphDB + Cassandra。我正在按如下方式启动 Titan

cd titan-cassandra-0.3.1
bin/titan.sh config/titan-server-rexster.xml config/titan-server-cassandra.properties

我有一个 Rexster shell,可以用来与上面的 Titan+Cassandra 通信。

cd rexster-console-2.3.0
bin/rexster-console.sh

我正在尝试使用 Titan Graph DB 对网络拓扑进行建模。我想从我的 python 程序中对 Titan Graph DB 进行编程。我正在为此使用灯泡包。 我创建了三种类型的顶点

 - switch
 - port 
 - device

我在物理连接的端口之间创建带标签的边缘。我使用的标签是“链接”。

假设我有两个端口顶点portAportB

我想写一个函数如下

def is_connected(portA, portB):
     ...
     ... 
     ...

如何确定两个顶点是否“由标记的边连接”?

我有两个图顶点

src_sw_other_ports
<Vertex: http://localhost:8182/graphs/graph/vertices/48>

dst_sw_other_ports
<Vertex: http://localhost:8182/graphs/graph/vertices/72>

我试过了

link_exists = src_sw_other_ports.out.retain([dst_sw_other_ports]).hasNext()

它给了我以下错误。

  File "/home/karthik/Projects/ryu/ryu/app/simple_switch.py", line 238, in compute_path
    link_exists = src_sw_other_ports.out.retain([dst_sw_other_ports]).hasNext()
  File "/usr/local/lib/python2.7/dist-packages/bulbs/element.py", line 209, in __getattr__
    raise AttributeError(name)
AttributeError: out

【问题讨论】:

标签: groovy cassandra gremlin titan bulbs


【解决方案1】:
def is_connected(portA, portB):
    return portA.both("link").retain([portB]).hasNext()

http://gremlindocs.com/#recipes/finding-edges-between-vertices

注意:我在上面的代码中使用了您的函数定义;但是,您的函数定义使用 Python 语法,而不是 Groovy,因此上述示例仅在您从 Jython 调用 Gremlin 代码时才有效。

Gremlin-Groovy 的定义是:

def isConnected (portA, portB) {
     return portA.both("link").retain([portB]).hasNext()
}

【讨论】:

  • count() 是否可以返回任何正数或零或仅返回“0”或“1”?
  • count()可以返回0或任意正数(任意正数为true);但是,我更新了使用retain()hasNext() 的答案,因此count() 不再与此答案相关。
  • 我正在使用来自 Python 的 Gremlin 代码?您是说 Jython 还是打错字?
  • 它似乎可以使用上面链接中提到的 gremlin 我必须从我的 python 代码创建一个 TinkerGraph()。我目前正在创建 Graph() ?如果我想使用 gremlin 查询,这会起作用吗?
  • 当我尝试上面的代码文件“/home/karthik/Projects/ryu/ryu/app/simple_switch.py​​”,第 160 行,在 is_connected return portA.both("link ").retain([portB]).hasNext() 文件 "/usr/local/lib/python2.7/dist-packages/bulbs/element.py",第 209 行,在 getattr 中引发AttributeError(name) AttributeError: both
【解决方案2】:

espeed 的回答很好。这是另一种选择:

gremlin> g = TinkerGraphFactory.createTinkerGraph()
==>tinkergraph[vertices:6 edges:6]
gremlin> v1 = g.v(1)
==>v[1]
gremlin> v3 = g.v(3)
==>v[3]
gremlin> v6 = g.v(6)
==>v[6]
gremlin> v1.out.retain([v3]).hasNext()
==>true
gremlin> v1.out.retain([v6]).hasNext()
==>false

比使用count 好一点,就好像你只想要一个边缘的“存在”,你不必迭代整个管道来这样做。

【讨论】:

  • 从我的python代码中,我从bulbs.titan import Graph g = Graph() 执行以下操作,或者我应该使用createTinkerGraph()吗?
  • 我只用createTinkerGraph()创建了一些示例数据来演示retain的使用。显然,您应该使用自己在 Rexster 中定义的 g 状态。
  • 我已经尝试了你的建议,但我遇到了一些错误。我已经更新了问题的详细信息。谢谢。
  • 我知道您正在使用 Bulbs,但要正确使用该工具,您需要了解 Gremlin 及其在 Groovy 中的根源。现在,您似乎正试图将我工作的 Groovy 与 Python/Bulbs 的东西混合在一起……那是行不通的。如果我错了,espeed 可以纠正我,但我认为你需要将你的函数编写为一个 groovy 脚本,然后你可以在灯泡中作为一个函数执行。 bulbflow.com/api/bulbs/gremlin
  • 我已经发布了另一个关于如何调用 gremlin 脚本的问题。stackoverflow.com/questions/24769857/…
猜你喜欢
  • 2012-12-01
  • 1970-01-01
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多