调戏data mongodb 之 mapreduce
《调戏data mongodb 之 mapreduce》要点: 《调戏data mongodb 之 mapreduce》是否对您有启发,欢迎查看更多与《调戏data mongodb 之 mapreduce》相关教程,学精学透。编程之家PHP学院为您提供精彩教程。
这边主要的js代码都将写在js文件中,放在classpath下面统一维护,修改起来也比较方便,如果直接用字符串拼接的方式在代码中,难看又难维护. 就算不用框架,就用驱动操作mapreduce时,自己也可以将js代码写在xml中,跟mybatis一样,然后写个工具类去读取即可. MapReduceOptions options = MapReduceOptions.options(); options.outputCollection("Article_MapReduce"); options.outputTypeReduce(); options.finalizeFunction("classpath:finalize.js"); MapReduceResults<ValueObject> reduceResults = mongoTemplate.mapReduce("article_info","classpath:map.js", "classpath:reduce.js",options,ValueObject.class); reduceResults.forEach(System.out::println); outputCollection是指将结果输出某个集合中 finalizeFunction是对应的finalize的js函数代码 mapReduce有多个重载方法,下面可以看到有不同的参数,有可以指定输入集合名称的,也有直接传Query的,用Query意味着可以处理符合条件的一些数据,如果不指定Query,那么将处理集合中的所有数据. mongoTemplate.mapReduce(inputCollectionName,mapFunction, reduceFunction,entityClass) mongoTemplate.mapReduce(query,inputCollectionName, mapFunction,reduceFunction,entityClass) mongoTemplate.mapReduce(inputCollectionName,mapReduceOptions,entityClass) 前面说到对应的js代码我们是写在文件中,然后调用的时候传这个文件的名称,框架自己回去加载对应的js代码,我们从源码中可以看到有读取js代码的方法. public <T> MapReduceResults<T> mapReduce(Query query,String inputCollectionName,String mapFunction, String reduceFunction,MapReduceOptions mapReduceOptions,Class<T> entityClass) { String mapFunc = replaceWithResourceIfNecessary(mapFunction); String reduceFunc = replaceWithResourceIfNecessary(reduceFunction); DBCollection inputCollection = getCollection(inputCollectionName); MapReduceCommand command = new MapReduceCommand(inputCollection,mapFunc,reduceFunc, mapReduceOptions.getOutputCollection(),mapReduceOptions.getOutputType(), query == null || query.getQueryObject() == null ? null : queryMapper.getMappedObject(query.getQueryObject(),null)); protected String replaceWithResourceIfNecessary(String function) { String func = function; if (this.resourceLoader != null && ResourceUtils.isUrl(function)) { Resource functionResource = resourceLoader.getResource(func); if (!functionResource.exists()) { throw new InvalidDataAccessApiUsageException(String.format("Resource %s not found!",function)); } Scanner scanner = null; try { scanner = new Scanner(functionResource.getInputStream()); return scanner.useDelimiter("A").next(); } catch (IOException e) { throw new InvalidDataAccessApiUsageException(String.format("Cannot read map-reduce file %s!",function),e); } finally { if (scanner != null) { scanner.close(); } } } return func; } 下面贴出今天测试的js代码,按文章的作者统计文章的次数 map.js function() { emit(this.author,1); } reduce.js function(key,values) { var sum = 0; for (var i = 0; i < values.length; i++) sum += values[i]; return sum; } finalize.js function(key,reduce) { return reduce; } finalize中没有去格式化输出的格式,所以输出的格式是原始的格式 { "_id" : "文章作者","value" : 文章次数 } 上面的调用代码中虽然指定了输出结果的集合名称,但还是定义了ValueObject来接收返回值,那么ValueObject的格式肯定也是id和value. public class ValueObject { private String id; private Integer value; public String getId() { return id; } public void setId(String id) { this.id = id; } public Integer getValue() { return value; } public void setValue(Integer value) { this.value = value; } @Override public String toString() { return "ValueObject [id=" + id + ",value=" + value + "]"; } } 在库中查询的原始数据格式如下 > db.Article_MapReduce.find(); { "_id" : "jason","value" : 1 } { "_id" : "mk","value" : 1 } { "_id" : "yinjihuan","value" : 18 } > 源码下载:https://github.com/yinjihuan/cxytiandi (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |