gremlin_python encapsulation using add-delete and alter methods

Keywords: ElasticSearch Attribute Apache pip

Article Directory

1. Install Janusgraph

1. Download the installation package

Going to janusgraph's Official Release Page Download the offline installation package, as shown in the figure below. The latest version is 0.4.0. Click janusgraph-0.4.0-hadoop2.zip to download.Please note that this installation package can only be installed on linux system!!!


2. Unzip operation

Open the terminal, use the unzip command to unzip the downloaded zip installation package, then go to the janusgraph-0.4.0 directory, execute bin/janusgraph.sh start, you will get the following results, indicating that janusgraph ran successfully.

	$ bin/janusgraph.sh start
	Forking Cassandra...
	Running `nodetool statusthrift`.. OK (returned exit status 0 and printed string "running").
	Forking Elasticsearch...
	Connecting to Elasticsearch (127.0.0.1:9300)... OK (connected to 127.0.0.1:9300).
	Forking Gremlin-Server...
	Connecting to Gremlin-Server (127.0.0.1:8182)... OK (connected to 127.0.0.1:8182).
	Run gremlin.sh to connect.

3. Other operations

Executing bin/janusgraph.sh status in the janusgraph-0.4.0 directory allows you to see the running status and process ID of janusgraph, while executing bin/janusgraph.sh stop shuts down janusgraph and all processes associated with it.

	$ ./bin/janusgraph.sh status
	Gremlin-Server (org.apache.tinkerpop.gremlin.server.GremlinServer) is running with pid 31841
	Elasticsearch (org.elasticsearch.bootstrap.Elasticsearch) is running with pid 31668
	Cassandra (org.apache.cassandra.service.CassandraDaemon) is running with pid 31336
	
	$ bin/janusgraph.sh stop
	Killing Gremlin-Server (pid 31841)...
	Killing Elasticsearch (pid 31668)...
	Killing Cassandra (pid 31336)...

2. Connect gremlin

1. Connect using the gremlin console

In the janusgraph-0.4.0 directory, executing bin/gremlin.sh launches the Gremlin console, as shown below.You can then perform gremlin-related operations, such as: the remote command tells the console to connect to the gremlin service remotely using the conf/remote.yaml configuration file, > is the Submit command, such as: > graph.addVertex ("name", "test") is a node with a name of test, and then: > g.V(). values ('name') is the query for thatAll nodes in the graph have values for the name attribute.

	$  bin/gremlin.sh
	         \,,,/
	         (o o)
	-----oOOo-(3)-oOOo-----
	plugin activated: tinkerpop.server
	plugin activated: tinkerpop.hadoop
	plugin activated: tinkerpop.utilities
	plugin activated: janusgraph.imports
	plugin activated: tinkerpop.tinkergraph
	gremlin> :remote connect tinkerpop.server conf/remote.yaml
	==>Connected - localhost/127.0.0.1:8182
	gremlin> :> graph.addVertex("name", "test")
	==>v[4726]
	gremlin> :> g.V().values('name')
	==>test

2. Connect using gremlin_python

First you need to install gremlin_python, a dependent package, and use the pip command to install pip install Gremlin python==3.4.1. Note here that the maximum version of Gremlin supported by janusgraph0.4.0 is 3.4.1, so you need to specify the version number when you install it.
Once the installation is complete, you can connect in your python program, noting that you need to keep the janusgraph running.

	from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
	from gremlin_python.process.anonymous_traversal import traversal
	
	connection = DriverRemoteConnection('ws://127.0.0.1:8182/gremlin', 'g')
	graph = traversal().withRemote(connection)
	graph.addV('person').property('name', 'test').next()	# Add Node
	print(graph.V().values('name').toList())	# Output as ['test']

3. Encapsulation of methods for addition, deletion and alteration

1. New Nodes

Simply pass in the label and properties (dict, optional) of the new node and return the Vertex(id, label) type.

	def add_vertex(graph, label, properties=None):
	    """
	    add vertex
	    :param graph: graph, type: GraphTraversalSource
	    :param label: label, type: str
	    :param properties: property dict, like {'p1': 'value1', 'p2': 'value2'}
	    :return: vertex, Vertex(id, label)
	    """
	    vert = graph.addV(label)
	    if properties:
	        for key in properties.keys():
	            vert.property(key, properties.get(key))
	    return vert.next()

2. Add Edges

Pass in label and properties (dict, optional) for the new edge, and two nodes (or their ID s) v_from and v_to for which the edge needs to be added.

	def add_edge(graph, label, v_from, v_to, properties=None):
	    """
	    add edge
	    :param graph: graph, type: GraphTraversalSource
	    :param label: label, type: str
	    :param v_from: long vertex id or Vertex(id, label) of from
	    :param v_to: long vertex id or Vertex(id, label) of to
	    :param properties: property dict, like {'p1': 'value1', 'p2': 'value2'}
	    :return: None
	    """
	    if isinstance(v_from, int):
	        v_from = graph.V().hasId(v_from).next()
	    if isinstance(v_to, int):
	        v_to = graph.V().hasId(v_to).next()
	    edge = graph.V(v_from).addE(label).to(v_to)
	    if properties:
	        for key in properties.keys():
	            edge.property(key, properties.get(key))
	    edge.next()

3. Delete Nodes

You can delete specific nodes as required, such as nodes (or their ID s), label s, properties.If no other parameters are passed in, all nodes are deleted by default.

	def drop_vertex(graph, v_id=None, label=None, properties=None):
	    """
	    drop all vertex or specific vertex
	    :param graph: graph, type: GraphTraversalSource
	    :param v_id: long vertex id or Vertex(id, label)
	    :param label: label, type: str
	    :param properties: property list, like ['p1', 'p2', {'p3': 'value'}]
	    :return: None
	    """
	    if isinstance(v_id, int):
	        v_id = graph.V().hasId(v_id).next()
	    travel = graph.V(v_id) if v_id else graph.V()
	    if label:
	        travel = travel.hasLabel(label)
	    if properties:
	        for p in properties:
	            if isinstance(p, dict):
	                key = list(p.keys())[0]
	                travel = travel.has(key, p.get(key))
	            else:
	                travel = travel.has(p)
	    travel.drop().iterate()

4. Delete Edges

Specific edges can be deleted as required, such as by edge ID, label, properties.If no other parameters are passed in, all edges are deleted by default.

	def drop_edge(graph, e_id=None, label=None, properties=None):
	    """
	    drop all edges or specific edge
	    :param graph: graph, type: GraphTraversalSource
	    :param e_id: edge id, type str
	    :param label: label, type: str
	    :param properties: property list, like ['p1', 'p2', {'p3': 'value'}]
	    :return: None
	    """
	    travel = graph.E(e_id) if e_id else graph.E()
	    if label:
	        travel = travel.hasLabel(label)
	    if properties:
	        for p in properties:
	            if isinstance(p, dict):
	                key = list(p.keys())[0]
	                travel = travel.has(key, p.get(key))
	            else:
	                travel = travel.has(p)
	    travel.drop().iterate()

5. Query Node

First, you can query all the attribute values of a node (or its ID), using return travel.valueMap().toList().
Second, you can query all eligible nodes through label or properties, using return travel.toList().

	def query_vertex(graph, v_id=None, label=None, properties=None):
	    """
	    query graph vertex (value) list
	    :param graph: graph, type: GraphTraversalSource
	    :param v_id: long vertex id or Vertex(id, label)
	    :param label: label, type: str
	    :param properties: property list, like ['p1', 'p2', {'p3': 'value'}]
	    :return: vertex list or vertex value list
	    """
	    if isinstance(v_id, int):
	        v_id = graph.V().hasId(v_id).next()
	    travel = graph.V(v_id) if v_id else graph.V()
	    if label:
	        travel = travel.hasLabel(label)
	    if properties:
	        for p in properties:
	            if isinstance(p, dict):
	                key = list(p.keys())[0]
	                travel = travel.has(key, p.get(key))
	            else:
	                travel = travel.has(p)
	    # return travel.valueMap().toList()
	    return travel.toList()

6. Query Edges

Query all attribute values of qualified edges based on their ID, label, or properties.

	def query_edge(graph, e_id=None, label=None, properties=None):
	    """
	    query graph edge value list
	    :param graph: graph, type: GraphTraversalSource
	    :param e_id: edge id, type str
	    :param label: label, type: str
	    :param properties: property list, like ['p1', 'p2', {'p3': 'value'}]
	    :return: valueMap list
	    """
	    travel = graph.E(e_id) if e_id else graph.E()
	    if label:
	        travel = travel.hasLabel(label)
	    if properties:
	        for p in properties:
	            if isinstance(p, dict):
	                key = list(p.keys())[0]
	                travel = travel.has(key, p.get(key))
	            else:
	                travel = travel.has(p)
	    return travel.valueMap().toList()

7. Query all edges connected to nodes

Query all edges connected to a node (or its ID).

	def query_edges_of_vertex(graph, v_id):
	    """
	    query all edges of vertex
	    :param graph: graph, type: GraphTraversalSource
	    :param v_id: v_id: long vertex id or Vertex(id, label)
	    :return: edge list
	    """
	    if isinstance(v_id, int):
	        v_id = graph.V().hasId(v_id).next()
	    result = []
	    in_edges = graph.V(v_id).inE().toList()
	    out_edges = graph.V(v_id).outE().toList()
	    result.extend(in_edges)
	    result.extend(out_edges)
	    return result

8. Query all nodes connected to a node

Queries all nodes connected to a node (or its ID).

	def query_near_vertex(graph, v_id):
	    """
	    query near vertices of vertex
	    :param graph: graph, type: GraphTraversalSource
	    :param v_id: v_id: long vertex id or Vertex(id, label)
	    :return: vertex list
	    """
	    if isinstance(v_id, int):
	        v_id = graph.V().hasId(v_id).next()
	    result = []
	    out_v = graph.V(v_id).out().toList()
	    in_v = graph.V(v_id).in_().toList()
	    result.extend(out_v)
	    result.extend(in_v)
	    return result

4. Other methods of encapsulation

1. Get the ID of the edge

	def get_edge_id(edge):
	    """
	    get edge id
	    :param edge: Egde(id, label, outV, inV)
	    :return: edge id, type str
	    """
	    return edge.id.get('@value').get('relationId')

2. Node Attributes to Dictionary

	def vertex_to_dict(graph, vertex):
	    """
	    transfer Vertex's info to dict
	    :param graph: graph, type: GraphTraversalSource
	    :param vertex: vertex, Vertex(id, label)
	    :return: vertex info dict
	    """
	    properties = graph.V(vertex).valueMap().toList()[0]
	    for key in properties.keys():
	        properties[key] = properties.get(key)[0]
	    return {
	        'id': vertex.id,
	        'label': vertex.label,
	        'properties': properties
	    }

3. Edge attribute conversion dict

def edge_to_dict(graph, edge):
    """
    transfer Edge's info to dict
    :param graph: graph, type: GraphTraversalSource
    :param edge: edge, Edge(id, label, outV, inV)
    :return: edge info dict
    """
    e_id = get_edge_id(edge)
    properties = graph.E(e_id).valueMap().toList()[0]
    return {
        'id': e_id,
        'label': edge.label,
        'properties': properties
    }

4. Determine whether a node is in the graph

For known node label and properties values, determine whether the node is in the graph; if it is, return the node, otherwise return None.

	def judge_vertex_in_graph(graph, vertex_dict):
	    """
	    judge a vertex whether in graph
	    :param graph: graph, type: GraphTraversalSource
	    :param vertex_dict: vertex dict, like {'label': 'value1', 'properties': {'p1': 'v1', ...}}
	    :return: None or Vertex(id,label)
	    """
	    label = vertex_dict.get('label')
	    properties = vertex_dict.get('properties')
	    travel = graph.V()
	    if label:
	        travel = travel.hasLabel(label)
	    if properties:
	        for k in properties.keys():
	            travel = travel.has(k, properties.get(k))
	    if travel.hasNext():
	        return travel.next()
	    return None

5. Getting subgraphs

The subgraph is derived from specific node or edge information.

	def get_sub_graph(graph, vertices=None, edges=None, vertex_properties=None):
	    """
	    get sub graph
	    :param graph: graph, type: GraphTraversalSource
	    :param vertices: hasLabel('label').has('property').has('age', gt(20))
	    :param edges: hasLabel('label').has('property')
	    :param vertex_properties:
	    :return: sub_graph, type: GraphTraversalSource
	    """
	    strategy = SubgraphStrategy(vertices=vertices, edges=edges, vertex_properties=vertex_properties)
	    return graph.withStrategies(strategy)

5. Acknowledgements

All of the above codes are for reference and we appreciate your reading.If you have any problems, you can leave a message below to communicate. I hope you can study together and make progress together.

Posted by tckephart on Thu, 05 Sep 2019 21:04:36 -0700