Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case study II of MongoDB mapReduce

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Example 2. Find the total number of orders corresponding to an item in an order and the average sales quantity of each order for this item

1. Take a look at the order table first. The data structure of orders is as follows.

There are six such data items. As follows, only the field items is listed.

2. The map function is given below.

Var mapFunction2 = function () {for (var idx = 0; idx < this.items.length; idx++) {var key = this.items [IDX] .sku; var value = {count: 1this.items [IDX] .qty}; emit (key, value);}}

3. Write reduce function

Var reduceFunction2 = function (keySKU, countObjVals) {reducedVal = {count: 0, qty: 0}; for (var idx = 0; idx < countObjVals.length; idx++) {reducedVal.count + = countObjVals [IDX] .count; reducedVal.qty + = countObjVals [IDX] .qty;} return reducedVal;}

4. Write finalize function

Var finalizeFunction2 = function (key, reducedVal) {reducedVal.avg = reducedVal.qty/reducedVal.count;return reducedVal;}

5. Execute the mapReduce function

6. View the results

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report