平凡な社会人の日記

平凡な社会人の日記です。怠惰な毎日を送っております。

grinem_python チートシート

これはなに?

gremlin_pythoについての知見が自分の中で溜まってきたので、共有も兼ねてここに残しておきます。

目標

gremlin python で基本的なクエリの書き方が分からない方が雰囲気で書けるようになる

こんにちは

ソフトウェアエンジニア一年生のさっちゃん(twitter: @toshiakisan1127)です。普段はWebアプリケーションのバックエンドをSpring boot, Pythonを使って書いたり、CloudFormationやTerraformでAWS上のリソースを管理をしています。好きなAWSのサービスはLambdaです。

はじめに

この記事を見る方はgremlin pythonについてはご存知だと思いますので、グラフDBやgremlin python自体の説明は省きます。知らない方は Amazon Neptune 触ってみた。~グラフDB楽しい。~ を見てもらえるとありがたいです。 また、Neptuneを使っている方はjupyter notebookからグラフを操作することもあると思いますが、その際にgremlin_pythonと書き方が少し違ったりするので、gremlin_pythonでの書き方と同時にjupyter_notebookでの書き方も一緒に載せておきます。

version

gremlinpython==3.5.2 python3.8

グラフDBとの接続

Lambdaなどから接続できない場合はセキュリティグループの設定やプライベートサブネットのルートテーブルのデフォルトルートがNATゲートウェイに向いているかを確認してください。

from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.process.anonymous_traversal import traversal

g = traversal().withRemote(DriverRemomteConnection("wss://(endpoint_name):(port_number)/gremlin','g'"))

Neptuneのマネジメントコンソールから開いた場合は, %%gremlin をセルの最初に書けば g というのが GraphTraversalSourceオブジェクトとして認識されます。

書き込み系

頂点を追加する

gremlinpython

g.addV("cat").toList() # Labelが cat の頂点ができる。戻り値はvertex object のリスト: [ v[abcd-12e...] ] .

Neptune jupyter notebook

%%gremlin
g.addV("cat")

頂点を作成する時にpropertyを追加する

gremlinpython

%%gremlin
g.addV("cat").property("name", "Tama").toList() # idがuuid, Labelが cat , name property が Tama の頂点ができる。

Neptune jupyter notebook

%%gremlin
g.addV("cat")..property("name", "Tama")

頂点のpropertyを変更する

gremlinpython

from gremlin_python.process.traversal import Cardinality

cat_vertex = g.addV("cat").property(Cardinality.single, "name", "Tama").toList()[0] # idがuuid, Labelが cat , name property が Tama の頂点ができる。
g.V(cat_vertex).property(Cardinality.single, "name", "Pochi").toList()

Neptune jupyter notebook

%%gremlin
g.addV("cat").property("name", "Tama")
%%gremlin
g.V().has(id, "cat_vertex_id").property(single, "name", "Pochi")

辺を追加する

from gremlin_python.process.traversal import  Cardinality

cat_vertex = g.addV("cat").property(Cardinality.single, "name", "Tama").toList()[0]
food_vertex = g.addV("food").toList()[0]

g.addE('love').from_(cat_vertex).to(food_vertex).toList()

Neptune jupyter notebook

%%gremlin
g.addE("love").from(g.V().has(id, "vertex1_id")).to(g.V().has(id, "vertex2_id"))

辺にpropertyを追加する

from gremlin_python.process.traversal import Cardinality

cat_vertex = g.addV("cat").property(Cardinality.single, "name", "Tama").toList()[0]
food_vertex = g.addV("food").toList()[0]

g.addE('love').property("hoge", "fuga").from_(cat_vertex).to(food_vertex).toList()

Neptune jupyter notebook

%%gremlin
g.addE("love").property("hoge", "fuga").from(g.V().has(id, "vertex1_id")).to(g.V().has(id, "vertex2_id"))

読み込み系

特定のIDを持つ頂点を取得する

from gremlin_python.process.traversal import T
cat_vertex_list = g.V().has(T.id, "<vertex_id>").toList()

Neptune jupyter notebook

%%gremlin
g.V().has(id, "<vertex_id>")

特定のLabelを持つ頂点を取得する

cat_vertex_list = g.V().hasLabel("cat").toList()

Neptune jupyter notebook

%%gremlin
g.V("cat")

特定のpropertyを持つ頂点を取得する

cat_vertex_list = g.V().has("property_1", "hoge").toList()

Neptune jupyter notebook

%%gremlin
cat_vertex = g.V().has("property_1", "hoge")

特定のIDを持つ辺を取得する

from gremlin_python.process.traversal import T
edge_list = g.E().has(T.id, "<edge_id>").toList()

Neptune jupyter notebook

%%gremlin
edge = g.E().has(T.id, "<edge_id>")

特定のLabelを持つ辺を取得する

edge_list = g.E().hasLabel("some_label").toList()

Neptune jupyter notebook

%%gremlin
edge = g.E("some_label")

特定のpropertyを持つ辺を取得する

edge_list = g.E().has("property_1", "hoge").toList()

Neptune jupyter notebook

%%gremlin
edge = g.E().has("property_1", "hoge")

特定の頂点から伸びる辺を取得する

from gremlin_python.process.traversal import T
edge_list = g.V().has(T.id, "some_vertex_id").outE().toList()

Neptune jupyter notebook

%%gremlin
edge = g.V().has(id, "some_vertex_id").outE()

特定の頂点から伸びる辺を取得する(Labelで絞る)

from gremlin_python.process.traversal import T
edge_list = g.V().has(T.id, "some_vertex_id").outE("some_edge_label").toList()

Neptune jupyter notebook

%%gremlin
edge = g.V().has(id, "some_vertex_id").outE("some_edge_label")

特定の頂点に入る辺を取得する

from gremlin_python.process.traversal import T
edge_list = g.V().has(T.id, "some_vertex_id").inE().toList()

Neptune jupyter notebook

%%gremlin
edge = g.V().has(id, "some_vertex_id").inE()

特定の頂点に入る辺を取得する(Labelで絞る)

from gremlin_python.process.traversal import T
edge_list = g.V().has(T.id, "some_vertex_id").inE("some_edge_label").toList()

Neptune jupyter notebook

%%gremlin
edge = g.V().has(id, "some_vertex_id").inE("some_edge_label")

特定の頂点に関する辺を向きに関係なく取得する

from gremlin_python.process.traversal import T
edge_list = g.V().has(T.id, "some_vertex_id").out

Neptune jupyter notebook

%%gremlin
edge = g.V().has(id, "some_vertex_id").both("some_edge_label")

特定の頂点から別の頂点へ伸びる辺を取得する

from gremlin_python.process.traversal import T
edge_list = g.V().has(T.id, "some_vertex_id").outE("some_edge_label").as_('e').inV().hasLabel('vertex_label').select('e').toList()

Neptune jupyter notebook

%%gremlin
edge = g.V().has(id, "some_vertex_id").outE("some_edge_label").as_('e').inV().hasLabel('vertex_label').select('e')

辺を無向辺だとみなしたときに連結な頂点の中から特定の頂点を探す

from gremlin_python.process.traversal import T
 #dedup().by(T.id) でidが重複している格納しているリストの中から消す。これによって無限ループにならない。
vertex_list = g.V().has(T.id, "some_vertex_id") \
    .repeat(__.both().dedup().by(T.id) \ 
    .until(__.has(T.id, "some_vertex2_id")) \
    .toList()

Neptune jupyter notebook

%%gremlin
edge = g.V().has(id, "some_vertex_id") \
    .repeat(__both()).dedup().by(id))\
    .until(__.has(id, "some_vertex2_id")) \
    .toList()

他の小技

jupyter nootbook上でグラフをいい感じに表示する

%%gremlin -p v,oute, inv
g.V().outE().inV().path().by(elementMap())

備考

使うサーバーによってはidがuuidだったりintegerだったりするので注意。

終わりに

案外多かった...。他にもあれば追加します。誰かの役に立てば幸いです。

参考