日期:2014-05-16 浏览次数:20476 次
import java.io.BufferedReader; import java.io.FileReader; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MapReduceCommand; import com.mongodb.MapReduceOutput; import com.mongodb.Mongo; import com.mongodb.MongoException; /** * */ /** * @author songlianjun * */ public class Test4MongoDb { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub //insertTestKeywordLog(); calculateSearchKeyword(); } /** * 生成测试数据 */ private static void insertTestKeywordLog() { List<String> keyWordList = new ArrayList<String>(); try { BufferedReader reader = new BufferedReader(new FileReader( "d:\\pinyin.txt")); String line = null; Random ran = new Random(System.currentTimeMillis()); Mongo m; int totalRows = 0; long start = 0; long end = 0; m = new Mongo("localhost", 9999); DB db = m.getDB("test"); DBCollection collection = db.getCollection("t_log"); String month = "02"; String year = "2010"; start = System.currentTimeMillis(); while ((line = reader.readLine()) != null) { int insertCount = ran.nextInt(100); if (insertCount == 0) { insertCount = 1; } totalRows += insertCount; for (int i = 0; i < insertCount; i++) { DBObject record = new BasicDBObject(); record.put("id", System.currentTimeMillis()); record.put("keyword", line); int day = ran.nextInt(28); if (day == 0) { day = 1; } record.put("dd", year + "-" + month + (day < 10 ? "-0" + day : "-" + day)); collection.save(record); } } end = System.currentTimeMillis(); System.out.println("insert time =" + (end - start) + " row count=" + totalRows); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 统计查询关键字频率 */ private static void calculateSearchKeyword() { long start = 0; long end = 0; Mongo m; try { m = new Mongo("localhost", 9999); DB db = m.getDB("test"); DBCollection collection = db.getCollection("t_log"); DBObject newDB = new BasicDBObject(); newDB.put("max", 100000); String collectionName = "t_log_result_" + System.currentTimeMillis(); DBCollection resultCollection = db.createCollection(collectionName, newDB); //创建统计数量索引 DBObject indexObject = new BasicDBObject(); indexObject.put("hitCount", -1); resultCollection.createIndex(indexObject); //DBCollection resultCollection = db.getCollection("t_log_result_"+System.currentTimeMillis()); start = System.currentTimeMillis(); DBObject dbKey = new BasicDBObject(); dbKey.put("dd", true); //查询符合条件的数据 DBObject condition = new BasicDBObject(); condition.put("dd", new BasicDBObject("$gte", "2010-02-01").append( "$lte", "2010-02-28")); //定义map String map = "function() { key=this.keyword; " + " emit(key,{'count':1}); " + "}"; //定义reduce String reduce = " function r( key, values ) { " + " var count=0;" + " for ( var i = 0; i < values.length; i++ ){" + " count += values[i].count;" + " }" + " return count;} "; ///Map<String,Object> scope =