最近需要将sqlserver数据导入到mongodb中,在github上搜了一圈,发现两个项目有点适合

  1. mongify
  2. sql2mongodb

先试了下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版本以上才行

  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的更多相关文章

  1. SQLSERVER数据导入到MYSQL

    SQLSERVER数据导入到MYSQL http://hi.baidu.com/luck001221/item/cb4462299f9ea79ab73263d2?qq-pf-to=pcqq.group ...

  2. SQLServer 数据导入导出 SSIS 包 位置

    笔记:sqlserver 在执行数据导入导出的时候,可以选择是否保存SSIS包,如果选择保存,在保存方式有:SQlserver .文件系统.如果选择sqlserver 则 包信息保存在 msdb 系统 ...

  3. python 读取SQLServer数据插入到MongoDB数据库中

    # -*- coding: utf-8 -*-import pyodbcimport osimport csvimport pymongofrom pymongo import ASCENDING, ...

  4. SQLServer数据导入Mongodb

    一.思路 MongoVUE免费版支持MySQL导入Mongo,所以思路是SQLServer导入MySQL,再从MySQL导入Mongo. 二.准备 1,安装mysql数据库(我用的是WAMP,集成my ...

  5. sqlserver数据导入导出问题

    sqlserver,如果用结果另存为,导出txt数据,然后在导入数据库,有时候会出问题,很难解决. 但是全选,右击,复制到自己创建的txt里面,在导入数据,就不会有问题的. 神奇,不知道为什么,但是能 ...

  6. sqlserver数据导入问题:报错“对COM组件的调用返回了错误HRESULT E_FAIL”

    SQL server 2008,导出了两个sql文件. 打开第一个文件,没有问题,建好相应的数据库,运行脚本,即可导入. 第二个文件却遇到问题,始终报错“对COM组件的调用返回了错误HRESULT E ...

  7. SqlServer数据导入到ORACLE

    ORACLE中执行 select * from SYSTEM."employ_epl"

  8. 使用pandas把mysql的数据导入MongoDB。

    使用pandas把mysql的数据导入MongoDB. 首先说下我的需求,我需要把mysql的70万条数据导入到mongodb并去重, 同时在第二列加入一个url字段,字段的值和第三列的值一样,代码如 ...

  9. 【实战】使用 Kettle 工具将 mysql 数据增量导入到 MongoDB 中

    最近有一个将 mysql 数据导入到 MongoDB 中的需求,打算使用 Kettle 工具实现.本文章记录了数据导入从0到1的过程,最终实现了每秒钟快速导入约 1200 条数据.一起来看吧~ 一.K ...

随机推荐

  1. js 温故而知新 webkitTransitionEnd 监听Transition动画结束事件

    css3的过渡属性transition,在动画结束时,也存在结束的事件:webkitTransitionEnd; 注意:transition,也仅仅有这一个事件. http://www.runoob. ...

  2. Android基础笔记(十八)- Fragment

    博客的感悟终点-開始 什么是Fragment 加入fragment到Activity的两种方式 Fragment的生命周期 Fragment的向下兼容 Fragment之间的通信 博客的感悟,终点-開 ...

  3. php优化(php.ini)

    PHP优化 ------------------------------------- 尽量选择php5.4及以上的版本,里面很多优化参数已经移除了相比以前版本   1.引擎解析优化和加速 1)eac ...

  4. nodejs 学习资料大全

    1.blog学习篇 http://blog.fens.me/series-nodejs/ 从零开始nodejs系列文章

  5. quartz项目中的运用

    下面是之前项目中quartz的运用,我将它梳理出来. 测试类: public class OrdExpireTaskMain { public static void main(String[] ar ...

  6. 转载了个js代码

    document.selection.createRange方法 document.selection.createRange() 根据当前文字选择返回 TextRange 对象,或根据控件选择返回 ...

  7. 第三篇: Ansible 配置节点认证

    应用场景:          有如下4台主机:               cpy01.dev.xjh.com              cpy02.dev.xjh.com              ...

  8. Java线程—-Runnable和Callable的区别和联系

    Java 提供了三种创建线程的方法 1.继承Thread接口 public class Thread2Thread { public static void main(String[] args) { ...

  9. Swift_4_闭包(Blocks)

    import Foundation println("Hello, World!") var arr = [1,2,4,6,74,2] func hasClosure(list:[ ...

  10. 2016 acm香港网络赛 A题. A+B Problem (FFT)

    原题地址:https://open.kattis.com/problems/aplusb FFT代码参考kuangbin的博客:http://www.cnblogs.com/kuangbin/arch ...