search
HomeWeb Front-endJS TutorialA brief analysis of how JavaScript mapreduce works_Basic knowledge

Google published three very influential articles in succession from 2003 to 2006, namely GFS released on SOSP in 2003, MapReduce released on OSDI in 2004, and BigTable released on OSDI in 2006. . GFS is related to file systems and has guiding significance for subsequent distributed file system design; MapReduce is a parallel computing programming model used for job scheduling; BigTable is a distributed storage system used to manage structured data. Built on Google technologies such as GFS, Chubby, SSTable, etc. Quite a few Google applications use these three technologies, such as Google Search, Google Earth, and Google Analytics, etc. Therefore, these three technologies are collectively called the "Three Treasures" of Google technology. Today, D Gua Ge is doing his best to do a "cook job" on MapReduce!

Introduction to MapReduce
MapReduce is a programming model and a related implementation of an algorithm model for processing and generating extremely large data sets. The user first creates a
Map function to process a data set based on key/value pairs and outputs the intermediate data set based on key/value pairs; then
creates a Reduce function to merge all data sets with the same intermediate The middle value of the key value.
A picture is worth a thousand words, let’s use a picture to illustrate MapReduce:

图解MapReduce原理


Programming Practice
As the saying goes: "Practice brings true knowledge." Whether it's a mule or a horse, you won't know until you take it out for a walk. Therefore, if you really want to understand this principle, it is best to write the code yourself and practice it.
Recently I am learning JavaScript with a few friends, so I pay more attention to JavaScript. Yesterday, when I was browsing around on the Internet, I was surprised to find that there was an awesome person who implemented the MapReduce algorithm using JavaScript. Then I will share it with you, and at the same time add some of my own introduction, hoping to help everyone understand MapReduce. The specific code implementation is as follows:

Copy code The code is as follows:

var Job = {
//Data to be processed
data : [
"We are glad to see you here. This site is dedicated to",
"poetry and to the people who make poetry possible",
" poets and their readers. FamousPoetsAndPoems.com is",
"a free poetry site. On our site you can find a large",
"collection of poems and quotes from over 631 poets",
"Read and Enjoy Poetry",
"I, too, sing America",
"I am the darker brother",
"They send me to eat in the kitchen",
"When company comes" ,
"But I laugh",
"And eat well",
"And grow strong",
"Tomorrow",
"Ill be at the table",
" When company comes",
"Nobodyll dare",
"Say to me",
"Eat in the kitchen",
"Then",
"Besides",
" They'll see how beautiful I am",
"And be ashamed",
"I, too, am America" ​​
],
//Separate each line of strings in the data with spaces Open,
// and "reorganize" it into an object in the format of {key: word, value: 1}, return an object array
map : function(line) {
var splits = line.split(" ");
var temp = [];
for(var i=0; itemp.push({key : splits[i], value : 1} );
}
return temp;
},
//Calculate the number of times each word appears in "data"
reduce: function(allSteps) {
var result = {};
for(var i=0; ivar step = allSteps[i];
result[step.key] = result[step. key] ? (result[step.key] 1) : 1;
}
return result;
},
//Initialization, and also the entry point for operation.
init : function() {
var allSteps = [];
for(var i=0; i//If multiple threads can be called here The Job.map function is even more realistic. ? ?
allSteps = allSteps.concat(Job.map(Job.data[i]));
}
//The only drawback is that the Job.reduce function cannot be called by multiple threads here? ?
var result = Job.reduce(allSteps)
console.log(JSON.stringify(result));
}
}; // Job
//Start executing
Job .init();

Copy these codes and paste them directly into the browser's console (Console), or put them into an HTML file and open it with a browser. You can see the following effect in the console output:

Flaw in the ointment
After this article was published, some netizens "roared": "What kind of MapReduce can a js that doesn't even have multi-threading do?" In fact, Brother D Gua also asked this question Found it. After seeing the explanation of this code, Brother D Gua wondered if JavaScript was not a single process? How can we still simulate MapReduce? After carefully reading the code and single-step debugging, D Gua Ge's views are further confirmed. (The questions about D Gua Ge have been commented in the code.)
However, if you think about it again, these do not affect our understanding of the principles of MapReduce. This is just a single process, the most basic version. After understanding this first, it may be easier to understand the whole multi-threading process.

To be continued
In fact, Brother D is now considering using Java to implement a multi-threaded version based on this example, so that the simulated MapReduce will be more realistic. After Brother D Gua has thought through some issues clearly, he will send out the code. Stay tuned!
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor