migrate Data From PostgreSQL to mongoDB using PG's copy AND mongoimport

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云数据库 MongoDB,通用型 2核4GB
简介:
最近有个项目启用了mongoDB主要用作查询缓存。
需要将部分PostgreSQL中的数据导入到mongoDB中,写程序来处理的话确实是比较烦。
对应格式如下:
migrate Data From PostgreSQL to mongoDB using PGs copy AND mongoimport - 德哥@Digoal - The Heart,The World.
 
还好mongoDB有一个比较好的工具mongoimport可以导入格式csv , tsc , json 等格式的文件。

1. 使用csv格式导入,这里遇到了数据类型的问题,比如某字段需要导入为string类型,进去变成了数值类型。(这里的话应该是可以搞的,暂时没找到好的办法)
首先从PostgreSQL导出到文件,由于"createTime","cardName"导入到mongoDB是string类型,所以使用"括起来,结果还是很悲剧,mongoimport不认为这样会是string。凡是看起来是数字的都变成了数字.
copy (select skyid as "skyId",real_amount as "realAmount",pay_type as "payType",to_char(create_time,'yyyymmddhh24miss') as "createTime",card_type as "cardType",card_name as "cardName",app_id as "appId",amount from tbl_digoal) to '/home/postgres/to_mongo.csv' with csv HEADER quote '"' force quote "createTime","cardName";
取出一条作为范例:(下面一行范例中createTime已经使用双引号了,cardName为空)
99999999,0,1,"20101111230027",99,,999999,9999
导入的写法如下:
mongoimport -h 127.0.0.1:5281 -d 库名 -c collection名 -u 用户 -p 密码 -f skyId,realAmount,payType,createTime,cardType,cardName,appId,amount --ignoreBlanks --type csv --headerline --file ./to_mongo.csv
导入后到mongoDB中查看结果如下:
{ "_id" : ObjectId("4d26fd83f96edba6b10e3e5d"), "skyId" : NumberLong(99999999), "realAmount" : 0, "payType" : 1, "createTime" : NumberLong("20101111230027"), "cardType" : 99, "appId" : 999999, "amount" : 9999 }
很明显,createTime变成NumberLong类型了.
cardName达到预期没有出现在行中.如果不使用--ignoreBlanks,那样的话会导入"cardName" : "" 的值。

2. 使用json格式导入,由于key和输入的值都是指定的,没有类型的问题了。就是从PostgreSQL到处的文件大了3倍。
下面调整一下PostgreSQL的导出SQL:
copy (select case when skyid is null then '{"skyId" : null,' else '{"skyId" : '||skyid||',' end || case when real_amount is null then '"realAmount" : null,' else '"realAmount" : '||real_amount||',' end || case when pay_type is null then '"payType" : null,' else '"payType" : '||pay_type||',' end || case when to_char(create_time,'yyyymmddhh24miss') is null then '"createTime" : null,' else '"createTime" : "'||to_char(create_time,'yyyymmddhh24miss')||'",' end || case when card_type is null then '"cardType" : null,' else '"cardType" : '||card_type||',' end || case when card_name is null then '"cardName" : null,' else '"cardName" : "'||card_name||'",' end || case when app_id is null then '"appId" : null,' else '"appId" : '||app_id||',' end || case when amount is null then '"amount" : null}' else '"amount" : '||amount||'}' end  from tbl_digoal ) to '/home/postgres/to_mongo.json' 
取出一条作为范例:(下面一行范例中createTime已经使用双引号了,cardName为空)
{"skyId" : 9999999999,"realAmount" : 0,"payType" : 1,"createTime" : "20101111230027","cardType" : 99,"cardName" : null,"appId" : 9999999,"amount" : 9999}
导入的写法如下:
mongoimport -h 127.0.0.1:5281 -d 库名 -c collection名 -u 用户名 -p 密码 --type json --file ./to_mongo.json 
导入后取出记录如下,和对应的类型一致.达到预期:
{ "_id" : ObjectId("4d26ff29f96edba6b10e3e60"), "skyId" : NumberLong("9999999999"), "realAmount" : 0, "payType" : 1, "createTime" : "20101111230027", "cardType" : 99, "cardName" : null, "appId" : 9999999, "amount" : 9999 }
> db.test.find({"skyId" : 9999999999})
{ "_id" : ObjectId("4d26ff29f96edba6b10e3e60"), "skyId" : NumberLong("9999999999"), "realAmount" : 0, "payType" : 1, "createTime" : "20101111230027", "cardType" : 99, "cardName" : null, "appId" : 9999999, "amount" : 9999 }

csv导入和json导入的区别:
1. csv导入的null可以直接把key忽略掉,而json的话显示为"$key" : null.当然你可以在json中删除掉"$key" : null这一节,那就和csv一样了.
2. 导入速度 没啥分别.

参考:
mongo@db-192-168-169-90-> mongoimport --help
options:
  --help                  produce help message
  -v [ --verbose ]        be more verbose (include multiple times for more 
                          verbosity e.g. -vvvvv)
  -h [ --host ] arg       mongo host to connect to ("left,right" for pairs)
  --port arg              server port. Can also use --host hostname:port
  -d [ --db ] arg         database to use
  -c [ --collection ] arg collection to use (some commands)
  -u [ --username ] arg   username
  -p [ --password ] arg   password
  --ipv6                  enable IPv6 support (disabled by default)
  --dbpath arg            directly access mongod database files in the given 
                          path, instead of connecting to a mongod  server - 
                          needs to lock the data directory, so cannot be used 
                          if a mongod is currently accessing the same path
  --directoryperdb        if dbpath specified, each db is in a separate 
                          directory
  -f [ --fields ] arg     comma separated list of field names e.g. -f name,age
  --fieldFile arg         file with fields names - 1 per line
  --ignoreBlanks          if given, empty fields in csv and tsv will be ignored
  --type arg              type of file to import.  default: json (json,csv,tsv)
  --file arg              file to import from; if not specified stdin is used
  --drop                  drop collection first 
  --headerline            CSV,TSV only - use first line as headers
  --upsert                insert or update objects that already exist
  --upsertFields arg      comma-separated fields for the query part of the 
                          upsert. You should make sure this is indexed
  --stopOnError           stop importing at first error rather than continuing
  --jsonArray             load a json array, not one item per line. Currently 
                          limited to 4MB.
相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。   相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
相关文章
|
3月前
|
存储 关系型数据库 Java
polardb有没有搞过pg 全量及增量备份管理的
【1月更文挑战第3天】【1月更文挑战第11篇】 polardb有没有搞过pg 全量及增量备份管理的
33 1
|
28天前
|
负载均衡 监控 关系型数据库
PostgreSQL从小白到高手教程 - 第48讲:PG高可用实现keepalived
PostgreSQL技术大讲堂 - 第48讲:PG高可用实现keepalived
64 1
|
1月前
|
存储 NoSQL 关系型数据库
一篇文章带你搞懂非关系型数据库MongoDB
一篇文章带你搞懂非关系型数据库MongoDB
55 0
|
2月前
|
SQL 关系型数据库 数据库
PostgreSQL从小白到高手教程 - 第44讲:pg流复制部署
PostgreSQL技术大讲堂 - 第44讲:pg流复制部署
54 0
|
4月前
|
SQL 关系型数据库 数据库
postgresql|数据库|pg数据库的文件系统详解---最全面的解析
postgresql|数据库|pg数据库的文件系统详解---最全面的解析
114 0
|
9月前
|
存储 JSON NoSQL
NoSql非关系型数据库之MongoDB应用(三):MongoDB在项目中的初步应用
NoSql非关系型数据库之MongoDB应用(三):MongoDB在项目中的初步应用
|
4月前
|
安全 关系型数据库 数据库
上新|阿里云RDS PostgreSQL支持PG 16版本,AliPG提供丰富自研能力
AliPG在社区版16.0的基础上,在安全、成本、可运维性等多个方面做了提升,丰富的内核/插件特性支持,满足业务场景的需求
|
4月前
|
关系型数据库 数据库 PostgreSQL
flink postgresql cdc实时同步(含pg安装配置等)
flink postgresql cdc实时同步(含pg安装配置等)
151 0
|
6月前
|
JSON Java 关系型数据库
Spring Boot 学习研究笔记(十三) Spring Data JPA与PostgreSQL的jsonb类型集成
Spring Boot 学习研究笔记(十三) Spring Data JPA与PostgreSQL的jsonb类型集成
|
7月前
|
NoSQL 关系型数据库 分布式数据库
RDS、PolarDB、Redis、MongoDB、DAS中执行一条修改语句为啥要开binlog呢
RDS、PolarDB、Redis、MongoDB、DAS中执行一条修改语句为啥要开binlog呢
113 1