Created
October 10, 2011 19:16
-
-
Save Andrzej/1276244 to your computer and use it in GitHub Desktop.
Convert BSON file into SQL, useful for fast copy data from MongoDB to MySQL (mongodump => bson2sql => import to mysql)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "bson" | |
def make_insert(table_name, bson) | |
columns = ["id",*bson["value"].keys] * ", " | |
values = ["'#{bson["_id"]}'",*bson["value"].values.map{|value| value.is_a?(Numeric) ? value : "'#{value}'"}] * ", " | |
return "insert into #{table_name} (#{columns}) values (#{values});" | |
end | |
file_name = ARGV.first | |
file=File.new(file_name) | |
table_name=File.basename(file_name,".*") | |
while not file.eof? do | |
bson = BSON.read_bson_document(file) | |
STDOUT << make_insert(table_name,bson) | |
STDOUT << "\n" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
bson2sql.rb:13:in
<main>': undefined method
read_bson_document' for BSON:Module (NoMethodError)Has the bson module changed since this was developed?