自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

教你如何利用MySQL學(xué)習(xí)MongoDB之導(dǎo)入和導(dǎo)出

數(shù)據(jù)庫 其他數(shù)據(jù)庫 MySQL MongoDB
在上文中,我們了解了教你如何利用MySQL學(xué)習(xí)MongoDB之授權(quán)和權(quán)限,本文中我們繼續(xù)我們的學(xué)習(xí)之旅,學(xué)習(xí)兩者的導(dǎo)入和導(dǎo)出。

在上文中,我們了解了教你如何利用MySQL學(xué)習(xí)MongoDB之授權(quán)和權(quán)限,本文中我們繼續(xù)我們的學(xué)習(xí)之旅,學(xué)習(xí)兩者的導(dǎo)入和導(dǎo)出。

1、MySQL導(dǎo)入和導(dǎo)出

(1)、mysqlimport

此工具位于mysql/bin目錄中,是MySQL的一個(gè)載入(或者說導(dǎo)入)數(shù)據(jù)的一個(gè)非常有效的工具。這是一個(gè)命令行工具。有兩個(gè)參數(shù)以及大量的選項(xiàng)可供選擇。這個(gè)工具把一個(gè)文本文件(text file)導(dǎo)入到你指定的數(shù)據(jù)庫和表中。比方說我們要從文件student.txt中把數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫class中的表 student中:

mysqlimport class.student student.txt

(2)、load data infile

這個(gè)命令與mysqlimport非常相似,但這個(gè)方法可以在MySQL命令行中使用。 如mysqlimport工具一樣,這個(gè)命令也有一些可以選擇的參數(shù)。比如您需要把自己的電腦上的數(shù)據(jù)導(dǎo)入到遠(yuǎn)程的數(shù)據(jù)庫服務(wù)器中,您可以使用下面的命令:

Load data local infile "d:\student.txt" into table student;

上面的local參數(shù)表示文件是本地的文件,服務(wù)器是您所登陸的服務(wù)器。這樣就省去了使用ftp來上傳文件到服務(wù)器,mysql替你完成了。

(3)、mysqldump

mysqldump工具很多方面類似相反作用的工具mysqlimport。它們有一些同樣的選項(xiàng)。但mysqldump能夠做更多的事情。它可以把整個(gè)數(shù)據(jù)庫裝載到一個(gè)單獨(dú)的文本文件中。這個(gè)文件包含有所有重建您的數(shù)據(jù)庫所需要的SQL命令。這個(gè)命令取得所有的模式并且將其轉(zhuǎn)換成DDL語法,取得所有的數(shù)據(jù),并且從這些數(shù)據(jù)中創(chuàng)建INSERT語句。這個(gè)工具將您的數(shù)據(jù)庫中所有的設(shè)計(jì)倒轉(zhuǎn)。因?yàn)樗械臇|西都被包含到了一個(gè)文本文件中。這個(gè)文本文件可以用一個(gè)簡單的批處理和一個(gè)合適SQL語句導(dǎo)回到MySQL中。這個(gè)工具令人難以置信地簡單而快速。決不會(huì)有半點(diǎn)讓人頭疼地地方。因此,如果您像裝載整個(gè)數(shù)據(jù)庫mydb的內(nèi)容到一個(gè)文件中,可以使用下面的命令:

bin/mysqldump –p mydb > mydb.txt

2、MongoDB導(dǎo)入和導(dǎo)出

(1)、mongoexport導(dǎo)出工具

MongoDB提供了mongoexport工具,可以把一個(gè)collection導(dǎo)出成json格式或csv格式的文件。可以指定導(dǎo)出哪些數(shù)據(jù)項(xiàng),也可以根據(jù)給定的條件導(dǎo)出數(shù)據(jù)。工具幫助信息如下:

  1. [root@localhost bin]# ./mongoexport --help  
  2. options:  
  3. --help produce help message   
  4. -v [ --verbose ] be more verbose (include multiple times for more   
  5. verbosity e.g. -vvvvv)   
  6. -h [ --host ] arg mongo host to connect to ( /s1,s2 for sets)   
  7. --port arg server port. Can also use --host hostname:port   
  8. --ipv6 enable IPv6 support (disabled by default)   
  9. -u [ --username ] arg username   
  10. -p [ --password ] arg password   
  11. --dbpath arg directly access mongod database files in the given   
  12. path, instead of connecting to a mongod server -   
  13. needs to lock the data directory, so cannot be used   
  14. if a mongod is currently accessing the same path   
  15. --directoryperdb if dbpath specified, each db is in a separate   
  16. directory   
  17. -d [ --db ] arg database to use   
  18. -c [ --collection ] arg collection to use (some commands)   
  19. -f [ --fields ] arg comma separated list of field names e.g. -f name,age   
  20. --fieldFile arg file with fields names - 1 per line   
  21. -q [ --query ] arg query filter, as a JSON string   
  22. --csv export to csv instead of json   
  23. -o [ --out ] arg output file; if not specified, stdout is used   
  24. --jsonArray output to a json array rather than one object per   
  25. line   
  26. [root@localhost bin]#  

 

下面我們將以一個(gè)實(shí)際的例子說明,此工具的用法:

將foo庫中的表t1導(dǎo)出成json格式:

  1. [root@localhost bin]# ./mongoexport -d foo -c t1 -o /data/t1.json   
  2. connected to: 127.0.0.1   
  3. exported 1 records   
  4. [root@localhost bin]#  

 

 

導(dǎo)出成功后我們看一下/data/t1.json文件的樣式,是否是我們所希望的:

  1. [root@localhost data]# more t1.json   
  2. "_id" : { "$oid" : "4f927e2385b7a6814a0540a0" }, "age" : 2 }   
  3. [root@localhost data]#  

 

 

通過以上說明導(dǎo)出成功,但有一個(gè)問題,要是異構(gòu)數(shù)據(jù)庫的遷移怎么辦呢?例如我們要將MongoDB的數(shù)據(jù)導(dǎo)入到MySQL該怎么辦呢?MongoDB提供了一種csv的導(dǎo)出格式,就可以解決異構(gòu)數(shù)據(jù)庫遷移的問題了. 下面將foo庫的t2表的age和name列導(dǎo)出, 具體如下:

  1. [root@localhost bin]# ./mongoexport -d foo -c t2 --csv -f age,name -o /data/t2.csv   
  2. connected to: 127.0.0.1   
  3. exported 1 records   
  4. [root@localhost bin]#  

 

 

查看/data/t2.csv的導(dǎo)出結(jié)果:

  1. [root@localhost data]# more t2.csv   
  2. age,name   
  3. 1,"wwl"   
  4. [root@localhost data]#  

 

 

可以看出MongoDB為我們提供了一個(gè)強(qiáng)在的數(shù)據(jù)導(dǎo)出工具。

(2)、mongoimport導(dǎo)入工具

MongoDB提供了mongoimport工具,可以把一個(gè)特定格式文件中的內(nèi)容導(dǎo)入到某張collection中。工具幫助信息如下:

  1. [root@localhost bin]# ./mongoimport --help   
  2. options:   
  3. --help produce help message   
  4. -v [ --verbose ] be more verbose (include multiple times for more   
  5. verbosity e.g. -vvvvv)   
  6. -h [ --host ] arg mongo host to connect to ( /s1,s2 for sets)   
  7. --port arg server port. Can also use --host hostname:port   
  8. --ipv6 enable IPv6 support (disabled by default)   
  9. -u [ --username ] arg username   
  10. -p [ --password ] arg password   
  11. --dbpath arg directly access mongod database files in the given   
  12. path, instead of connecting to a mongod server -   
  13. needs to lock the data directory, so cannot be used   
  14. if a mongod is currently accessing the same path   
  15. --directoryperdb if dbpath specified, each db is in a separate   
  16. directory   
  17. -d [ --db ] arg database to use   
  18. -c [ --collection ] arg collection to use (some commands)   
  19. -f [ --fields ] arg comma separated list of field names e.g. -f name,age   
  20. --fieldFile arg file with fields names - 1 per line   
  21. --ignoreBlanks if given, empty fields in csv and tsv will be ignored   
  22. --type arg type of file to import. default: json (json,csv,tsv)   
  23. --file arg file to import from; if not specified stdin is used   
  24. --drop drop collection first   
  25. --headerline CSV,TSV only - use first line as headers   
  26. --upsert insert or update objects that already exist   
  27. --upsertFields arg comma-separated fields for the query part of the   
  28. upsert. You should make sure this is indexed   
  29. --stopOnError stop importing at first error rather than continuing   
  30. --jsonArray load a json array, not one item per line. Currently   
  31. limited to 4MB.  

 

 

下面我們將以一人實(shí)際的例子說明,此工具的用法:

先看一下foo庫中的t1表數(shù)據(jù):

  1. > db.t1.find();   
  2. "_id" : ObjectId("4f937a56450beadc560feaa9"), "age" : 5 }   
  3. >  

 

 

t1其中有一條age=5的記錄, 我們?cè)倏匆幌耲son文件中的數(shù)據(jù)是什么樣子的:

  1. [root@localhost data]# more t1.json   
  2. "_id" : { "$oid" : "4f937a56450beadc560feaa7" }, "age" : 8 }   
  3. [root@localhost data]#  

 

 

可以看到t1.json文件中有一條age=8的數(shù)據(jù),下面我們將用mongoimport工具將json文件中的記錄導(dǎo)入到t1表中:

  1. [root@localhost bin]# ./mongoimport -d foo -c t1 /data/t1.json   
  2. connected to: 127.0.0.1   
  3. imported 1 objects  

 

 

工具返回信息說明向表中插入了一條記錄. 我們進(jìn)庫里實(shí)際驗(yàn)證一下:

  1. [root@localhost bin]# ./mongo   
  2. MongoDB shell version: 1.8.1   
  3. connecting to: test   
  4. > use foo   
  5. switched to db foo   
  6. > db.t1.find();   
  7. "_id" : ObjectId("4f937a56450beadc560feaa9"), "age" : 5 }   
  8. "_id" : ObjectId("4f937a56450beadc560feaa7"), "age" : 8 }   
  9. >  

【編輯推薦】

  1. 教你如何利用MySQL學(xué)習(xí)MongoDB之SQL語法
  2. 教你如何利用MySQL學(xué)習(xí)MongoDB之?dāng)?shù)據(jù)存儲(chǔ)結(jié)構(gòu)
  3. 如何解決PHP+MySQL出現(xiàn)亂碼的現(xiàn)象
  4. 教你如何利用MySQL學(xué)習(xí)MongoDB之安裝篇
  5. MySQL配置時(shí)提示無法連接到MySQL本地服務(wù)器
責(zé)任編輯:艾婧 來源: it168
相關(guān)推薦

2011-05-24 10:11:30

MySQLMongoDB

2011-05-24 09:23:16

MySQLMongoDB

2011-05-23 09:23:19

MySQLMongoDB

2011-05-24 09:10:24

MySQLMongoDB

2011-05-23 13:30:00

MySQLMongoDB

2012-02-21 10:10:16

2014-08-15 13:44:40

mongodb

2019-08-25 23:30:10

mysql命令mysqldump

2011-09-14 15:30:00

MongoDB

2010-06-09 10:09:39

MySQL 數(shù)據(jù)庫導(dǎo)入

2023-12-01 10:21:00

機(jī)器學(xué)習(xí)算法

2024-09-13 15:20:46

2024-12-02 14:48:30

Docker鏡像文件

2011-04-01 14:51:37

Oracle數(shù)據(jù)庫導(dǎo)入導(dǎo)出

2023-11-02 13:34:00

云計(jì)算聯(lián)合學(xué)習(xí)

2010-05-26 17:12:52

2021-10-29 10:32:45

數(shù)據(jù) Navicat方法

2020-12-23 14:18:43

JavaScript模塊導(dǎo)出

2011-04-01 14:51:37

Oracle數(shù)據(jù)庫導(dǎo)入導(dǎo)出

2010-10-28 11:55:47

oracle數(shù)據(jù)導(dǎo)出
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)