Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
=begin
USAGE:
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'
=end
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')
File.open(out_filename, 'w') do |out_file|
File.open(filename, '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]
end
# 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)')
end
# Add ";"
if line.start_with? 'create (' or
line.start_with? 'MATCH '
# prepend ";", because multiline values possible for node/relation properties
line = ";\n"+line
end
out_file.print line
end
out_file.print ";\n"
end
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.