Memorandum on how to use gremlin python

Article content

I tried using Gremlin, which is a GraphDB, in Python. Make a note of how to use

Library preparation

Install the library to handle Gremlin in Python

pip install gremlinpython

Implementation

Library import

from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

Creating a connection

graph = Graph()

# Gremlin connection creation
g = graph.traversal().withRemote(DriverRemoteConnection('ws://localhost:8182/gremlin','g'))

Pass Gremlin server information as the first argument of DriverRemoteConnection "G" is passed in the second argument, but it is unknown what this "g" refers to. The following log is output when the server starts, and it seems to be this "g".

[INFO] ServerGremlinExecutor - A GraphTraversalSource is now bound to [g] with graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]

I feel like I can change it with some configuration file, but I haven't been able to find out so far at this point.

Data registration

Register the data and try to get the registered data

g.addV('tarouo').property('name', 'tarou').toSet()
g.addV('tarouo').property('name', 'kotarou').toSet()
name = g.V().has('name', 'tarou').valueMap().toList()
print(name)

result

[{'name': ['tarou']}]

The acquisition result is returned as a dict. The registered name is in list format for some reason Data acquisition is not a partial match but an exact match

Add property

Add property to registered data

1 item added

tarou = g.V().has('name', 'tarou').toList()[0]
g.V(tarou).property('from', 'Tokyo').toSet()
tarou = g.V(tarou).valueMap().toList()[0]
print(tarou)

result

{'name': ['tarou'], 'from': ['Tokyo']}

Add multiple items

g.V(tarou).property('from', 'Tokyo').property('age', 20).toSet()
tarou = g.V(tarou).valueMap().toList()[0]
print(tarou)

result

{'name': ['tarou'], 'from': ['Tokyo'], 'age': [20]}

Multiple items can be added by connecting .property ()

update

Updated when a property that has already been registered is registered with a different value

g.V(tarou).property('from', 'Kanagawa').property('age', 30).toSet()
tarou = g.V(tarou).valueMap().toList()[0]
print(tarou)

Delete property

I feel like I can do it, but I don't know how to do it ... I will update it when I know how to do it.

Data deletion

g.V().drop()
g.E().drop()
g.V().drop().iterate()
g.E().drop().iterate()

Without iterate, the first one will be deleted. If iterate is present, all will be deleted.

Add edge

Add edges to tarou and kotarou

tarou = g.V().has('name', 'tarou').toList()[0]
kotarou = g.V().has('name', 'kotarou').toList()[0]
g.addE('follow').from_(tarou).to(kotarou).toSet()
edgeList = g.E().toList()
for edge in edgeList:
    print(edge)

result

e[267][259-follow->261]

I was able to get that ID259 and ID261 are connected in a follow relationship.

tarou = g.V().has('name', 'tarou').toList()[0]
kotarou = g.V().has('name', 'kotarou').toList()[0]
magotarou = g.V().has('name', 'magotarou').toList()[0]
g.addE('son').from_(tarou).to(kotarou).toSet()
g.addE('son').from_(kotarou).to(magotarou).toSet()
g.addE('grandchild').from_(tarou).to(magotarou).toSet()

You can add an edge like this

e[298][288-son->290]
e[299][290-son->292]
e[300][288-grandchild->292]

Get edge information

Get a list (value) related to a specific point

valueList = g.V(tarou).both().valueMap(True).toList()
for value in valueList:
    print(value)

result

{<T.id: 1>: 498, <T.label: 4>: 'kotarou', 'name': ['kotarou']}
{<T.id: 1>: 500, <T.label: 4>: 'magotarou', 'name': ['magotarou']}

I was able to get information about kotarou and magotarou that have a relationship with tarou.

Do not set the valueMap argument

If True is not set, only the minimum data will be available.

{'name': ['kotarou']}
{'name': ['magotarou']}

Get a list (edge) related to a specific point

g.V(tarou).bothE()
g.V(tarou).bothE().valueMap().toList()
g.V(tarou).bothE().valueMap(True).toList()

I will try to get it in three ways

 edge list 1..
e[597][587-son->589]
e[599][587-grandchild->591]
 edge list 2..
{}
{}
 edge list 3..
{<T.id: 1>: 597, <T.label: 4>: 'son'}
{<T.id: 1>: 599, <T.label: 4>: 'grandchild'}

I was able to get it. I thought that g.V () was a Value-related operation, so I wondered if I could get edge-related data with g.E (), but I couldn't get it well.

in conclusion

I was researching how to use Gremlin for a while, thinking that it could be used for network analysis, but I got the impression that it would be difficult without understanding the concept of GraphDB. I will update the article if I find an interesting usage other than the contents described.

Recommended Posts

Memorandum on how to use gremlin python
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
A memorandum on how to use keras.preprocessing.image in Keras
How to use Django on Google App Engine / Python
Python: How to use async with
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
Notes on how to use pywinauto
Notes on how to use featuretools
How to use homebrew on Debian
[Python] How to use list 3 Added
How to use Mysql in python
How to use OpenPose's Python API
How to use ChemSpider in Python
How to use FTP with Python
Python: How to use pydub (playback)
How to use PubChem in Python
How to use python zip function
Notes on how to use doctest
[Python] How to use Typetalk API
How to use MultiIndex (personal memorandum)
[Hyperledger Iroha] Notes on how to use the Python SDK
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to read pydoc on python interpreter
How to install and use pandas_datareader [Python]
[Kivy] How to install Kivy on Windows [Python]
How to use mecab, neologd-ipadic on colab
[python] How to use __command__, function explanation
[Python] How to use import sys sys.argv
How to use Google Assistant on Windows 10
How to erase Python 2.x on Mac.
[Python] Organizing how to use for statements
[Python2.7] Summary of how to use unittest
python: How to use locals () and globals ()
How to use __slots__ in Python class
Install python on xserver to use pip
How to use "deque" for Python data
How to use Python zip and enumerate
[Python] Understand how to use recursive functions
Summary of how to use Python list
How to use regular expressions in Python
[Python2.7] Summary of how to use subprocess
How to use is and == in Python
[Blender x Python] How to use modifiers
[Question] How to use plot_surface of python
How to use python put in pyenv on macOS with PyCall
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data