Examples
Batch Insert
Efficiently load multiple edges at once using put_batch.
Use put_batch() to efficiently insert multiple edges in a single operation.
Inserting Multiple Edges
from cog.torque import Graph
g = Graph("movies")
g.put_batch([
("inception", "directed_by", "nolan"),
("inception", "genre", "scifi"),
("interstellar", "directed_by", "nolan"),
("interstellar", "genre", "scifi"),
("dunkirk", "directed_by", "nolan"),
("dunkirk", "genre", "war"),
])Querying the Data
Find all films directed by Nolan by following incoming directed_by edges:
# All Nolan films
g.v("nolan").inc("directed_by").all()
# {'result': [{'id': 'inception'}, {'id': 'interstellar'}, {'id': 'dunkirk'}]}Performance Tips
- Use
put_batch()instead of multipleput()calls for better performance - Set
flush_intervalwhen creating the graph for large imports:
g = Graph("movies", flush_interval=100)- Call
g.sync()after batch operations to ensure data is persisted