github.com/lingyao2333/mo-zero@v1.4.1/core/mr/readme.md (about)

     1  <img align="right" width="150px" src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/go-zero.png">
     2  
     3  # mapreduce
     4  
     5  English | [简体中文](readme-cn.md)
     6  
     7  ## Why MapReduce is needed
     8  
     9  In practical business scenarios we often need to get the corresponding properties from different rpc services to assemble complex objects.
    10  
    11  For example, to query product details.
    12  
    13  1. product service - query product attributes
    14  2. inventory service - query inventory properties
    15  3. price service - query price attributes
    16  4. marketing service - query marketing properties
    17  
    18  If it is a serial call, the response time will increase linearly with the number of rpc calls, so we will generally change serial to parallel to optimize response time.
    19  
    20  Simple scenarios using `WaitGroup` can also meet the needs, but what if we need to check the data returned by the rpc call, data processing, data aggregation? The official go library does not have such a tool (CompleteFuture is provided in java), so we implemented an in-process data batching MapReduce concurrent tool based on the MapReduce architecture.
    21  
    22  ## Design ideas
    23  
    24  Let's try to put ourselves in the author's shoes and sort out the possible business scenarios for the concurrency tool:
    25  
    26  1. querying product details: supporting concurrent calls to multiple services to combine product attributes, and supporting call errors that can be ended immediately.
    27  2. automatic recommendation of user card coupons on product details page: support concurrently verifying card coupons, automatically rejecting them if they fail, and returning all of them.
    28  
    29  The above is actually processing the input data and finally outputting the cleaned data. There is a very classic asynchronous pattern for data processing: the producer-consumer pattern. So we can abstract the life cycle of data batch processing, which can be roughly divided into three phases.
    30  
    31  <img src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/mapreduce-serial-en.png" width="500">
    32  
    33  1. data production generate
    34  2. data processing mapper
    35  3. data aggregation reducer
    36  
    37  Data producing is an indispensable stage, data processing and data aggregation are optional stages, data producing and processing support concurrent calls, data aggregation is basically a pure memory operation, so a single concurrent process can do it.
    38  
    39  Since different stages of data processing are performed by different goroutines, it is natural to consider the use of channel to achieve communication between goroutines.
    40  
    41  <img src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/mapreduce-en.png" width="500">
    42  
    43  How can I terminate the process at any time?
    44  
    45  It's simple, just receive from a channel or the given context in the goroutine.
    46  
    47  ## A simple example
    48  
    49  Calculate the sum of squares, simulating the concurrency.
    50  
    51  ```go
    52  package main
    53  
    54  import (
    55      "fmt"
    56      "log"
    57  
    58      "github.com/lingyao2333/mo-zero/core/mr"
    59  )
    60  
    61  func main() {
    62      val, err := mr.MapReduce(func(source chan<- interface{}) {
    63          // generator
    64          for i := 0; i < 10; i++ {
    65              source <- i
    66          }
    67      }, func(item interface{}, writer mr.Writer, cancel func(error)) {
    68          // mapper
    69          i := item.(int)
    70          writer.Write(i * i)
    71      }, func(pipe <-chan interface{}, writer mr.Writer, cancel func(error)) {
    72          // reducer
    73          var sum int
    74          for i := range pipe {
    75              sum += i.(int)
    76          }
    77          writer.Write(sum)
    78      })
    79      if err != nil {
    80          log.Fatal(err)
    81      }
    82      fmt.Println("result:", val)
    83  }
    84  ```
    85  
    86  More examples: [https://github.com/zeromicro/zero-examples/tree/main/mapreduce](https://github.com/zeromicro/zero-examples/tree/main/mapreduce)
    87  
    88  ## Give a Star! ⭐
    89  
    90  If you like or are using this project to learn or start your solution, please give it a star. Thanks!