2014-01-17 41 views
1

我有一個蒙戈DB 5萬個條目,看起來像這樣:MapReduce的MongoDB的用戶代理

{ 
    "_id" : ObjectId("525facace4b0c1f5e78753ea"), 
    "productId" : null, 
    "name" : "example name", 
    "time" : ISODate("2013-10-17T09:23:56.131Z"), 
    "type" : "hover", 
    "url" : "www.example.com", 
    "userAgent" : "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 openssl/0.9.8r zlib/1.2.5" 
} 

我需要添加到每個條目一個新的領域被稱爲device這將有兩種價值desktopmobile 。這意味着,我們的目標將是有以下種類的條目:

{ 
    "_id" : ObjectId("525facace4b0c1f5e78753ea"), 
    "productId" : null, 
    "device" : "desktop", 
    "name" : "example name", 
    "time" : ISODate("2013-10-17T09:23:56.131Z"), 
    "type" : "hover", 
    "url" : "www.example.com", 
    "userAgent" : "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 openssl/0.9.8r zlib/1.2.5" 
} 

我跟MongoDB的Java驅動程序的工作,到目前爲止,我做了以下內容:

DBObject query = new BasicDBObject(); 
query.put("device", new BasicDBObject("$exists", false)); //some entries already have such field 
DBCursor cursor = resource.find(query); 
cursor.addOption(Bytes.QUERYOPTION_NOTIMEOUT); 
Iterator<DBObject> iterator = cursor.iterator(); 
int size = cursor.count(); 

然後我用while(iterator.hasNext())迭代,做一個的if-else與一個巨大的正則表達式,我發現在那裏,並根據結果這樣的if-else我執行類似:

BasicDBObject newDocument = new BasicDBObject("$set", new BasicDBObject().append("device", "desktop")); //of "mobile", depending on the if-else  
BasicDBObject searchQuery = new BasicDBObject("_id", id);    
resource.getCollection(DatabaseConfiguration.WEBSITE_STATISTICS).update(searchQuery, newDocument); 

Howev呃,由於大量的數據(超過500萬條),這需要永遠。

有沒有這樣做的方式與地圖縮小?到目前爲止,我只用MapReduce進行計數,所以我不確定它是否可以用於其他事情。

回答

0

我發現一種方式,由於整個配置有點棘手。

安裝的Hadoop以下這個link後,我做了以下:

  1. 創建一類稱爲MongoUpdate,具有方法run哪裏設置所有的配置(如輸入和輸出URI),並創建一個作業並配置所有設置。在這些,還有job.setMapperClass(MongoMapper.class)

  2. 創建MongoMapper在那裏我有哪些得到了BSONObject方法map。這裏我執行if-else條件,並且在最後我執行:

    Text id = new Text(pValue.get(「_ id」)。toString()); pContext.write(id,new BSONWritable(pValue));

  3. Main類,其主要方法簡單地實例化一個類MongoUpdate並運行它run方法

  4. 導出與所有庫罐和類型的終端上:hadoop java NameOfTheJar.jar