最近爬取mobike和ofo單車數據,需要存儲在csv文件中,因為設計的程序沒有寫存儲csv文件的方法,為了偷懶所以就繼續存儲到了MongoDB中。恰好MongoDB支持導出的數據可以是csv文件和json文件,所以下面介紹下MongoDB mongoexport和mongoimport方法;
一、導出工具mongoexport 簡介,通過幫助先了解下mongoexport的功能參數
[root@iZ2ze4b308vd83fulq9n7iZ ~]# mongoexport --help Usage: mongoexport <options> Export data from MongoDB in CSV or JSON format. See http://docs.mongodb.org/manual/reference/program/mongoexport/ for more information. general options: --help print usage --version print the tool version and exit verbosity options: -v, --verbose more detailed log output (include multiple times for more verbosity, e.g. -vvvvv) --quiet hide all log output connection options: -h, --host= mongodb host to connect to (setname/host1,host2 for replica sets) --port= server port (can also use --host hostname:port) authentication options: -u, --username= username for authentication -p, --password= password for authentication --authenticationDatabase= database that holds the user's credentials --authenticationMechanism= authentication mechanism to use namespace options: -d, --db= database to use -c, --collection= collection to use output options: -f, --fields= comma separated list of field names (required for exporting CSV) e.g. -f "name,age" --fieldFile= file with field names - 1 per line --type= the output format, either json or csv (defaults to 'json') -o, --out= output file; if not specified, stdout is used --jsonArray output to a JSON array rather than one object per line --pretty output JSON formatted to be human-readable querying options: -q, --query= query filter, as a JSON string, e.g., '{x:{$gt:1}}' -k, --slaveOk allow secondary reads if available (default true) --forceTableScan force a table scan (do not use $snapshot) --skip= number of documents to skip --limit= limit the number of documents to export --sort= sort order, as a JSON string, e.g. '{x:1}'
關鍵參數說明:
- -h,--host :代表遠程連接的數據庫地址,默認連接本地Mongo數據庫;
- --port:代表遠程連接的數據庫的端口,默認連接的遠程端口27017;
- -u,--username:代表連接遠程數據庫的賬號,如果設置數據庫的認證,需要指定用戶賬號;
- -p,--password:代表連接數據庫的賬號對應的密碼;
- -d,--db:代表連接的數據庫;
- -c,--collection:代表連接數據庫中的集合;
- -f, --fields:代表集合中的字段,可以根據設置選擇導出的字段;
- --type:代表導出輸出的文件類型,包括csv和json文件;
- -o, --out:代表導出的文件名;
- -q, --query:代表查詢條件;
- --skip:跳過指定數量的數據;
- --limit:讀取指定數量的數據記錄;
- --sort:對數據進行排序,可以通過參數指定排序的字段,並使用 1 和 -1 來指定排序的方式,其中 1 為升序排列,而-1是用於降序排列,如sort({KEY:1})。
注意:
當查詢時同時使用sort,skip,limit,無論位置先后,最先執行順序 sort再skip再limit。
實例:
首先查看下數據庫中的數據一共多少條,通過的命令的方式查看下我們要導出的數據信息
> db.bike.find().count() 16878865 > db.bike.find({"source":"ofo"}).limit(1) { "_id" : ObjectId("59e8c27804390e04a063159d"), "lat" : 39.9571954199, "bikeId" : "pgdAVg", "current_time" : "2017-10-19 23:19:19", "source" : "ofo", "lng" : 116.3926501736 } >
如何通過mongoexport導出"bikeId" : "pgdAVg"的數據,我導出了json和csv兩種類型的數據;
#導出類型為json,數據庫:mapdb,集合:bike 字段:bikeId,lat,lng,current_time,source ,條件為source字段為ofo第一條數據 mongoexport --port 27030 -u sa -p Expressin@0618 -d mapdb -c bike -f bikeId,lat,lng,current_time,source --type=json -o bike.csv --query='{"source":"ofo"}' --limit=1 #導出類型為csv,數據庫:mapdb,集合:bike 字段:bikeId,lat,lng,current_time,source ,條件為source字段為ofo第一條數據 mongoexport --port 27030 -u sa -p Expressin@0618 -d mapdb -c bike -f bikeId,lat,lng,current_time,source --type=csv -o bike.csv --query='{"source":"ofo"}' --limit=1
查看下結果是否是我們兩種不同的結果集;
1.導出csv類型;
2.導出json類型;
二、導入工具mongoimport 簡介,通過幫助先了解下mongoimport的功能參數
[root@iZ2ze4b308vd83fulq9n7iZ ~]# mongoimport --help Usage: mongoimport <options> <file> Import CSV, TSV or JSON data into MongoDB. If no file is provided, mongoimport reads from stdin. See http://docs.mongodb.org/manual/reference/program/mongoimport/ for more information. general options: --help print usage --version print the tool version and exit verbosity options: -v, --verbose more detailed log output (include multiple times for more verbosity, e.g. -vvvvv) --quiet hide all log output connection options: -h, --host= mongodb host to connect to (setname/host1,host2 for replica sets) --port= server port (can also use --host hostname:port) authentication options: -u, --username= username for authentication -p, --password= password for authentication --authenticationDatabase= database that holds the user's credentials --authenticationMechanism= authentication mechanism to use namespace options: -d, --db= database to use -c, --collection= collection to use input options: -f, --fields= comma separated list of field names, e.g. -f name,age --fieldFile= file with field names - 1 per line --file= file to import from; if not specified, stdin is used --headerline use first line in input source as the field list (CSV and TSV only) --jsonArray treat input source as a JSON array --type= input format to import: json, csv, or tsv (defaults to 'json') ingest options: --drop drop collection before inserting documents --ignoreBlanks ignore fields with empty values in CSV and TSV --maintainInsertionOrder insert documents in the order of their appearance in the input source -j, --numInsertionWorkers= number of insert operations to run concurrently (defaults to 1) --stopOnError stop importing at first insert/upsert error --upsert insert or update objects that already exist --upsertFields= comma-separated fields for the query part of the upsert --writeConcern= write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}' (defaults to 'majority')
關鍵參數說明:
- h,--host :代表遠程連接的數據庫地址,默認連接本地Mongo數據庫;
- --port:代表遠程連接的數據庫的端口,默認連接的遠程端口27017;
- -u,--username:代表連接遠程數據庫的賬號,如果設置數據庫的認證,需要指定用戶賬號;
- -p,--password:代表連接數據庫的賬號對應的密碼;
- -d,--db:代表連接的數據庫;
- -c,--collection:代表連接數據庫中的集合;
- -f, --fields:代表導入集合中的字段;
- --type:代表導入的文件類型,包括csv和json,tsv文件,默認json格式;
- --file:導入的文件名稱
- --headerline:導入csv文件時,指明第一行是列名,不需要導入;
實例演示:
#首先查看集合中的數據 > db.bike_bak.find() { "_id" : ObjectId("59e8c27804390e04a063159d"), "lat" : 39.9571954199, "bikeId" : "pgdAVg", "current_time" : "2017-10-19 23:19:19", "source" : "ofo", "lng" : 116.3926501736 } #刪除集合中的數據 > db.bike_bak.remove({"bikeId" : "pgdAVg"}) WriteResult({ "nRemoved" : 1 }) #查看集合是否包含數據 > db.bike_bak.find() > #導入數據 [root@iZ2ze4b308vd83fulq9n7iZ ~]# mongoimport --port 27030 -u sa -p Expressin@0618 -d mapdb -c bike_bak --type=json --file bike.csv 2017-10-25T11:59:51.020+0800 connected to: localhost:27030 2017-10-25T11:59:51.030+0800 imported 1 document #檢查數據是否導入成功 > db.bike_bak.find() { "_id" : ObjectId("59e8c27804390e04a063159d"), "bikeId" : "pgdAVg", "current_time" : "2017-10-19 23:19:19", "lat" : 39.9571954199, "lng" : 116.3926501736, "source" : "ofo" }
測試過程中發現--headerline -f bikeId 同時存在會報錯,提示不兼容,目前不知道如何處理。