简体   繁体   English

使用Java的MongoDB数据处理

[英]MongoDB data processing using Java

Hi I had mongo collection as below 嗨,我的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 "      
   }
}

Now my java code used map reduced functionality below is my java code 现在我下面的Java代码使用的地图缩减功能是我的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());

    }

Now this print all info related to key, now I want to put this info in new collection as below format 现在,此打印与密钥相关的所有信息,现在我想将此信息放入以下格式的新集合中

 {
   "PID":3464,
   "USER":root,
   "PR" : 20,
   "%CPU":3.2,
   "%MEM":2.3,
   "COMMAND":skype
 }

So is possible do this thing using map reduced or here I used traditional Java string manipulation? 那么可以使用map减少这件事吗?还是在这里我使用了传统的Java字符串操作?

You're going to extract and parse individual "top" output lines from your data. 您将要从数据中提取并解析各个“顶部”输出行。 That's a job for regular expressions. 这是正则表达式的工作。 Pretty easy to do in Java. 用Java很容易做到。 I don't see why you would want to use MapReduce for that -- much easier to simply read your raw "top" data from the database one record at a time, do the parsing and conversion in the client, and write the results to a new collection. 我不明白为什么要使用MapReduce -轻松地一次从数据库中一次读取一条记录中的原始“顶部”数据,在客户端中进行解析和转换,然后将结果写入一个新的收藏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM