如何在 Mongoid 中获取用于调试 map/reduce 的打印输出? [英] How to get print output for debugging map/reduce in Mongoid?

查看:13
本文介绍了如何在 Mongoid 中获取用于调试 map/reduce 的打印输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Mongoid 3.0 编写 map/reduce 操作.我正在尝试使用 print 语句来调试 JS 函数.这是 MongoDB 文档中的故障排除建议,例如:

I'm writing a map/reduce operation with Mongoid 3.0. I'm trying to use the print statement to debug the JS functions. This is a troubleshooting suggestion from the MongoDB docs, e.g.:

reduce = %Q{
   function(user_id, timestamps) {
      var max = 0;
      timestamps.forEach(function(t) {
        var diff = t.started_at - t.attempted_at;
        if (diff > max) { max = diff; }
      });
      print(user_id + ', ' + max);
      return max;
    };
  }

 MyCollection.all.map_reduce(map, reduce).to_a

不幸的是,print 语句的输出既没有显示在控制台上,也没有显示在日志上——这似乎是在 DB、驱动程序、Moped 或任何中间层之间的某个地方被抑制了.有没有办法打开它?

Unfortunately the output from the print statement shows up neither on the console nor on the log--it seems that this is suppressed somewhere between the DB, the driver, Moped or any of the intervening layers. Is there a way to turn this on?

推荐答案

根据发帖者的评论,第一步是确保您正在查看 mongod 服务器日志.在本地执行此操作的最简单方法是访问鲜为人知的 HTTP 接口到 mongod:http://localhost:28017.即使服务器未安装在其默认位置,这也将起作用,因此,日志不会位于默认的操作系统特定位置,例如 /usr/local/var/log/mongodb/mongo.log 在 Mac OS X 上.请参阅文档了解如何在非标准部署环境中访问它.

Per the poster's comment, the first step is to make sure you are looking at the mongod server log. The easiest way to do this locally is by visiting the little-known HTTP interface to mongod: http://localhost:28017. This will work even if the server is not installed in its default location and, therefore, the log will not be in the default OS-specific location, e.g, /usr/local/var/log/mongodb/mongo.log on Mac OS X. See the docs for how to access this in non-standard deployment environments.

如果这不能解决您的问题,您将不得不更深入地调试 MongoDB M/R,这很棘手,原因有几个:

If that doesn't solve your problem, you'll have to go deeper into debugging MongoDB M/R, which is tricky for several reasons:

  1. M/R 作业在与数据库其余部分隔离的 JS 范围内执行
  2. M/R 作业不能写入 M/R 输出文档以外的任何内容
  3. MongoDB 中的记录器可以自行决定向日志流发送什么内容

以下是基于我们在生产中广泛使用 Mongo M/R 的建议:

Here are my suggestions based on our extensive use of Mongo M/R in production:

提高日志记录级别

突然间,一些以前不可见的 print 输出会出现.一次做一层,直到你开始看到它.

Suddenly, some of the previously invisible print output will show up. Do it one level at a time until you start seeing it.

use admin
db.runCommand( { setParameter: 1, logLevel: 2 } )

日志级别从 0 到 5 不等.请参阅 setParameter.您也可以在服务器启动时使用 -v、-vv、...、-vvvvv 执行此操作.

Log levels vary from 0 to 5. See setParameter. You can also do this at server startup with -v, -vv, ..., -vvvvv.

将分析级别提高到最高 (2)

我发现这很有用,尤其是与高日志级别结合使用时,因为它提供了可以以编程方式检查的数据耗尽(分析集合).请参阅文档.

I've found this useful, especially when combined with a high log level as it provides data exhaust (the profiling collection) that can be programmatically inspected. See the docs.

如果您正在运行一个非常大的 M/R 作业,这可能会对性能产生负面影响.

If you are running a very big M/R job, this may negatively affect performance.

通过 M/R 搭载调试输出

这个想法很简单:在所有发出的文档中添加一个包含您需要的任何调试信息的对象数组,例如在 _debug 字段中,并在缩减阶段按照您认为合适的方式过滤 + 连接.这样,_debug 数组将最终出现在您的输出集合中.如果您想快速找到特定问题,您甚至可以将它们编入索引.

The idea is simple: add an array of objects with whatever debugging info you need, say in the _debug field, to all emitted documents and filter + concatenate as you see fit in the reduce phase. This way, the _debug arrays will end up in your output collection. You can even index them if you want to quickly find a particular problem.

希望这会有所帮助.祝你好运!

Hope this helps. Good luck!

这篇关于如何在 Mongoid 中获取用于调试 map/reduce 的打印输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆