使用mongify将sqlserver数据导入到mongodb
最近需要将sqlserver数据导入到mongodb中,在github上搜了一圈,发现两个项目有点适合
先试了下sql2mongodb(有个好名字果然有好处啊), 可是,but,但是,这东西好像并不没有名字取得好,试了下感觉不靠谱,弃坑,逃~~~
说点题外话,sql2server以来nodejs, mongify依赖ruby, 这两个库都是我最讨厌的,暂时没有理由。
好了,现在咱专注mongify吧
先看官网介绍:
Mongify是一个将数据从sql数据库转换到mongoDB的ruby工具。支持MySQL,PostgreSQL,SQLite,Oracle,SQLServer和DB2(基本上ActiveRecord内置的内容),支持任何版本的MongoDB。
但是,缺点是作者声称只在MySql和SQLite测试过,对于我现在需要的sqlserver,心中一群在奔腾。。。
安装
使用的环境是ubuntu12.04,该版本软件仓库中的ruby是1.8版本的,需要ruby2.1版本以上才行
- 省懒,直接加ppa吧, 这里可以随便安装到2.3版本
From the travis-cli installation instructions for Ubuntu, the Brightbox Ruby NG(NextGeneration) ppa:
$ sudo apt-get install python-software-properties
$ sudo apt-add-repository ppa:brightbox/ruby-ng
$ sudo apt-get update
$ sudo apt-get install ruby2.1 ruby-switch
$ sudo ruby-switch --set ruby2.1
顺便注明下gem的使用吧
ubuntu@aws:~$ gem --help
RubyGems is a sophisticated package manager for Ruby. This is a
basic help message containing pointers to more information.
Usage:
gem -h/--help
gem -v/--version
gem command [arguments...] [options...]
Examples:
gem install rake
gem list --local
gem build package.gemspec
gem help install
Further help:
gem help commands list all 'gem' commands
gem help examples show some examples of usage
gem help gem_dependencies gem dependencies file guide
gem help platforms gem platforms guide
gem help <COMMAND> show help on COMMAND
(e.g. 'gem help install')
gem server present a web page at
http://localhost:8808/
with info about installed gems
Further information:
http://guides.rubygems.org
好,那我们安装mongify吧gem install mongify
经过一轮踩坑,还是关注下mongify目录结构吧 其中有一个文件mongify.gemspec
$:.push File.expand_path("../lib", __FILE__)
require "mongify/version"
Gem::Specification.new do |s|
s.name = "mongify"
s.version = Mongify::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Andrew Kalek"]
s.email = ["andrew.kalek@anlek.com"]
s.homepage = "http://mongify.com"
s.summary = %q{Translate your SQL data to MongoDB with ease}
s.description = %q{Mongify allows you to map your sql data into a mongodb document database with a simple DSL.}
s.required_ruby_version = ">= 1.8.7"
s.add_runtime_dependency('activerecord', ">= 4.2", "< 5.0")
s.add_runtime_dependency('activesupport', ">= 4.2", "< 5.0")
s.add_runtime_dependency('mongo', "= 1.12.5")
s.add_runtime_dependency('bson', "= 1.12.5")
s.add_runtime_dependency('bson_ext', "= 1.12.5") unless RUBY_PLATFORM == 'java'
s.add_runtime_dependency('highline', '= 1.7.8')
s.add_development_dependency('rspec', '~> 2.0')
s.add_development_dependency('rspec-collection_matchers', '~> 1.0')
s.add_development_dependency('cucumber', '>= 0.10')
s.add_development_dependency('mocha', '>= 0.9.8')
s.add_development_dependency('yard', '>= 0.8')
s.add_development_dependency('sqlite3', '>= 1.3')
s.add_development_dependency('pg', '>= 0.17')
s.add_development_dependency('mysql2', '>= 0.4')
s.add_development_dependency('watchr', '>= 0.6')
s.add_development_dependency('rake')
s.add_development_dependency('jazz_fingers')
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
s.extra_rdoc_files = [
"CHANGELOG.md",
"README.rdoc"
]
s.rdoc_options = ["--title", "Mongify -- SQL db to Mongo db converter", "--main", "README", "--line-numbers", "--inline-source"]
end
可以看到这里指定了依赖,对版本也有要求。
如果已经安装过mongify1.3.1版本的话,导出sqlserver数据库时将会发现如果要支持sqlserver数据库还需要安装activerecord-sqlserver-adapter
而在activerecord-sqlserver-adapter官网有这样的描述
The SQL Server adapter for ActiveRecord v5.0 using SQL Server 2012 or higher.
Interested in older versions? We follow a rational versioning policy that tracks Rails. That means that our 5.0.x version of the adapter is only for the latest 5.0 version of Rails. If you need the adapter for SQL Server 2008 or 2005, you are still in the right spot. Just install the latest 3.2.x to 4.1.x version of the adapter that matches your Rails version. We also have stable branches for each major/minor release of ActiveRecord.
由于我的sqlserver是2008版本的,也就是说我最高可以使用4.1.x版本,并且最好有配套的ActiveRecord
所以我选择了1.2.4版本的Mongify,可以看下这个版本的依赖
s.add_dependency('activerecord', "~> 3.2")
s.add_dependency('activesupport', "~> 3.2")
s.add_dependency('mongo', "~> 1.10.2")
s.add_dependency('bson', "~> 1.10.2")
s.add_dependency('bson_ext', "~> 1.10.2") unless RUBY_PLATFORM == 'java'
s.add_dependency('highline', '>= 1.6.1')
对应的可以满足3.2.x-4.1.x这个要求,那么就是他了
重新安装Mongify
gem install mongify -v 1.2.4
根据上面的依赖,会安装对应版本软件再安装activerecord-sqlserver-adapter
gem install activerecord-sqlserver-adapter -v 3.2.17
创建配置文件
为了让Mongify正常工作,它需要知道数据库的位置及连接信息。
构建配置文件就像这样简单: (对于我们,adapter需要改成sqlserver)
sql_connection do
adapter "mysql"
host "localhost"
username "root"
password "passw0rd"
database "my_database"
batch_size 10000 # This is defaulted to 10000 but in case you want to make that smaller (on lower RAM machines)
end
mongodb_connection do
host "localhost"
database "my_database"
end
检查配置文件
mongify check database.config
生成转换文件
如果您的数据库庞大而复杂,手工编写转换文件可能会有太多的工作。 Mongify的可以直接使用命令生成:
mongify translation database.config
可以使用重定向将配置输出到文件
mongify translation database.config > translation_file.rb
最后将sql数据导入到mongodb中
mongify process database.config translation_file.rb
导出时每次读取10000条数据,以进度条显示进度
Copying playsrc (261/607): (10000/10000) 100% |ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo| Time: 00:00:03
Copying playsrc (262/607): (10000/10000) 100% |ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo| Time: 00:00:03
Copying playsrc (263/607): (10000/10000) 100% |ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo| Time: 00:00:02
Copying playsrc (264/607): (10000/10000) 100% |ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo| Time: 00:00:02
Copying playsrc (265/607): (10000/10000) 100% |ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo| Time: 00:00:03
Copying playsrc (266/607): (10000/10000) 100% |ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo| Time: 00:00:03
Copying playsrc (267/607): (10000/10000) 100% |ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo| Time: 00:00:02
Copying playsrc (268/607): (10000/10000) 100% |ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo| Time: 00:00:02
Copying playsrc (269/607): (10000/10000) 100% |ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo| Time: 00:00:03
Copying playsrc (270/607): (10000/10000) 100% |ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo| Time: 00:00:03
Copying playsrc (271/607): (10000/10000) 100% |ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo| Time: 00:00:02
附录Mongify的命令,总共就四个主要命令,以上用过了三个
Usage: mongify command database_config [database_translation.rb]
Commands:
"check" or "ck" >> Checks connection for sql and no_sql databases [configuration_file]
"process" or "pr" >> Takes a translation and process it to mongodb [configuration_file, translation_file]
"sync" or "sy" >> Takes a translation and process it to mongodb, only syncs (insert/update) new or updated records based on the updated_at column [configuration_file, translation_file]
"translation" or "tr" >> Outputs a translation file from a sql connection [configuration_file]
Examples:
mongify translation datbase.config
mongify tr database.config
mongify check database.config
mongify process database.config database_translation.rb
mongify sync database.config database_translation.rb
Common options:
-h, --help Show this message
-v, --version Show version
使用mongify将sqlserver数据导入到mongodb的更多相关文章
- SQLSERVER数据导入到MYSQL
SQLSERVER数据导入到MYSQL http://hi.baidu.com/luck001221/item/cb4462299f9ea79ab73263d2?qq-pf-to=pcqq.group ...
- SQLServer 数据导入导出 SSIS 包 位置
笔记:sqlserver 在执行数据导入导出的时候,可以选择是否保存SSIS包,如果选择保存,在保存方式有:SQlserver .文件系统.如果选择sqlserver 则 包信息保存在 msdb 系统 ...
- python 读取SQLServer数据插入到MongoDB数据库中
# -*- coding: utf-8 -*-import pyodbcimport osimport csvimport pymongofrom pymongo import ASCENDING, ...
- SQLServer数据导入Mongodb
一.思路 MongoVUE免费版支持MySQL导入Mongo,所以思路是SQLServer导入MySQL,再从MySQL导入Mongo. 二.准备 1,安装mysql数据库(我用的是WAMP,集成my ...
- sqlserver数据导入导出问题
sqlserver,如果用结果另存为,导出txt数据,然后在导入数据库,有时候会出问题,很难解决. 但是全选,右击,复制到自己创建的txt里面,在导入数据,就不会有问题的. 神奇,不知道为什么,但是能 ...
- sqlserver数据导入问题:报错“对COM组件的调用返回了错误HRESULT E_FAIL”
SQL server 2008,导出了两个sql文件. 打开第一个文件,没有问题,建好相应的数据库,运行脚本,即可导入. 第二个文件却遇到问题,始终报错“对COM组件的调用返回了错误HRESULT E ...
- SqlServer数据导入到ORACLE
ORACLE中执行 select * from SYSTEM."employ_epl"
- 使用pandas把mysql的数据导入MongoDB。
使用pandas把mysql的数据导入MongoDB. 首先说下我的需求,我需要把mysql的70万条数据导入到mongodb并去重, 同时在第二列加入一个url字段,字段的值和第三列的值一样,代码如 ...
- 【实战】使用 Kettle 工具将 mysql 数据增量导入到 MongoDB 中
最近有一个将 mysql 数据导入到 MongoDB 中的需求,打算使用 Kettle 工具实现.本文章记录了数据导入从0到1的过程,最终实现了每秒钟快速导入约 1200 条数据.一起来看吧~ 一.K ...
随机推荐
- vim 批处理
一.使用args , argdo 进行文件批处理 1. :args ./src/**/*.js 利用args命令标记所要处理的文件 2. :argdo %s/tabindex/tabI ...
- Spring学习一----------Spring概况
© 版权声明:本文为博主原创文章,转载请注明出处 Spring概况 Spring是为了解决企业应用开发的复杂性而创建的. Spring是一种轻量级的控制反转(IOC)和面向切面(AOP)的容器框架. ...
- MFC错误集锦
MFC中相关报错及其解决的方法: (1)0x00000005: 解决的方法:看是哪里的 数组越界: (2)0xCCCCCCCC:在类中声明指针,但没有赋初值之类的错误. 解决的方法:在类的构造函数中给 ...
- 解读Unity中的CG编写Shader系列3——表面剔除与剪裁模式
在上一个样例中,我们得到了由mesh组件传递的信息经过数学转换至合适的颜色区间以颜色的形式着色到物体上. 这篇文章将要在此基础上研究片段的擦除(discarding fragments)和前面剪裁.后 ...
- js项目第一课:获取节点的方法有三个
第一种方法: demo.html代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...
- Junit 内部解密之二: TestResult + TestListener + Assert
转自:http://blog.sina.com.cn/s/blog_6cf812be0100wbhw.html 之前我们看到了Test接口里面的run方法有个TestResult的参数,不错,这个类就 ...
- 一个方便的图片载入框架——ImageViewEx
我的博客:http://mrfufufu.github.io/ 一.前言 近期在整理项目中的一些代码,以备即将开展的新项目中使用,刚刚整理到一个图片载入的 lib.用起来很的简单,和 picasso ...
- hdu - 5033 - Building(单调栈)
题意:N 幢楼排成一列(1<=N<=10^5),各楼有横坐标 xi(1<=xi<=10^7) 以及高度 hi(1<=hi<=10^7),在各楼之间的Q个位置(1&l ...
- linux 参数内核
优化Linux内核参数 转自:http://www.centoscn.com/CentOS/config/2013/0804/992.html vim /etc/sysctl.conf 1.net ...
- 解决ajax跨域问题的多种方法
//第一种方法使用jsonp的方式 <script type="text/javascript" src="http://www.youxiaju.com/js/j ...