嗨,我有如下的 mongo 收藏
{
"command":{
"top -n 1 b":"top - 10:53:01 up 1:25, 6 users, load average: 0.31, 0.39, 0.35
Tasks: 228 total, 1 running, 227 sleeping, 0 stopped, 0 zombie
Cpu(s): 9.9%us, 1.7%sy, 0.0%ni, 87.1%id, 1.2%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 3795988k total, 3648196k used, 147792k free, 71016k buffers
Swap: 3932152k total, 0k used, 3932152k free, 975424k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
3464 root 20 0 380m 83m 6708 S 3.2 2.3 8:22.56 skype
1741 root 20 0 0 0 0 S 1.6 0.0 0:02.89 kondemand/2
3084 root 20 0 433m 13m 9.9m S 1.6 0.4 0:02.53 metacity
3415 root 20 0 1190m 292m 42m S 1.6 7.9 12:57.92 firefox
3711 root 20 0 3453m 560m 34m S 1.6 15.1 4:25.03 java
6375 root 20 0 15028 1200 832 R 1.6 0.0 0:00.11 top "
}
}
现在我的 java 代码使用了 map 缩减功能,下面是我的 java 代码
String map ="function () {"+
"emit({hostid:this._id},{commands:this.command});}}}";
String reduce = "function (key, values) { var reduced = {top:[]};"+
"for(var i =0 ; i< values.length;i++){"+
"reduced.top=values[i].commands['top -n 1 -b']"+
"}"+
"return reduced;}";
MapReduceCommand cmd = new MapReduceCommand(collection, map, reduce,
null, MapReduceCommand.OutputType.INLINE, null);
MapReduceOutput out = collection.mapReduce(cmd);
for (DBObject o : out.results()) {
System.out.println(o.toString());
}
现在打印与密钥相关的所有信息,现在我想将此信息放入新集合中,格式如下
{
"PID":3464,
"USER":root,
"PR" : 20,
"%CPU":3.2,
"%MEM":2.3,
"COMMAND":skype
}
那么是否可以使用 map reduce 来做这件事,或者在这里我使用传统的 Java 字符串操作?