/
mysql2mongo.rb
58 lines (51 loc) · 1.5 KB
/
mysql2mongo.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
#
# this program uses mysql library instead of mysql2 library because of 'server side cursor problem(for too-big-tables)'.
# under my circumstans, mysql2 :stream option does not work well(may be database is too old(mysql 4.1.***))(2012/4/20).
#
# !! may be mysql library version should be 2.8.XXX. it seems that 3.XXX(still alpla version now) will not support server side cursor.
#
# HACKME
# maybe column name 'id' should set to be '_id'
#
require 'rubygems'
require 'mysql' # should be 2.8.XXX # should use bundler......sorry
require 'mongo'
MONGO_SETTING = {
:connection_string => 'localhost',
:database => 'test'
}
MYSQL_SETTING = {
:host=>'localhost',
:user=>'hoge',
:password=>'hoge',
:database=>'test',
:charset_name=>'utf8'
}
my = Mysql.init
my.options(Mysql::SET_CHARSET_NAME, MYSQL_SETTING[:charset_name])
my.connect(MYSQL_SETTING[:host],MYSQL_SETTING[:user],MYSQL_SETTING[:password],MYSQL_SETTING[:database])
my.query_with_result = false
mongo = Mongo::Connection.new(MONGO_SETTING[:connection_string])
mongo_db = mongo[MONGO_SETTING[:database]]
tables = []
query = "show tables"
my.query(query)
my.use_result.each do |row|
tables << row[0]
end
tables.each_with_index do |current_table,i|
puts "-- #{current_table} --"
coll = mongo_db[current_table]
query = "select * from #{current_table}"
my.query(query)
j = 0
results = my.use_result
results.each_hash do |hash|
print "." if j % 10000 == 0
coll.insert(hash)
j += 1
end
puts ''
#resutl = nil
end