MongoDB05-GridFS

1、GridFS简介

  • MongoDB为了性能将文档大小的上限设置为16MB。如果要存储超过16MB的文档,就需要使用GridFS规范。
    • 因为,即使仅请求文档中的一小部分字段,MongoDB仍然需要把整个文档加载到内存中。
    • 使用GridFS可以轻松存储大文件,并且可以只访问部分文件,而不是读取完整的文件,从而保持高性能。
  • GridFS并不是一个真正的软件特性。
    • MongoDB中并没有任何专门用于管理GridFS的服务器端代码(尽管有一些辅助函数便于编写GridFS驱动)。相反,GridFS是由MongoDB的所有驱动使用的一个简单规范。这样做的最大优势在于,由一个驱动存储的文件可以被另一个使用了相同规范的驱动使用。
    • 它可以手动操作,因为GridFS规范中的MongoDB文件只是一些包含文档的普通集合
  • GridFS由两部分(集合)组成
    • 一个集合存储文件名和诸如大小这样的相关信息(称为元数据),另一个集合保存文件数据自身,以255KB为一块。该规范将这两个集合分别称为files和chunks。
    • 默认情况下,files和chunks集合在fs名称空间中。可以使用不同的名称空间,将不同类型的文件分开存储,例如可将图像和电影文件分开存储。

2、手动操作GridFS

  • 可以通过mongofiles命令操作GridFS

2.1、mongofiles命令

]# mongofiles --help
Usage:
  mongofiles <options> <connection-string> <command> <filename or _id>
Possible commands include:
	list      - 列出所有文件;'filename'是一个可选的前缀,列出的文件名必须以它开头
	search    - 搜索所有文件;'filename'是一个正则表达式,列出的文件名必须匹配
	put       - 添加在支持参数中指定文件名的文件
	put_id    - 添加一个文件,文件名'filename'和给定的'_id'
	get       - 获取在支持参数中指定文件名的文件
	get_id    - 获取一个带有给定'_id'的文件
	get_regex - 获取与提供的'regex'匹配的文件
	delete    - 删除所有文件名为“filename”的文件
	delete_id - 删除带有给定'_id'的文件

See http://docs.mongodb.com/database-tools/mongofiles/ for more information.

general options:
      --help                                      print usage
      --version                                   print the tool version and exit
      --config=                                   配置文件的路径
verbosity options:
  -v, --verbose=<level>                           更详细的日志输出(例如-vvvvv,或指定一个数值,例如--verbose=N)
      --quiet                                     隐藏所有日志输出
connection options:
  -h, --host=<hostname>                           Mongodb主机(setname/host1,host2用于复制集)
      --port=<port>                               服务器端口(can also use --host hostname:port)
ssl options:
      --ssl                                       connect to a mongod or mongos that has ssl enabled
      --sslCAFile=<filename>                      the .pem file containing the root certificate chain from the certificate authority
      --sslPEMKeyFile=<filename>                  the .pem file containing the certificate and key
      --sslPEMKeyPassword=<password>              the password to decrypt the sslPEMKeyFile, if necessary
      --sslCRLFile=<filename>                     the .pem file containing the certificate revocation list
      --sslFIPSMode                               use FIPS mode of the installed openssl library
      --tlsInsecure                               bypass the validation for server's certificate chain and host name
authentication options:
  -u, --username=<username>                       用户名进行身份验证
  -p, --password=<password>                       密码进行身份验证
      --authenticationDatabase=<database-name>    保存用户凭证的数据库
      --authenticationMechanism=<mechanism>       要使用的身份验证机制
      --awsSessionToken=<aws-session-token>       会话令牌通过AWS IAM进行认证
kerberos options:
      --gssapiServiceName=<service-name>          在使用GSSAPI/Kerberos进行身份验证时使用的服务名(default: mongodb)
      --gssapiHostName=<host-name>                在使用GSSAPI/Kerberos进行身份验证时使用的主机名(default: <remote server's address>)
uri options:
      --uri=mongodb-uri                           Mongodb uri连接字符串
storage options:
  -d, --db=<database-name>                        数据库使用
  -l, --local=<filename>                          put|get的本地文件名
  -t, --type=                                     put的内容/MIME类型(可选)
  -r, --replace                                   在放置后删除其他同名的文件
      --prefix=<prefix>                           使用GridFS前缀
      --writeConcern=<write-concern>              写相关的选项 e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}'
      --regexOptions=<regex-options>              用于get_regex的正则表达式选项
query options:
      --readPreference=<string>|<json>            指定首选模式(例如:'nearest')或首选项json对象 (e.g.'{mode: "nearest", tagSets: [{a: "b"}], maxStalenessSeconds: 123}')

2.2、使用mongofiles命令操作GridFS

1、上传文件

  • 如果基于文件名上传两个同名的文件,MongoDB不会选择更新已有文档,而是创建一个新的文档(除了_id键外其他都相同)。
  • 上传一个本地文件到mongodb
//创建一个36MB的文件hello.txt
]# dd if=/dev/zero of=hello.txt bs=1M count=36
记录了36+0 的读入
记录了36+0 的写出
37748736字节(38 MB)已复制,0.129464 秒,292 MB/秒
//查看hello.txt文件
]# ls -lh
总用量 84M
-rw-r--r--  1 root root  36M 9月  13 15:13 hello.txt

//将hello.txt文件上传到mongodb
]# mongofiles -h 10.1.1.11:27017 put hello.txt 
2022-09-13T15:19:04.500+0800	connected to: mongodb://10.1.1.11:27017/
2022-09-13T15:19:04.500+0800	adding gridFile: hello.txt
2022-09-13T15:19:05.028+0800	added gridFile: hello.txt
//查看hello.txt文件
]# mongofiles -h 10.1.1.11:27017 list
2022-09-13T15:19:18.420+0800	connected to: mongodb://10.1.1.11:27017/
hello.txt	37748736

2、查看文件

  • 在上传一个文件wwwhello.txt。
]# cp hello.txt wwwhello.txt
]# mongofiles -h 10.1.1.11:27017 put wwwhello.txt
  • 使用list和search子命令获取文件
//使用list子命令获取文件名以hello开头的
]# mongofiles -h 10.1.1.11:27017 list hello
2022-09-13T15:32:34.111+0800	connected to: mongodb://10.1.1.11:27017/
hello.txt	37748736

//使用searcht子命令获取文件名包含hello开头的(正则表达式)
]# mongofiles -h 10.1.1.11:27017 search hello
2022-09-13T15:32:26.191+0800	connected to: mongodb://10.1.1.11:27017/
hello.txt	37748736
wwwhello.txt	37748736

3、删除文件

  • delete子命令基于文件名删除文件。因此,如果同时有多个同名文件,那么该命令将删除所有文件。
//删除wwwhello.txt文件
]# mongofiles -h 10.1.1.11:27017 delete wwwhello.txt 
2022-09-13T15:42:01.999+0800	connected to: mongodb://10.1.1.11:27017/
2022-09-13T15:42:02.059+0800	successfully deleted all instances of 'wwwhello.txt' from GridFS

]# mongofiles -h 10.1.1.11:27017 list
2022-09-13T15:42:08.647+0800	connected to: mongodb://10.1.1.11:27017/
hello.txt	37748736

4、获取文件

]# cd test/

]# mongofiles -h 10.1.1.11:27017 get hello.txt 
2022-09-13T15:45:41.251+0800	connected to: mongodb://10.1.1.11:27017/
2022-09-13T15:45:41.442+0800	finished writing to hello.txt

[root@11 test]# ll
总用量 36864
-rw-r--r-- 1 root root 37748736 9月  13 15:45 hello.txt

2.3、查看MongoDB中的数据

//切换数据库(默认是test数据库)
> use test
switched to db test

//查看相关集合
> show collections
fs.chunks
fs.files

//查看文件的元数据
> db.fs.files.find()
{ "_id" : ObjectId("63202ee859b7b99dfe345c89"), "length" : NumberLong(37748736), "chunkSize" : 261120, "uploadDate" : ISODate("2022-09-13T07:19:05.027Z"), "filename" : "hello.txt", "metadata" : {  } }

//查看文件的数据(使用{"data": 0}不显示数据本身)
> db.fs.chunks.find({},{"data": 0})
{ "_id" : ObjectId("63202ee859b7b99dfe345c8a"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 0 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c8b"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 1 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c8c"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 2 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c8d"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 3 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c8e"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 4 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c8f"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 5 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c90"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 6 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c91"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 7 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c92"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 8 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c93"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 9 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c94"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 10 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c95"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 11 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c96"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 12 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c97"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 13 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c98"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 14 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c99"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 15 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c9a"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 16 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c9b"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 17 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c9c"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 18 }
{ "_id" : ObjectId("63202ee859b7b99dfe345c9d"), "files_id" : ObjectId("63202ee859b7b99dfe345c89"), "n" : 19 }
Type "it" for more

1

#                                                                                                                         #
posted @ 2022-09-10 01:49  麦恒  阅读(69)  评论(0编辑  收藏  举报