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

淺析MongoDB數(shù)據(jù)庫分布式存儲物理文件

數(shù)據(jù)庫 分布式 MongoDB
今天我們將談到的是基于MongoDB數(shù)據(jù)庫的分布式存儲物理文件,包括測試的過程,希望本文能對大家有所幫助。

我們知道了如何對關(guān)系型數(shù)據(jù)數(shù)據(jù)通過auto-sharding進行分布式數(shù)據(jù)存儲,今天介紹如何對物理文件(小文件,基本小于100K)進行分布式存儲。

接著看一下要配置的測試環(huán)境(與前一篇中類似):

 模擬2個shard服務(wù)和一個config服務(wù), 均運行在10.0.4.85機器上,只是端口不同:    

Shard1:27020

Shard2:27021

Config:27022

Mongos啟動時默認使用的27017端口

在C,D,E磁盤下分別建立如下文件夾:

mongodb\bin

mongodb\db

然后用CMD命令行依次打開相應(yīng)文件夾下的mongd文件:

c:\mongodb\bin\mongod --dbpath c:\mongodb\db\ --port 27020

d:\mongodb\bin\mongod --dbpath d:\mongodb\db\ --port 27021

e:\mongodb\bin\mongod --configsvr --dbpath e:\mongodb\db\ --port 27022   (注:config配置服務(wù)器)

啟動mongos時,默認開啟了27017端口

e:\mongodb\bin\mongos --configdb 10.0.4.85:27022

然后打開mongo:

E:\mongodb\bin>mongo   回車  (有時加端口會造成下面的addshard命令出問題)

  1.  > use admin  
  2.           switched to db admin  
  3. > db.runCommand( { addshard : "10.0.4.85:27020", allowLocal : 1, maxSize:2 , minKey:1, maxKey:10} )    
  4.  
  5. --添加sharding,maxsize單位是M,此處設(shè)置比較小的數(shù)值只為演示sharding效果  
  6.  
  7.          { "shardAdded" : "shard0000", "ok" : 1 }  
  8. > db.runCommand( { addshard : "10.0.4.85:27021", allowLocal : 1, minKey:1000} )  
  9.          { "shardAdded" : "shard0001", "ok" : 1 }    

注:如果要移除sharding,可用下面寫法

  1.  db.runCommand( { removeshard : "localhost:10000" } );  
  2.  
  3. > db.runCommand({listshards:1});   --查看shard節(jié)點列表      
  4. > config = connect("10.0.4.85:27022")  
  5. > configconfig = config.getSisterDB("config")  
  6. > dnt_mongodb=db.getSisterDB("dnt_mongodb");  
  7.           dnt_mongodb  
  8. > db.runCommand({enablesharding:"dnt_mongodb"})  
  9.           { "ok" : 1 }  
  10.      
  11. > db.printShardingStatus()    
  12.  
  1. --- Sharding Status ---  
  2.  sharding version: { "_id" : 1, "version" : 3 }  
  3.  shards:  
  4.      {  
  5.        "_id" : "shard0000",  
  6.        "host" : "10.0.4.85:27020",  
  7.        "maxSize" : NumberLong( 2 )  
  8.      }  
  9.      { "_id" : "shard0001", "host" : "10.0.4.85:27021" }  
  10.  databases:  
  11.        { "_id" : "admin", "partitioned" : false, "primary" : "config" }  
  12.        { "_id" : "dnt_mongodb", "partitioned" : true, "primary" : "shard0001" }  
  13.  
  14. > db.runCommand( { shardcollection : "dnt_mongodb.attach_gfstream.chunks", key : { files_id : 1 } } )  
  15. --此處與之前的數(shù)據(jù)存儲方式有些不同,目前shard似乎僅支持files_id  
  16.       { "collectionsharded" : "dnt_mongodb.attach_gfstream.chunks", "ok" : 1 } 

注:運行上面命令之前需要設(shè)置files_id為唯一索引[unique index]。        

創(chuàng)建完sharding和設(shè)置相應(yīng)信息后,我們加載一下測試數(shù)據(jù),我用下面代碼來讀取要本地文件,然后批量向mongodb中添加(通過循環(huán)修改文件名來添加相同大小的文件)。       

  1. /// <summary>  
  2.         /// 上傳文件到mongodb  
  3.         /// </summary>  
  4.         /// <param name="uploadDir">要上傳文件所在路徑</param>  
  5.         /// <param name="fileName">要上傳的文件名</param>  
  6.         /// <returns></returns>  
  7.         public bool UploadFile(string uploadDir, string fileName)  
  8.         {  
  9.             for (int i = 1; i < 10000; i++)  
  10.             {  
  11.                 try 
  12.                 {  
  13.                     Mongo mongo = mongoDB;  
  14.                     mongo.Connect();  
  15.                     IMongoDatabase DB = mongo["dnt_mongodb"];  
  16.  using (FileStream fileStream = new FileStream(uploadDir + fileName, FileMode.Open))  
  17.                     {  
  18.                         int nFileLen = (int)fileStream.Length;  
  19.  
  20.                         byte[] myData = new Byte[nFileLen];  
  21.                         fileStream.Read(myData, 0, nFileLen);  
  22.  
  23.                         GridFile fs = new GridFile(DB, "attach_gfstream");  
  24.                         using (GridFileStream gfs = fs.Create(fileName + i))  
  25.                         {  
  26.                             gfs.Write(myData, 0, nFileLen);  
  27.                         }  
  28.                     }  
  29.                     mongo.Disconnect();  
  30.                 }  
  31.                 catch { }           
  32.             }  
  33.             return true;  
  34.         } 

在批量添加約10000次(約10000個文件)之后,mongodb開始把sharding出來的chunk從shard0000分布到shard0001上,我們可以用下面指令來進行驗證:

> db.printShardingStatus()

  1. --- Sharding Status ---  
  2.   sharding version: { "_id" : 1, "version" : 3 }  
  3.   shards:  
  4.       {  
  5.         "_id" : "shard0000",  
  6.         "host" : "10.0.4.85:27020",  
  7.         "maxSize" : NumberLong( 2 )  
  8.       }  
  9.       { "_id" : "shard0001", "host" : "10.0.4.85:27021" }  
  10.   databases:  
  11.         { "_id" : "admin", "partitioned" : false, "primary" : "config" }  
  12.         { "_id" : "dnt_mongodb", "partitioned" : true, "primary" : "shard0000" }  
  13.  
  14.                 dnt_mongodb.attach_gfstream.chunks chunks:  
  15.                         { "files_id" : { $minKey : 1 } } -->> { "files_id" : ObjectId("4c85fd02145a9b1534010d89") } on : shard0001 { "t" : 2000, "i" : 0 }  
  16.                         { "files_id" : ObjectId("4c85fd02145a9b1534010d89") } -->> { "files_id" : ObjectId("4c85fdec145a9b0b340005a7") } on : shard0000 { "t" :3000, "i" : 1 }  
  17.                         { "files_id" : ObjectId("4c85fdec145a9b0b340005a7") } -->> { "files_id" : ObjectId("4c85fe08145a9b0b34000aaf") } on : shard0001 { "t" :3000, "i" : 4 }  
  18.                         { "files_id" : ObjectId("4c85fe08145a9b0b34000aaf") } -->> { "files_id" : ObjectId("4c85fe27145a9b0b34000fb7") } on : shard0001 { "t" :4000, "i" : 1 }  
  19.                         { "files_id" : ObjectId("4c85fe27145a9b0b34000fb7") } -->> { "files_id" : ObjectId("4c85fe43145a9b0b340014bf") } on : shard0000 { "t" :4000, "i" : 7 }  
  20.                         { "files_id" : ObjectId("4c85fe43145a9b0b340014bf") } -->> { "files_id" : ObjectId("4c85fe61145a9b0b340019c7") } on : shard0000 { "t" :4000, "i" : 8 }  
  21.                         { "files_id" : ObjectId("4c85fe61145a9b0b340019c7") } -->> { "files_id" : ObjectId("4c85fe7b145a9b0b34001ecf") } on : shard0000 { "t" :5000, "i" : 1 }  
  22.                         { "files_id" : ObjectId("4c85fe7b145a9b0b34001ecf") } -->> { "files_id" : ObjectId("4c85fe9a145a9b0b340023d7") } on : shard0001 { "t" :5000, "i" : 4 }  
  23.                         { "files_id" : ObjectId("4c85fe9a145a9b0b340023d7") } -->> { "files_id" : ObjectId("4c85feb7145a9b0b340028df") } on : shard0001 { "t" :6000, "i" : 1 }  
  24.                         { "files_id" : ObjectId("4c85feb7145a9b0b340028df") } -->> { "files_id" : ObjectId("4c85feea145a9b0b340032ef") } on : shard0000 { "t" :6000, "i" : 4 }  
  25.                         { "files_id" : ObjectId("4c85feea145a9b0b340032ef") } -->> { "files_id" : ObjectId("4c85ff25145a9b0b34003cff") } on : shard0000 { "t" :7000, "i" : 1 }  
  26.                         { "files_id" : ObjectId("4c85ff25145a9b0b34003cff") } -->> { "files_id" : ObjectId("4c85ff57145a9b0b3400470f") } on : shard0001 { "t" :7000, "i" : 4 }  
  27.                         { "files_id" : ObjectId("4c85ff57145a9b0b3400470f") } -->> { "files_id" : ObjectId("4c85ff87145a9b0b3400511f") } on : shard0001 { "t" :8000, "i" : 1 }  
  28.                         { "files_id" : ObjectId("4c85ff87145a9b0b3400511f") } -->> { "files_id" : ObjectId("4c85ffcd145a9b0b34005b2f") } on : shard0000 { "t" :8000, "i" : 16 }  
  29.                         { "files_id" : ObjectId("4c85ffcd145a9b0b34005b2f") } -->> { "files_id" : ObjectId("4c85fff7145a9b0b3400653f") } on : shard0000 { "t" :8000, "i" : 17 }  
  30.                         { "files_id" : ObjectId("4c85fff7145a9b0b3400653f") } -->> { "files_id" : ObjectId("4c860021145a9b0b34006f4f") } on : shard0000 { "t" :8000, "i" : 18 }  
  31.                         { "files_id" : ObjectId("4c860021145a9b0b34006f4f") } -->> { "files_id" : ObjectId("4c86004f145a9b0b3400795f") } on : shard0000 { "t" :8000, "i" : 19 }  
  32.                         { "files_id" : ObjectId("4c86004f145a9b0b3400795f") } -->> { "files_id" : ObjectId("4c860080145a9b0b3400836f") } on : shard0000 { "t" :9000, "i" : 1 }  
  33.                         { "files_id" : ObjectId("4c860080145a9b0b3400836f") } -->> { "files_id" : ObjectId("4c8600b5145a9b0b34008d7f") } on : shard0001 { "t" :9000, "i" : 7 }  
  34.                         { "files_id" : ObjectId("4c8600b5145a9b0b34008d7f") } -->> { "files_id" : ObjectId("4c860115145a9b0b3400a183") } on : shard0001 { "t" :9000, "i" : 8 }  
  35.                         { "files_id" : ObjectId("4c860115145a9b0b3400a183") } -->> { "files_id" : ObjectId("4c860198145a9b0b3400b587") } on : shard0001 { "t" :10000, "i" : 1 }  
  36.                         { "files_id" : ObjectId("4c860198145a9b0b3400b587") } -->> { "files_id" : ObjectId("4c8601fc145a9b0b3400c98b") } on : shard0000 { "t" :10000, "i" : 11 }  
  37.                         { "files_id" : ObjectId("4c8601fc145a9b0b3400c98b") } -->> { "files_id" : ObjectId("4c86025b145a9b0b3400dd8f") } on : shard0000 { "t" :10000, "i" : 12 }  
  38.                         { "files_id" : ObjectId("4c86025b145a9b0b3400dd8f") } -->> { "files_id" : ObjectId("4c8602ca145a9b0b3400f193") } on : shard0000 { "t" :10000, "i" : 13 }  
  39.                         { "files_id" : ObjectId("4c8602ca145a9b0b3400f193") } -->> { "files_id" : ObjectId("4c860330145a9b0b34010597") } on : shard0000 { "t" :10000, "i" : 14 }  
  40.                         { "files_id" : ObjectId("4c860330145a9b0b34010597") } -->> { "files_id" : { $maxKey : 1 } } on : shard0000 { "t" : 10000, "i" : 15 } 

當前,綜合比較,發(fā)現(xiàn)還是chunks的值要遠大于files集合所占用的磁盤空間(前者存儲文件二進制流信息,后者存儲結(jié)構(gòu)化數(shù)據(jù)信息(如文件名稱大小等):

      

  磁盤信息比較    

    

  

下面是一個測試,用于讀寫shard0001(注意不是shard0000)上的圖片數(shù)據(jù),因為mongos可以很好的管理sharding下各分區(qū)下的數(shù)據(jù)chunk,所以我們只要告訴它要取的文件名稱即可:)

比如要獲取"2010\09\07\2\2856090617370.gif6243"這個文件(帶日期型文件路徑只是一種格式,因為我們的產(chǎn)品會將上傳的附件放到相應(yīng)磁盤目錄下,這種帶路徑的命名方式會方便與磁盤路徑進行對應(yīng)),其目前位于shard0001中,我們只要通過下面html代碼即可獲取圖文件信息:      

  1. <img src="getfile.aspx?filename=2010\09\07\2\2856090617370.gif6243"  width="30" /> 

相應(yīng)的getfile.aspx.cs 代碼參見如下:       

  1. public partial class getfile : System.Web.UI.Page  
  2.     {  
  3.  public Mongo Mongo { getset; }  
  4. public IMongoDatabase DB  
  5.         {  
  6.             get 
  7.             {  
  8.                 return this.Mongo["dnt_mongodb"];  
  9.             }  
  10.         }  
  11.  
  12.         /// <summary>  
  13. /// Sets up the test environment.  
  14. You can either override this OnInit to add custom initialization.  
  15.         /// </summary>  
  16.         public virtual void Init()  
  17.         {  
  18. string ConnectionString = "Server=10.0.4.85:27017;ConnectTimeout=30000;ConnectionLifetime=300000;
  19. MinimumPoolSize=512;MaximumPoolSize=51200;Pooled=true";  
  20. if (String.IsNullOrEmpty(ConnectionString))  
  21. throw new ArgumentNullException("Connection string not found.");  
  22.             this.Mongo = new Mongo(ConnectionString);  
  23.             this.Mongo.Connect();    
  24.         }   
  25.  
  26.         protected void Page_Load(object sender, EventArgs e)  
  27.         {  
  28.             if (!string.IsNullOrEmpty(Request.QueryString["filename"]))  
  29.             {  
  30.                 string filename = Request.QueryString["filename"];  
  31.                 Init();  
  32.                 String filesystem = "attach_gfstream";  
  33.  
  34.                 GridFile fs = new GridFile(DB, filesystem);  
  35.                 GridFileStream gfs = fs.OpenRead(filename);  
  36.  
  37.                 Byte[] buffer = new Byte[gfs.Length];  
  38.  
  39. //下面的Expires和Cache-Control設(shè)置主要用于squid反向加速
  40. 更多內(nèi)容參見  http://www.cnblogs.com/daizhj/archive/2010/08/19/1803454.html  
  41.                 HttpContext.Current.Response.AddHeader("Expires", DateTime.Now.AddDays(20).ToString("r"));  
  42.                 HttpContext.Current.Response.AddHeader("Cache-Control""public");  
  43.     
  44.                 // 需要讀的數(shù)據(jù)長度  
  45.                 long dataToRead = gfs.Length;  
  46.                 int length;  
  47.                 while (dataToRead > 0)  
  48.                 {  
  49.                     // 檢查客戶端是否還處于連接狀態(tài)  
  50.                     if (HttpContext.Current.Response.IsClientConnected)  
  51.                     {  
  52.                         length = gfs.Read(buffer, 0, 10000);  
  53.                         HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);  
  54.                         HttpContext.Current.Response.Flush();  
  55.                         buffer = new Byte[10000];  
  56.                         dataToRead = dataToRead - length;  
  57.                     }  
  58.                     else 
  59.                     {  
  60.                         // 如果不再連接則跳出死循環(huán)  
  61.                         dataToRead = -1;  
  62.                     }  
  63.                 }  
  64.                 gfs.Dispose();  
  65.                 this.Mongo.Disconnect();  
  66.                 HttpContext.Current.Response.End();           
  67.             }  
  68.         }  
  69.     } 

當然,上面只是對chunks進行sharding,如果要對files集合分片時,可以用下面命令行:            

  1. > db.runCommand( { shardcollection : "dnt_mongodb.attach_gfstream.files", key : { _id : 1 } } )    
  2. { "collectionsharded" : "dnt_mongodb.attach_gfstream.files", "ok" : 1 } 

在我添加了近50萬記錄后,mongos開始將新的文件信息保存到shard0001上,如下圖:

              

可以使用如下命令行來查看 shard上的信息:

  1. > db.printShardingStatus()  
  2.  .../省略之前files_id的shard信息  
  3.  { "filename" : { $minKey : 1 } } -->> { "filename" : "2010\\09\\08\\2\\1393993713076.gif1" } on : shard0000 { "t" : 1000, "i" : 6 }  
  4.  { "filename" : "2010\\09\\08\\2\\1393993713076.gif1" } -->> { "filename" : "2010\\09\\08\\2\\2396571814760.gif9999" } on : shard0000 { "t" : 1000, "i" : 7 }  
  5.  { "filename" : "2010\\09\\08\\2\\2396571814760.gif9999"} -->> { "filename" : "2010\\09\\08\\2\\2819270318096.gif25366" } on : shard0000 { "t" : 2000, "i" : 2 }  
  6.  { "filename" : "2010\\09\\08\\2\\2819270318096.gif25366" } -->> { "filename" : "2010\\09\\08\\2\\3100748419355.gif999" } on : shard0000{ "t" : 2000, "i" : 3 }  
  7.  { "filename" : "2010\\09\\08\\2\\3100748419355.gif999" } -->> { "filename" : { $maxKey : 1 } } on : shard0001 { "t" : 2000, "i" : 0 } 

下面是mongos上進行sharding時的信息:    

  1.  Wed Sep 08 17:25:44 [conn5] ns: dnt_mongodb.attach_gfstream.files ClusteredCursor::query ShardConnection had to change attempt: 0  
  2.      Wed Sep 08 17:32:34 [conn6] ns: dnt_mongodb.attach_gfstream.files ClusteredCursor::query ShardConnection had to change attempt: 0  
  3.      Wed Sep 08 17:38:49 [conn55] autosplitting dnt_mongodb.attach_gfstream.chunks size: 188884488 shard: ns:dnt_mongodb.attach_gfstream.chunks at: shard0001:10.0.4.85:27021 lastmod: 11|3 min: { files_id: ObjectId('4c8755b3145a9b16d41d5dc9') } m  
  4. ax: { files_id: MaxKey } on: { files_id: ObjectId('4c8759a5145a9b16d42300d7') }(splitThreshold 188743680)  
  5.      Wed Sep 08 17:38:49 [conn55] config change: { _id: "4_85-2010-09-08T09:38:49-10", server: "4_85", time: new Date(1283938729648), what: "split", ns: "dnt_mongodb.attach_gfstream.chunks", details: { before: { min: { files_id: ObjectId('4c8755  
  6. b3145a9b16d41d5dc9') }, max: { files_id: MaxKey } }, left: { min: { files_id: ObjectId('4c8755b3145a9b16d41d5dc9') }, max: { files_id: ObjectId('4c8759a5145a9b16d42300d7') } }, right: { min: { files_id: ObjectId('4c8759a5145a9b16d42300d7')  
  7. }, max: { files_id: MaxKey } } } }  
  8.      Wed Sep 08 17:38:49 [conn98] ns: dnt_mongodb.attach_gfstream.chunks ClusteredCursor::query ShardConnection had to change attempt: 0 

好了,今天的文章就先到這里了。

原文標題:基于Mongodb分布式存儲物理文件

鏈接:http://www.cnblogs.com/daizhj/archive/2010/09/08/1821481.html

【編輯推薦】

  1. MongoDB CEO談NoSQL的大數(shù)據(jù)量處理能力
  2. 拋棄關(guān)系數(shù)據(jù)庫 PHP程序員應(yīng)了解MongoDB的五件事
  3. MongoDB,無模式文檔型數(shù)據(jù)庫簡介
  4. 關(guān)系數(shù)據(jù)庫的末日是否已經(jīng)來臨
  5. 扔掉沉沒成本 嘗試關(guān)系數(shù)據(jù)庫替代品OODBMS  
責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2019-06-10 14:31:24

MySQL存儲數(shù)據(jù)庫

2012-05-10 10:49:41

MongoDB

2021-11-08 10:52:02

數(shù)據(jù)庫分布式技術(shù)

2011-03-18 08:51:23

MongoDB分布式文檔

2023-09-11 11:22:22

分布式數(shù)據(jù)庫數(shù)據(jù)庫

2013-04-26 16:18:29

大數(shù)據(jù)全球技術(shù)峰會

2021-12-20 15:44:28

ShardingSph分布式數(shù)據(jù)庫開源

2023-03-26 12:43:31

數(shù)據(jù)庫KeyValue

2023-12-05 07:30:40

KlustronBa數(shù)據(jù)庫

2014-06-30 14:20:05

NoSQL數(shù)據(jù)庫

2009-06-19 15:28:31

JDBC分布式事務(wù)

2023-08-22 13:16:00

分布式數(shù)據(jù)庫架構(gòu)數(shù)據(jù)存儲

2018-01-02 20:00:28

數(shù)據(jù)庫MySQL分布式存儲

2009-12-31 09:51:59

BeansDB鍵值存儲

2017-10-17 08:33:31

存儲系統(tǒng)分布式

2024-03-11 08:57:02

國產(chǎn)數(shù)據(jù)庫證券

2023-11-14 08:24:59

性能Scylla系統(tǒng)架構(gòu)

2023-12-18 11:21:40

MongoDB數(shù)據(jù)庫

2022-03-10 06:36:59

分布式數(shù)據(jù)庫排序

2011-11-29 09:49:16

數(shù)據(jù)庫其他數(shù)據(jù)庫NoSQL
點贊
收藏

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