Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
ruby convert_cypher_dump.rb dump.cypher
(!) Remember to set proper ID_FIELD_NAME
Removes transactions from .cypher dump:
1. removes begin/commit
2. Adds matching of nodes by ID to create relation
3. adds ";" to the end of each instruction
Import result:
cat dump_converted.cql | neo4j-shell | grep -E 'WARNING:|Unknown\ command'
ID_FIELD_NAME = 'ID' # field, by which your nodes UNIQUELY identified
ids = {}
filename = ARGV[0]
raise 'wrong extension' unless filename.end_with? '.cypher'
out_filename = filename.gsub('.cypher', '_converted.cypher'), 'w') do |out_file|, 'r').each do |line|
next unless line.length > 3
# Clean transactions
next if %w(begin commit).include? line.strip
# Nodes creation
pair = /^.*\((_\d+):.*ID`:(\d+).*$/.match line
if pair
ids[pair[1]] = pair[2]
# Relations creation
pair = /^.*\((_\d+)\)-.*>\((_\d+)\)$/.match line
# RegEXP for older Neo4j versions:
# pair = /^.*(_\d+)-.*>(_\d+)$/.match line
if pair
id1 = ids[pair[1]]
id2 = ids[pair[2]]
line = "MATCH (a {#{ID_FIELD_NAME}:#{id1}}), (b {#{ID_FIELD_NAME}:#{id2}}) #{line}"
line.gsub!(/\((_\d+)\)-/, '(a)-')
line.gsub!(/>\((_\d+)\)/, '>(b)')
# old Neo4j format:
#line.gsub!(/(_\d+)-/, '(a)-')
#line.gsub!(/>(_\d+)/, '>(b)')
# Add ";"
if line.start_with? 'create (' or
line.start_with? 'MATCH '
# prepend ";", because multiline values possible for node/relation properties
line = ";\n"+line
out_file.print line
out_file.print ";\n"
puts "done: #{out_filename}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.