支持MongoDB环境的例程和类
jaraco.mongodb的Python项目详细描述
迁移管理器
jaraco.mongodb.migration按功能实现迁移管理器 在MongoWorld 2016演示文稿上 From the Polls to the Trolls。使用 它将各种模式版本的文档加载到 您的应用程序需要。
会话
jaraco.mongodb.sessions实现由 MongoDB。
默认情况下,会话存储将处理与任何可以 自然地插入到MongoDB集合中。
为了支持更丰富的对象,可以将编解码器配置为使用jaraco.modb。
字段
jaraco.mongodb.fields提供了两个函数,encode和decode 获取任意Unicode文本并将其转换为适合作为键的值 在MongoDB的旧版本上,通过反斜杠转义这些值。
监视器索引创建
要监视服务器中正在进行的索引操作,只需调用:
python -m jaraco.mongodb.monitor-index-creation mongodb://host/db
移动网格
要将文件从一个gridfs集合移动到另一个gridfs集合,请调用:
python -m jaraco.mongodb.move-gridfs –help
并遵循移动所有或部分gridfs文件和 之后可以选择删除文件。
oplog
这个包提供了一个oplog模块,它基于 mongooplog-alt项目, 它本身就是official mongooplog utility的Python翻版, 从版本2.2开始随MongoDB提供,在3.2中已弃用。 它读取遥控器的oplog 服务器,并将操作应用于本地服务器。这个可以用来保存 独立副本集以与副本集几乎相同的方式疯狂同步 正在同步,并且可能 在各种备份和迁移场景中非常有用。
oplog实现了官方实用程序的基本功能,并且 添加以下功能:
- 可裁剪的oplog阅读器:永远运行轮询新的oplog事件 对于保持两个独立副本集几乎实时同步很有用。
- 仅同步选定数据库/集合的选项。
- 从中排除一个或多个命名空间(即dbs或集合)的选项 正在同步。
- 动态“重命名”数据库/集合的能力,即目标命名空间可以 与原来的不同。此功能适用于MongoDB 1.8及更高版本。 官方实用程序仅支持2.2.x及更高版本。
- 将上次处理的时间戳保存到文件,稍后从保存点继续。
以模块脚本的形式调用命令:python -m jaraco.mongodb.oplog。
命令行选项
用法如下:
$ python -m jaraco.mongodb.oplog --help usage: oplog.py [--help] [--source host[:port]] [--oplogns OPLOGNS] [--dest host[:port]] [-w WINDOW] [-f] [--ns [NS [NS ...]]] [-x [EXCLUDE [EXCLUDE ...]]] [--rename [ns_old=ns_new [ns_old=ns_new ...]]] [--dry-run] [--resume-file FILENAME] [-s SECONDS] [-l LOG_LEVEL] optional arguments: --help show usage information --source host[:port] Hostname of the mongod server from which oplog operations are going to be pulled. Called "--from" in mongooplog. --oplogns OPLOGNS Source namespace for oplog --dest host[:port] Hostname of the mongod server (or replica set as <set name>/s1,s2) to which oplog operations are going to be applied. Default is "localhost". Called "--host" in mongooplog. -w WINDOW, --window WINDOW Time window to query, like "3 days" or "24:00" (24 hours, 0 minutes). -f, --follow Wait for new data in oplog. Makes the utility polling oplog forever (until interrupted). New data is going to be applied immediately with at most one second delay. --ns [NS [NS ...]] Process only these namespaces, ignoring all others. Space separated list of strings in form of ``dname`` or ``dbname.collection``. May be specified multiple times. -x [EXCLUDE [EXCLUDE ...]], --exclude [EXCLUDE [EXCLUDE ...]] List of space separated namespaces which should be ignored. Can be in form of ``dname`` or ``dbname.collection``. May be specified multiple times. --rename [ns_old=ns_new [ns_old=ns_new ...]] Rename database(s) and/or collection(s). Operations on namespace ``ns_old`` from the source server will be applied to namespace ``ns_new`` on the destination server. May be specified multiple times. --dry-run Suppress application of ops. --resume-file FILENAME Read from and write to this file the last processed timestamp. -l LOG_LEVEL, --log-level LOG_LEVEL Set log level (DEBUG, INFO, WARNING, ERROR)
示例用法
考虑以下示例用法:
python -m jaraco.mongodb.oplog --source prod.example.com:28000 --dest dev.example.com:28500 -f --exclude logdb data.transactions --seconds 600
这个命令将从prod的最后10分钟开始执行操作, 并将它们应用到dev.databaselogdb和集合transactions中 data数据库将被忽略。最后几分钟的手术后 应用后,命令将等待新的更改,继续运行直到 收到CTRL+C或其他终止信号。
该工具提供一个--dry-run选项,当在调试级别进行日志记录时 发出oplog条目。将这些组合起来,将该工具用作oplog cat工具:
$ python -m jaraco.mongodb.oplog --dry-run -s 0 -f --source prod.example.com --ns survey_tabs -l DEBUG
测试
oplog的测试使用测试工具用javascript编写 用于测试MongoDB站点自身。您可以使用以下命令运行oplog套件:
mongo tests/oplog.js
测试产生大量的输出。成功的执行以这样的行结束:
ReplSetTest stopSet *** Shut down repl set - test worked ****
这些测试作为持续集成和发布验收的一部分运行 特拉维斯的测试。