自己做一个类似mongofiles的命令行工具来操作GridFS

本文介绍了一款自定义的MongoDB文件管理工具,旨在解决MongoDB自带工具无法操作特定bucket下的文件的问题。该工具提供了包括列出、上传、下载、删除及MD5校验等功能。

为什么要自己做一个呢?

因为mongodb自带的mongofiles到现在也不能操作和查看某个bucket的下的文件, 只能查看和操作fs下的文件. mongo-java-driver中的那个CLI也不能做这个, 甚至连用户名和密码都不能指定.

 

所以不如我们自己做一个吧. 代码如下, 不多说了:

 

import com.mongodb.DB;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import com.mongodb.util.Util;

import java.io.File;
import java.security.DigestInputStream;
import java.security.MessageDigest;

/**
 * User: matianyi
 * Date: 13/05/02
 */
public class CLI {
    /**
     * Dumps usage info to stdout
     */
    private static void printUsage() {
        System.out.println("Usage : -h x.x.x.x -u userid -p password -d db -b bucketname action");
        System.out.println("  where  action is one of:");
        System.out.println("      list                      : lists all files in the store");
        System.out.println("      put filename              : puts the file filename into the store");
        System.out.println("      get filename              : gets filename1 from store");
        System.out.println("      md5 filename              : does an md5 hash on a file in the db (for testing)");
        System.out.println("      del filename              : delete filename1 from store");
    }

    private static String host = "127.0.0.1";
    private static String dbName = "test";
    private static String usr = "";
    private static String pwd = "";
    private static String bucket = "";

    private static Mongo _mongo = null;

    private static Mongo getMongo()
            throws Exception {
        if (_mongo == null) {
            _mongo = new Mongo(host);
        }
        return _mongo;
    }

    private static GridFS _gridfs;

    private static GridFS getGridFS()
            throws Exception {
        if (_gridfs == null) {
            DB db1 = getMongo().getDB(dbName);

            if (!usr.equals("")) {
                db1.authenticate(usr, pwd.toCharArray());
            }

            if (!bucket.equals("")) {
                _gridfs = new GridFS(db1, bucket);
            } else {
                _gridfs = new GridFS(db1);
            }
        }
        return _gridfs;
    }

    public static void main(String[] args) throws Exception {

        if (args.length < 1) {
            printUsage();
            return;
        }

        for (int i = 0; i < args.length; i++) {
            String s = args[i];

            if (s.equals("-d")) {
                dbName = args[i + 1];
                i++;
                continue;
            }

            if (s.equals("-u")) {
                usr = args[i + 1];
                i++;
                continue;
            }

            if (s.equals("-p")) {
                pwd = args[i + 1];
                i++;
                continue;
            }

            if (s.equals("-b")) {
                bucket = args[i + 1];
                i++;
                continue;
            }

            if (s.equals("-h")) {
                host = args[i + 1];
                i++;
                continue;
            }

            if (s.equals("help")) {
                printUsage();
                return;
            }

            if (s.equals("list")) {
                GridFS fs = getGridFS();

                System.out.printf("%-60s %-10s\n", "Filename", "Length");

                for (DBObject o : fs.getFileList()) {
                    System.out.printf("%-60s %-10d\n", o.get("filename"), ((Number) o.get("length")).longValue());
                }
                return;
            }

            if (s.equals("get")) {
                GridFS fs = getGridFS();
                String fn = args[i + 1];
                GridFSDBFile f = fs.findOne(fn);
                if (f == null) {
                    System.err.println("can't find file: " + fn);
                    return;
                }

                f.writeTo(f.getFilename());
                return;
            }

            if (s.equals("put")) {
                GridFS fs = getGridFS();
                String fn = args[i + 1];
                GridFSInputFile f = fs.createFile(new File(fn));
                f.save();
                f.validate();
                return;
            }

            if (s.equals("del")) {
                GridFS fs = getGridFS();
                String fn = args[i + 1];
                fs.remove(fn);
                return;
            }

            if (s.equals("md5")) {
                GridFS fs = getGridFS();
                String fn = args[i + 1];
                GridFSDBFile f = fs.findOne(fn);
                if (f == null) {
                    System.err.println("can't find file: " + fn);
                    return;
                }

                MessageDigest md5 = MessageDigest.getInstance("MD5");
                md5.reset();
                DigestInputStream is = new DigestInputStream(f.getInputStream(), md5);
                int read = 0;
                while (is.read() >= 0) {
                    read++;
                    int r = is.read(new byte[17]);
                    if (r < 0)
                        break;
                    read += r;
                }
                byte[] digest = md5.digest();
                System.out.println("length: " + read + " md5: " + Util.toHex(digest));
                return;
            }


            System.err.println("unknown option: " + s);
            return;
        }

    }
}

 

使用方法:

例如要查询/tmp/log 这个bucket下面的文件:

 

java -cp .;mongo-java-driver-2.9.0.jar CLI -h 127.0.0.1 -u imadmin -p imadmin -d im -b /mail/bounce list

 结果:

Filename                                                     Length    
51777c60e4b0eb5348652d68                                     2860      
51777c60e4b0eb5348652d6b                                     2845      
51777c60e4b0eb5348652d6e                                     3043      
51777c60e4b0eb5348652d71                                     3380      
51777d14e4b0eb5348652d74                                     2860      
51777d14e4b0eb5348652d77                                     3043      
51777d14e4b0eb5348652d7a                                     2845      
51777d14e4b0eb5348652d7d                                     3387      

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值