本文介绍了在 MONGODB 中对 CSV 中的数据进行拆分和计数时遇到问题(在 columnname 等列中具有空值:“")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下脚本对字段(mongodb)中逗号分隔值的数据进行拆分和计数.但在我的情况下,我有这样的空值",它显示errmsg":异常:reduce -> multiple not supported",(这个错误).如果值不为空,则它可以正常工作.我需要为具有空值的列工作,例如 [characters:""].请帮忙.

I have used the following script to split and count the number of data of comma separated value in a field(mongodb).but in my case i have null values like this "" for this it shows "errmsg" : "exception: reduce -> multiple not supported yet",(this error).if value is not null it works properly.i need to work it for column has null value like this [characters:""].kindly help.

错误:"errmsg" : "异常:reduce -> 不支持多个",代码":10075,好的":0

Error:"errmsg" : "exception: reduce -> multiple not supported yet","code" : 10075,"ok" : 0

enter code here
map = function() {
var array = this.characters.split(',');
emit(this.characters, array.length);
}

reduce = function(key, values) {
return values;
}

result = db.runCommand({
    "mapreduce" : "book", 
    "map" : map,
    "reduce" : reduce,
    "out" : "comma_result"
 });`

推荐答案

我想我已经发现了问题.考虑以下输入数据:

I think I have discovered the problem. Consider the following input data:

{_id: 1, characters: ""}
{_id: 2, characters: "a, b, c"}
{_id: 3, characters: "a, b, c"}

> db.collection.mapReduce(map, reduce, {out: { inline : 1}})
"exception: reduce -> multiple not supported yet"

此错误消息表明 MR 当前无法用于返回值数组.如果你看看你的reduce函数:

This error message indicates that MR currently cannot be used to return an array of values. If you take a look at your reduce function:

reduce = function(key, values) {
    return values;
}

"values" 将是一个由键组合在一起的 "array.length" 数组.由于键 "a,b,c" 被发出两次(对于带有 "" 的多个文档遵循相同的逻辑),值(在我的示例中)是一个包含两个元素的数组,而 MR 无法返回数组.

"values" will be an array of "array.length"s grouped together by key. Since the key "a,b,c" was emitted twice (the same logic follows for multiple documents with ""), values (in my example) is an array with two elements, and MR cannot return arrays.

如果为特定键发出单个文档(_id:1 就是这种情况),则不会调用 reduce 函数.这解释了为什么当您不发出空字符时不会收到错误消息.

If a single document is emitted for a particular key (which is the case for _id:1), the reduce function won't be called. This explains why you don't get an error message when you don't emit null characters.

要使这个 MR 操作工作,您需要为 {characters: ""} 发出单个文档.如果您提供有关您的数据的其他信息,我们可能会帮助找到解决方法.

To get this MR operation to work, you need to emit a single document for {characters: ""}. If you provide additional information about your data, we may be able to help find workarounds.

以下 reduce 函数将确保返回单个值,而不是数组:

The following reduce function will ensure that a single value, rather than an array, is returned:

reduce = function(key, values) {
        return values[0];
}

编辑 2:

为了防止错误,"errmsg" : "exception: map invoke failed: JS Error: TypeError: this.characters has no properties nofile_b:1", "code" : 9014...

To prevent the error, "errmsg" : "exception: map invoke failed: JS Error: TypeError: this.characters has no properties nofile_b:1", "code" : 9014...

map = function() { 
    if (this.characters != null){ 
         var array = this.characters.split(','); 
         emit(this.characters, array.length);
    } 
}

这篇关于在 MONGODB 中对 CSV 中的数据进行拆分和计数时遇到问题(在 columnname 等列中具有空值:“")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 16:05