Episode 8 : MapReduce Ka Jaadu: Luxmi Ki Kahani

 

Episode 8  MapReduce Ka Jaadu: Luxmi Ki Kahani

Luxmi ne pucha, "MapReduce ka example thoda aur samjha do, taaki mujhe clearly samajh aaye!" Mongo Baba ne Luxmi ki madad ke liye ek kahani banayi, jisme MapReduce ke concept ko asaani se samjhaya ja sake.


MapReduce: Ek Chhoti Kahani

Scenario: Gaav mein ek mela lagta hai, jahan har bacha apne-apne games khel raha hai. Mela mein ek competition hota hai jisme bachon ko unki games ki scores ko gather karna hota hai aur total score calculate karna hota hai. Yeh task bahut bada hai, isliye gaav ke bade log MapReduce ka use karte hain is problem ko solve karne ke liye.

1. Map Stage (Data Collection)

  • Map (Batao):

    • Sab bachon ko ek ek game ki scores collect karni hoti hai.
    • Har bachha apni game ke scores ko ek list mein record karta hai.
    • Jaise, Ravi apni car racing game ke scores record karta hai, Anu apni puzzle game ke scores record karti hai, aur baaki bacche bhi apne-apne scores record karte hain.
  • Example:

    • Ravi ne apne game "RaceMaster" ke scores diye:

      { "game": "RaceMaster", "score": 100 } { "game": "RaceMaster", "score": 150 } { "game": "RaceMaster", "score": 120 }
    • Anu ne apne game "PuzzleWiz" ke scores diye:

      { "game": "PuzzleWiz", "score": 200 } { "game": "PuzzleWiz", "score": 180 } { "game": "PuzzleWiz", "score": 210 }

2. Reduce Stage (Data Aggregation)

  • Reduce (Jodo):
    • Ab sab scores ko ek saath gather kiya jata hai aur aggregate kiya jata hai.

    • Ek data scientist (jo ki yeh role gaav mein koi bada admi play karta hai) scores ko sum karke final total calculate karta hai.

    • For example, "RaceMaster" game ke total score ka sum calculate karna:


      // Total Score Calculation for RaceMaster let raceMasterScores = [100, 150, 120]; let totalRaceMasterScore = raceMasterScores.reduce((acc, score) => acc + score, 0); // totalRaceMasterScore = 370
    • Similarly, "PuzzleWiz" game ke total score ka sum calculate karna:


      // Total Score Calculation for PuzzleWiz let puzzleWizScores = [200, 180, 210]; let totalPuzzleWizScore = puzzleWizScores.reduce((acc, score) => acc + score, 0); // totalPuzzleWizScore = 590

MapReduce Ka Summary

  1. Map Stage: Data ko chhote chhote parts mein divide kar ke collect kiya jata hai.
  2. Reduce Stage: Sab collected data ko aggregate kiya jata hai, taaki final result mil sake.

Example:

  • Ravi aur Anu ke game scores ko separately collect kiya aur unhe aggregate kiya. Ab gaav mein pata lag gaya ki "RaceMaster" aur "PuzzleWiz" games ke total scores kitne hain.

Luxmi ko ab samajh aaya ki MapReduce kis tarah se data ko efficiently handle karne mein help karta hai, jab data bahut bada ho aur processing ki zaroorat ho.


DataGaav mein aaj ka kissa yahin khatam hota hai. Aur koi sawaal ya kahani chahiye, toh batao!

 What are the restrictions of the MongoDB 32-bit versions?

 When should we normalize the data in MongoDB?

 How do we perform sorting and Explain Project in MongoDB?

 How can MongoDB simulate subquery or join?

MongoDB Ka Jaadu: Sab Sawalon Ka Solution

Mongo Baba ne aaj ke session mein dusre gaanv ke bacchon ke sawalon ka jawab dene ka faisla kiya. Chalo dekhte hain MongoDB ke restrictions, data normalization, sorting, projection, aur subqueries ka jaadu!


1. MongoDB 32-bit Versions Ki Restrictions:

Restrictions:

  1. Limited Addressable Space:

    • 32-bit versions mein aap sirf 2GB tak data store kar sakte hain, kyunki 32-bit addressing space mein maximum 2GB memory use ki ja sakti hai.
  2. Data Size Limitation:

    • Document size 16MB se zyada nahi ho sakta. Large documents ko handle karne ke liye aapko 64-bit version ki zaroorat hoti hai.
  3. Performance Issues:

    • 32-bit versions mein performance issues bhi ho sakte hain, especially jab data bahut bada ho.
  4. Limited Scalability:

    • 32-bit systems mein scalability ki limitation hoti hai, jo large-scale applications ke liye problematic ho sakti hai.

2. Data Normalization in MongoDB:

When to Normalize Data:

  • Data Redundancy Reduction:

    • Jab aapko data redundancy (duplicate data) ko reduce karna ho. Normalization helps in keeping data in a single place, reducing duplication.
  • Data Integrity:

    • Jab data integrity maintain karni ho. Normalization ensures that updates, deletions, and insertions are consistent across the database.
  • Complex Queries:

    • Jab complex queries aur joins ki zaroorat na ho. Normalization can be useful when working with relational data models that need frequent updates.

Example: Agar aapke paas multiple addresses for the same user, aap normalization karke ek alag address collection maintain kar sakte hain aur user collection mein reference store kar sakte hain.


3. Sorting and Projection in MongoDB:

Sorting:

  • Usage: Sorting data helps in organizing documents based on specific fields.

  • Example Command:

    javascript
    db.collection.find().sort({ fieldName: 1 }); // 1 for ascending, -1 for descending

    Example:

    javascript

    db.students.find().sort({ score: -1 }); // Sort students by score in descending order

Projection:

  • Usage: Projection allows you to include or exclude specific fields in the query results.

  • Example Command:

    javascript

    db.collection.find({}, { fieldName: 1 }); // Includes only the fieldName in results

    Example:

    javascript

    db.students.find({}, { name: 1, score: 1, _id: 0 }); // Only include name and score fields, exclude _id

4. Simulating Subqueries or Joins in MongoDB:

Subqueries and Joins Simulation:

  • Using Aggregation Framework:

    • $lookup Stage: This stage helps in simulating joins between collections.
    • Example Command:
      javascript

      db.orders.aggregate([ { $lookup: { from: "products", localField: "productId", foreignField: "productId", as: "orderDetails" } } ]);
      Explanation: This command joins the orders collection with the products collection based on productId.
  • Using $unwind:

    • $unwind Stage: This stage is used after $lookup to deconstruct the array of joined documents.
    • Example Command:
      javascript

      db.orders.aggregate([ { $lookup: { from: "products", localField: "productId", foreignField: "productId", as: "orderDetails" } }, { $unwind: "$orderDetails" } ]);
      Explanation: This command performs a join and then flattens the resulting array.
  • Manual Joins:

    • Retrieve documents from one collection and then query another collection using the data from the first query.

Example:

  • Fetching Orders with Product Details:
    javascript

    let orders = db.orders.find(); orders.forEach(order => { let product = db.products.findOne({ productId: order.productId }); print("Order ID: " + order._id + ", Product Name: " + product.name); });

Yeh hai MongoDB ke jaadu ki kahani! Bacche aur gaav ke logon ko ab MongoDB ke features aur functionalities samajh mein aa gaye hain. Agar aur koi sawaal hai ya kisi aur topic par jaankari chahiye, toh batao!

Post a Comment

0 Comments