`

Python 分布式文件系统 Mongodb GridFS

阅读更多
mongodb GridFS 性能
性能, 网评还不错.
不过在生产环境中,国外有用于存储视频流的.
GridFS的一个优点是可以存储上百万的文件而无需担心扩容性.
通过同步复制,可以解决分布式文件的备份问题.
通过ARP-ping可以实现一个双机热备切换,类mysql的mysql master master replic

使用Nginx module
http://github.com/mdirolf/nginx-gridfs
这是gridfs的nginx module. 可以通过nginx直接访问读取mongo gridfs中的文件.
和nginx对应的mogilefs module类似.

优点: 由于直接通过nginx,速度是最快的.
缺点: 只能通过file_path来查找,目前不支持_id来查找.因此必须在file_path上
建立索引.

其他一些信息:
1.通过runcommand可以直接在mongodb端运行处理脚本. 比如像mapreduce,或者一
些需要读取数据然后进行处理的.
这些command则是使用javascript方式来编写的,很容易. 好处就是避免了数据在服
务端和客户端之间的读取和传输,
提高效率.
2. sharding
sharding在目前开发版中已经具备,但还不成熟. 但是可以自己实现sharding比较
好.因为目前的sharding还是比较硬性的.
3.灵活使用magic操作符和upsert,比如$inc,$all,$in 等等


#!/bin/bash

安装mongodb
#mongodb 1.2.4
official
#http://www.mongodb.org/
admin
#http://www.mongodb.org/display/DOCS/Admin+Zone
manual
http://www.mongodb.org/display/DOCS/Manual
GridFS+Tools
http://www.mongodb.org/display/DOCS/GridFS+Tools
config
http://www.mongodb.org/display/DOCS/File+Based+Configuration
http://bytebucket.org/namlook/mongokit/wiki/html/gridfs.html

客户端
easy_install pymongo
API:http://api.mongodb.org/python/current/api/pymongo/
http://api.mongodb.org/python/1.4%2B/examples/gridfs.html



以下是安装脚本
mkdir mongodb
cd mongodb
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-static-legacy-1.2.4.tgz
tar xzf mongodb-linux-x86_64-static-legacy-1.2.4.tgz
cd mongodb-linux-x86_64-static-1.2.4/


#建立数据保存路径
mkdir data
cd data
mkdir db


启动服务
cd /home/bmc/mongodb/mongodb-linux-x86_64-static-1.2.4/bin/
./mongod --dbpath=/home/bmc/mongodb/data/db/

config
根据如下链接编写自己的config文件
http://www.mongodb.org/display/DOCS/File+Based+Configuration

你可以使用nginx+gridfs插件来进行访问mongodb,但是gridfs 插件需要boost,由于boost版本问题,这个东东基本安装不上或是安装很费劲,您可以使用django启动服务来代替这个复杂的插件,代码如下 :)
Views
return HttpResponse(im, mimetype="image/JPEG")


后台连接代码:
#encoding=utf-8
from pymongo import Connection
from gridfs import *
from PIL import Image
import StringIO
import threading, time

#文件处理系统
class GFS:
    #定义connection and fs
    c = None
    db = None
    fs = None
    instance = None
    locker = threading.Lock()
    
    #初始化
    def __init__(self):
        print "__init__"
        GFS._connect()
        print "server info " + " * " * 40
        print GFS.c.server_info
    
    #获得单列对象
    @staticmethod
    def getInstance():
        GFS.locker.acquire()
        try:
            GFS.instance
            if not GFS.instance:
                GFS.instance = GFS()
            return GFS.instance
        finally:
            GFS.locker.release()

    #写入
    def put(self,name,image,format="png",mime="image"):
        gf = None
        data = None
        try:
            data = StringIO.StringIO()
            image.save(data,format)
            data.getvalue()
            name = "%s.%s" % (name,format)
            print "name is %s" % name
            gf = GFS.fs.open(name,"w")
            gf.content_type = "%s/%s" % (mime,format)
            gf.write(data.getvalue())
        finally:
            try:
                gf.close()
                data.close()
            finally:
                GFS.c = None
                GFS._connect()
    
    #获得图片
  def get(self,name):
        gf = None
        try:
            gf  = GFS.fs.open(name,"r")
            print gf
            im = gf.read()
            dic = {}        
            dic["chunk_size"] =  gf.chunk_size
            dic["metadata"] = gf.metadata
            dic["mode"] = gf.mode
            dic["length"] = gf.length
            dic["upload_date"] = gf.upload_date
            dic["name"] = gf.name
            dic["content_type"] = gf.content_type
            return (im , dic)
        except Exception,e:
            print e
            return (None,None)
        finally:
                if gf:
                    if not gf.closed:
                        gf.close()


    #获得文件列表
    def list(self):
        return GFS.fs.list()

    #删除文件
    def remove(self,name):
        GFS.fs.remove(name)
        
    @staticmethod
    def _connect():
        if  not GFS.c:
            GFS.c = Connection("*********",27017)
            GFS.db = GFS.c['imagesdb']
            GFS.fs = GridFS(GFS.db)

分享到:
评论
4 楼 Sasoritattoo 2014-02-07  
GFS.fs.remove(name)新版本不支持这个接口了,改用delete(),并且

print gfs.delete.__doc__
Delete a file from GridFS by ``"_id"``.

        Removes all data belonging to the file with ``"_id"``:
        `file_id`.

        .. warning:: Any processes/threads reading from the file while
           this method is executing will likely see an invalid/corrupt
           file. Care should be taken to avoid concurrent reads to a file
           while it is being deleted.

        .. note:: Deletes of non-existent files are considered successful
           since the end result is the same: no file with that _id remains.

        :Parameters:
          - `file_id`: ``"_id"`` of the file to delete

        .. versionadded:: 1.6

3 楼 mimicom 2012-09-05  
没看懂........
2 楼 wxmfly 2010-04-17  
写的不错,学习了,希望继续深入分享相关主题。
1 楼 fire01312 2010-04-06  
没看懂,有空也研究下 MongoDB!

相关推荐

Global site tag (gtag.js) - Google Analytics