cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to export from query by APOC?

lingvisa
Graph Fellow

I am looking at this APOC export example:

WITH "MATCH path = (person:Person)-[:DIRECTED]->(movie)
      RETURN person.name AS name, person.born AS born,
             movie.title AS title, movie.tagline AS tagline, movie.released AS released" AS query
CALL apoc.export.csv.query(query, "movies-directed.csv", {})
YIELD file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data
RETURN file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data;

It's fine if I do this in browser, but I have to do it in my program. So, I type a query into a file. query.txt:

with "MATCH (m:Product) return m" as query

Then I read from the file, create the query and run it:

def query(cypher_path)
           query_str = ''
            with open(cypher_path, encoding='utf8') as f:
                for line in f:
                    line = line.strip()
                    query_str += line + '\n'
            query_str = query_str.strip()
            print(query_str)
            
            CALL = "\nCALL apoc.export.csv.data(query, "test.csv", {})"
            YIELD = "\nYIELD file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data"
            RETURN = "\nRETURN file, source, format, nodes, relationships, properties, time, rows, batchSize, batches, done, data"
            cypher = CALL + YIELD + RETURN
            print('{}'.format(cypher))
            tx.run(cypher)

This gives an error message:

neobolt.exceptions.CypherSyntaxError: Variable `query` not defined (line 2, column 27 (offset: 27))
"CALL apoc.export.csv.data(query, 'test.csv', {})"

How to make the 'query' available in the apoc.query() command in this case?

0 REPLIES 0