我正在尝试使用这个Ruby脚本:
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 要将数据从bson转换为sql,会抛出以下错误: convertbson2sql.rb:13:in <main>': undefined methodread_bson_document‘for BSON:Module (NoMethodError)
我认为在BSON库的新版本中不再使用read_bson_document方法,我在网上和BSON库文档中进行了搜索,但我找不到替代方法。
PS :我对Ruby几乎一无所知,我只想用脚本来转换我的数据。
发布于 2018-07-10 04:19:09
不是100%确定,但它看起来像是2.0.0有了大规模的CHANGE LOG变化。
基于此,您可以尝试以下操作:
require 'bson'
require 'stringio'
stringio = StringIO.new
stringio.write File.read(file_name)
stringio.rewind
bson = BSON::Document.from_bson(stringio)根据@SergioTulentsev的说法,合适的方式实际上可能是:
bson = BSON::Document.from_bson(BSON::ByteBuffer.new(File.read(file_name)))但是,只有当每个文件都是单个BSON::Document时,这才能起作用。
https://stackoverflow.com/questions/51242412
复制相似问题