I tried using Gremlin, which is a GraphDB, in Python. Make a note of how to use
Install the library to handle Gremlin in Python
pip install gremlinpython
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
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.
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 to registered data
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']}
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 ()
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)
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.
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 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]
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.
If True is not set, only the minimum data will be available.
{'name': ['kotarou']}
{'name': ['magotarou']}
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.
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