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

查看:102
本文介绍了如何获得打印输出以调试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服务器日志.在本地执行此操作的最简单方法是访问鲜为人知的mongod:http://localhost:28017的HTTP接口.即使未将服务器安装在其默认位置,该操作也将起作用,因此,日志将不会位于特定于操作系统的默认位置,例如,在Mac OS X上为/usr/local/var/log/mongodb/mongo.log. 有关如何在非标准部署环境中访问此文档的信息,请参见文档.

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字段中,然后在您认为适合于reduce阶段的情况下进行filter +连接.这样,_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天全站免登陆