1

I would like to execute a batch of queries in RedisGraph with the Python API in order to speed up the creation of big knowledge graphs.

In Neo4J, the UNWIND command can be used by the Neo4J Python API and allows to parallelize queries. In this snippet, you can seen how the Python API supports UNWIND - the run method takes batch as parameter. batch is a list of dictionaries. Every dictionary has head_id, tail_id and properties as keys.

with session.begin_transaction() as tx:  # In this transaction relationships are inserted in the database
    cypher_query = 'UNWIND $batch as row ' \
    'MATCH (head:Node) WHERE head.id = row.head_id ' \
    'MATCH (tail:Node) WHERE tail.id = row.tail_id ' \
    'CREATE (head)-[rel:RELATIONSHIP]->(tail) ' \
    'SET rel += row.properties'

     tx.run(cypher_query, batch=batch)

In RedisGraph, UNWIND is also available (as it is a Cypher command). However, I don't know how to pass a batch in the Python API:

cypher_query = 'UNWIND $batch as row ' \
        'MATCH (head:Node) WHERE head.id = row.head_id ' \
        'MATCH (tail:Node) WHERE tail.id = row.tail_id ' \
        'CREATE (head)-[rel:RELATIONSHIP]->(tail) ' \
        'SET rel += row.properties'
r = redis.StrictRedis()
r.execute_command('GRAPH.QUERY', graph_name, cypher_query)  #No batch can be passed!!

Do you know a solution? Thanks.

2 Answers2

1

The redisgraph-py README shows an example of how to pass parameters via its query() method:

...
params = {'purpose':"pleasure"}
query = """MATCH (p:person)-[v:visited {purpose:$purpose}]->(c:country)
     RETURN p.name, p.age, v.purpose, c.name"""

result = redis_graph.query(query, params)
...

If you really need to use execute_command() instead, you can take a look at how query() is implemented.

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • Thanks for the answer. I think redis_graph.query(query, params) does not really implement what I was looking for. In the snipped I posted in my question, batch is a list of dictionaries, which is then unwinded by the UNWIND command. query() does not seem to support that. – Filippo Grazioli Aug 18 '20 at 09:44
1

Here the answer from the RedisLabs dev team:

github.com/RedisGraph/RedisGraph/issues/1293

As of now, the feature is not supported, but will be introduced in the future.