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

为什么要自己做一个呢?

因为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      

猜你喜欢

转载自zjumty.iteye.com/blog/1858909