支持MongoDB环境的例程和类
项目描述
迁移管理器
jaraco.mongodb.migration实现了MongoDB 2016年MongoWorld展示的迁移管理器功能,请参阅从民意调查到网络暴民。使用它将不同模式版本的文档加载到应用程序期望的目标版本中。
安装
jaraco.mongodb.install使得在任何平台上获取MongoDB社区版下载变得非常简单
py -m jaraco.mongodb.install
默认情况下,它将安装到当前目录。使用--target安装到另一个位置。
会话
jaraco.mongodb.sessions实现了由MongoDB支持的CherryPy会话存储。
默认情况下,会话存储将处理可以自然插入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 项目,该项目本身是MongoDB官方工具 mongooplog 的Python重制版,自MongoDB 2.2版本开始提供,并在3.2版本中被弃用。它读取远程服务器的oplog,并将操作应用到本地服务器。这可以用来保持独立副本集以与副本集同步相似的方式同步,在各种备份和迁移场景中可能很有用。
oplog 实现了官方工具的基本功能,并增加了以下功能
可 tailing oplog 读取器:永久轮询新的oplog事件,这对于保持两个独立的副本集几乎实时同步非常有用。
可选同步选定数据库/集合。
可选排除一个或多个命名空间(即数据库或集合)以进行同步。
能够在飞行中“重命名”数据库/集合,即目标命名空间可以与原始命名空间不同。此功能适用于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。将省略logdb数据库和transactions集合中的操作。在应用最后几分钟的操作后,命令将等待新更改的到来,直到接收到Ctrl+C或其他终止信号。
该工具提供了 --dry-run 选项,并在DEBUG级别记录时发出oplog条目。将这些结合起来,可以将工具用作oplog cat工具
$ python -m jaraco.mongodb.oplog --dry-run -s 0 -f --source prod.example.com --ns survey_tabs -l DEBUG
测试
使用测试框架编写的 oplog 测试,该框架用于测试MongoDB本身。您可以使用以下命令运行oplog套件
mongo tests/oplog.js
测试会产生大量输出。成功执行以类似此行的行结束
ReplSetTest stopSet *** Shut down repl set - test worked ****
这些测试作为Travis中的持续集成和发布验收测试的一部分运行
固定装置
jaraco.mongodb 提供了一些相当复杂的pytest固定装置。
mongodb_instance 是一个运行中的MongoDB实例,带有可用的PyMongo连接。它尝试定位现有的MongoDB安装,如果找不到,它将下载社区版并将其安装到临时目录中,以便进行测试会话。
此固定装置使得运行脚本,如 此示例(在pip-run中)变得简单,该脚本可以全面验证MongoDB在任何平台上对行为的预期,而无需任何依赖,只需pip-run(当然还有Python)。
项目详情
下载文件
下载适用于您的平台的文件。如果您不确定该选择哪一个,请了解更多关于安装包的信息。