Objaśnienie działania MapReduce na dostarczonych danych z plików relation.txt i join.txt z wykorzystaniem notacji matematycznej (LaTeX)
Część 1: Przetwarzanie pliku relation.txt
Plik relation.txt zawiera pary składające się z nazwy relacji (A lub B) oraz wartości liczbowej. Naszym celem jest wykonanie podstawowych operacji na zbiorach:
- Suma zbiorów \( A \cup B \)
- Część wspólna zbiorów \( A \cap B \)
- Różnica zbiorów \( A - B \) oraz \( B - A \)
1. Tworzenie zbiorów \( A \) i \( B \)
Z pliku relation.txt wyodrębniamy wartości przypisane do relacji A i B:
-
Zbiór \( A \) zawiera wartości:
[
A = {0, 1, 4, 8, 9, 10, 17, 18, 19, 20, 23, 25, 26, 28, 29, 30, 31, 32, 33, 35, 36, 38, 39, 42, 44, 46, 47, 48, 50, 53, 55, 56, 58, 60, 63, 64, 65, 66, 68, 70, 71, 74, 75, 76, 78, 80, 82, 83, 84, 85, 86, 87, 88, 90, 91, 92, 95, 96, 98}
]
-
Zbiór \( B \) zawiera wartości:
[
B = {0, 2, 3, 5, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 35, 36, 37, 38, 39, 40, 41, 43, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 64, 65, 67, 68, 69, 71, 73, 75, 78, 79, 81, 82, 86, 88, 89, 93, 95, 97, 98, 99}
]
2. Mapowanie wartości na pary klucz-wartość
Dla każdej wartości w zbiorach \( A \) i \( B \) tworzymy parę (klucz, wartość), gdzie kluczem jest sama wartość, a wartością jest nazwa relacji:
-
Dla zbioru \( A \):
[
\text{mapA} = {(a, \text{'A'}) \mid a \in A}
]
-
Dla zbioru \( B \):
[
\text{mapB} = {(b, \text{'B'}) \mid b \in B}
]
3. Połączenie zmapowanych danych
Łączymy zbiory \( \text{mapA} \) i \( \text{mapB} \):
[
\text{unionMap} = \text{mapA} \cup \text{mapB}
]
4. Grupowanie po kluczu (wartości)
Grupujemy pary po kluczu (wartości), otrzymując dla każdej wartości listę relacji, do których ona należy:
[
\text{grouped} = {(x, \text{[lista relacji]}) \mid x \in A \cup B}
]
Przykład:
-
Wartość \( 0 \) występuje zarówno w \( A \), jak i w \( B \):
[
(0, [\text{'A'}, \text{'B'}])
]
-
Wartość \( 1 \) występuje tylko w \( A \):
[
(1, [\text{'A'}])
]
5. Operacje na zbiorach
a) Suma zbiorów \( A \cup B \)
Suma zbiorów to zbiór wszystkich unikalnych wartości z obu zbiorów:
[
A \cup B = {x \mid x \in A \ \text{lub} \ x \in B}
]
Zbiór ten otrzymujemy, biorąc klucze z \( \text{grouped} \):
[
\text{unionValues} = {x \mid x \in \text{grouped.keys()}}
]
Obliczenie:
Wylistujemy wszystkie wartości z kluczy \( \text{grouped} \):
[
A \cup B = {0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 63, 64, 65, 66, 67, 68, 69, 70, 71, 73, 74, 75, 76, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 95, 96, 97, 98, 99}
]
b) Część wspólna zbiorów \( A \cap B \)
Część wspólna to wartości, które występują w obu zbiorach:
[
A \cap B = {x \mid x \in A \ \text{i} \ x \in B}
]
Zbiór ten otrzymujemy, filtrując \( \text{grouped} \) po tych kluczach, dla których listy relacji zawierają zarówno A, jak i B:
[
\text{intersectionValues} = {x \mid x \in \text{grouped}, \ \text{'A'} \in \text{grouped}[x] \ \text{i} \ \text{'B'} \in \text{grouped}[x]}
]
Obliczenie:
Wartości wspólne to:
[
A \cap B = {0, 8, 9, 10, 17, 19, 20, 23, 25, 26, 28, 30, 35, 36, 38, 39, 46, 47, 48, 50, 53, 55, 56, 58, 64, 65, 68, 71, 75, 78, 82, 86, 88, 95, 98}
]
c) Różnica zbiorów \( A - B \)
Różnica zbiorów \( A - B \) zawiera wartości, które są w \( A \), ale nie ma ich w \( B \):
[
A - B = {x \mid x \in A \ \text{i} \ x \notin B}
]
Zbiór ten otrzymujemy, filtrując \( \text{grouped} \) po tych kluczach, dla których lista relacji zawiera A, ale nie zawiera B:
[
\text{differenceAB} = {x \mid x \in \text{grouped}, \ \text{'A'} \in \text{grouped}[x] \ \text{i} \ \text{'B'} \notin \text{grouped}[x]}
]
Obliczenie:
Wartości w \( A - B \):
[
A - B = {1, 4, 18, 29, 31, 32, 33, 42, 44, 60, 63, 66, 70, 74, 76, 80, 83, 84, 85, 87, 90, 91, 92, 96}
]
d) Różnica zbiorów \( B - A \)
Różnica zbiorów \( B - A \) zawiera wartości, które są w \( B \), ale nie ma ich w \( A \):
[
B - A = {x \mid x \in B \ \text{and} \ x \notin A}
]
Zbiór ten otrzymujemy, filtrując \( \text{grouped} \) po tych kluczach, dla których lista relacji zawiera B, ale nie zawiera A:
[
\text{differenceBA} = {x \mid x \in \text{grouped}, \ \text{'B'} \in \text{grouped}[x] \ \text{i} \ \text{'A'} \notin \text{grouped}[x]}
]
Obliczenie:
Wartości w \( B - A \):
[
B - A = {2, 3, 5, 11, 12, 13, 14, 16, 21, 22, 24, 27, 37, 40, 41, 43, 51, 52, 54, 57, 59, 61, 67, 69, 73, 79, 81, 89, 93, 97, 99}
]
6. Wyniki operacji
Część 2: Przetwarzanie pliku join.txt
Celem jest wykonanie złączenia (join) między tabelami Customers i Orders na podstawie CustomerID.
1. Dane wejściowe
-
Klienci (Customers):
[
\begin{align*}
& (\text{CustomerID}=1, \ \text{CompanyName}=\text{'Alfreds Futterkiste'}, \ \text{ContactName}=\text{'Maria Anders'}, \ \text{Country}=\text{'Germany'}) \
& (\text{CustomerID}=2, \ \text{CompanyName}=\text{'Ana Trujillo Emparedados y helados'}, \ \text{ContactName}=\text{'Ana Trujillo'}, \ \text{Country}=\text{'Mexico'}) \
& (\text{CustomerID}=3, \ \text{CompanyName}=\text{'Antonio Moreno Taquería'}, \ \text{ContactName}=\text{'Antonio Moreno'}, \ \text{Country}=\text{'Mexico'}) \
& (\text{CustomerID}=4, \ \text{CompanyName}=\text{'NO ORDERS'}, \ \text{ContactName}=\text{'NO ORDERS'}, \ \text{Country}=\text{'NO ORDERS'})
\end{align*}
]
-
Zamówienia (Orders):
[
\begin{align*}
& (\text{OrderID}=10308, \ \text{CustomerID}=1, \ \text{OrderDate}=\text{'2016-09-18'}) \
& (\text{OrderID}=10309, \ \text{CustomerID}=2, \ \text{OrderDate}=\text{'2016-09-19'}) \
& (\text{OrderID}=10310, \ \text{CustomerID}=3, \ \text{OrderDate}=\text{'2016-09-20'}) \
& (\text{OrderID}=10311, \ \text{CustomerID}=3, \ \text{OrderDate}=\text{'2016-09-20'}) \
& (\text{OrderID}=10312, \ \text{CustomerID}=3, \ \text{OrderDate}=\text{'2016-09-20'}) \
& (\text{OrderID}=10313, \ \text{CustomerID}=5, \ \text{OrderDate}=\text{'2016-09-20'})
\end{align*}
]
Uwaga: Klient o CustomerID=4 nie ma żadnych zamówień, a zamówienie z OrderID=10313 jest przypisane do nieznanego klienta CustomerID=5.
2. Mapowanie rekordów na pary klucz-wartość
Każdy rekord z pliku join.txt mapujemy na parę:
-
Dla klientów (Customers):
[
(\text{CustomerID}, (\text{'Customers'}, \text{CompanyName}, \text{ContactName}, \text{Country}))
]
-
Dla zamówień (Orders):
[
(\text{CustomerID}, (\text{'Orders'}, \text{OrderID}, \text{OrderDate}))
]
Przykład:
-
Rekord klienta:
[
\text{'Customers'} \ 1 \ \text{'Alfreds Futterkiste'} \ \text{'Maria Anders'} \ \text{'Germany'} \implies (1, (\text{'Customers'}, \text{'Alfreds Futterkiste'}, \text{'Maria Anders'}, \text{'Germany'}))
]
-
Rekord zamówienia:
[
\text{'Orders'} \ 10308 \ 1 \ \text{'2016-09-18'} \implies (1, (\text{'Orders'}, 10308, \text{'2016-09-18'}))
]
3. Grupowanie po kluczu CustomerID
Grupujemy wszystkie pary po kluczu CustomerID:
[
\text{grouped} = { (\text{CustomerID}, [\text{lista rekordów}]) }
]
Przykład dla CustomerID=1:
[
(1, [(\text{'Customers'}, \text{'Alfreds Futterkiste'}, \text{'Maria Anders'}, \text{'Germany'}), (\text{'Orders'}, 10308, \text{'2016-09-18'})])
]
4. Łączenie informacji o kliencie z zamówieniami
Dla każdego CustomerID:
-
Wyodrębniamy informacje o kliencie:
[
\text{CustomerInfo} = (\text{CompanyName}, \text{ContactName}, \text{Country})
]
-
Zbieramy listę zamówień:
[
\text{Orders} = [(\text{OrderID}, \text{OrderDate})]
]
-
Tworzymy połączone rekordy:
[
(\text{CustomerID}, \text{CustomerInfo}, \text{OrderID}, \text{OrderDate})
]
Przykłady dla poszczególnych CustomerID:
a) CustomerID=1
-
CustomerInfo:
[
(\text{'Alfreds Futterkiste'}, \text{'Maria Anders'}, \text{'Germany'})
]
-
Orders:
[
[(10308, \text{'2016-09-18'})]
]
-
Połączone rekordy:
[
(1, (\text{'Alfreds Futterkiste'}, \text{'Maria Anders'}, \text{'Germany'}), 10308, \text{'2016-09-18'})
]
b) CustomerID=2
-
CustomerInfo:
[
(\text{'Ana Trujillo Emparedados y helados'}, \text{'Ana Trujillo'}, \text{'Mexico'})
]
-
Orders:
[
[(10309, \text{'2016-09-19'})]
]
-
Połączone rekordy:
[
(2, (\text{'Ana Trujillo Emparedados y helados'}, \text{'Ana Trujillo'}, \text{'Mexico'}), 10309, \text{'2016-09-19'})
]
c) CustomerID=3
-
CustomerInfo:
[
(\text{'Antonio Moreno Taquería'}, \text{'Antonio Moreno'}, \text{'Mexico'})
]
-
Orders:
[
[(10310, \text{'2016-09-20'}), (10311, \text{'2016-09-20'}), (10312, \text{'2016-09-20'})]
]
-
Połączone rekordy:
[
\begin{align*}
& (3, (\text{'Antonio Moreno Taquería'}, \text{'Antonio Moreno'}, \text{'Mexico'}), 10310, \text{'2016-09-20'}) \
& (3, (\text{'Antonio Moreno Taquería'}, \text{'Antonio Moreno'}, \text{'Mexico'}), 10311, \text{'2016-09-20'}) \
& (3, (\text{'Antonio Moreno Taquería'}, \text{'Antonio Moreno'}, \text{'Mexico'}), 10312, \text{'2016-09-20'})
\end{align*}
]
d) CustomerID=4
-
CustomerInfo:
[
(\text{'NO ORDERS'}, \text{'NO ORDERS'}, \text{'NO ORDERS'})
]
-
Orders:
[
\text{Brak zamówień}
]
-
Połączone rekordy:
[
\text{Brak, ponieważ nie ma zamówień}
]
e) CustomerID=5
-
CustomerInfo:
[
\text{Brak, nie mamy informacji o kliencie z CustomerID=5}
]
-
Orders:
[
[(10313, \text{'2016-09-20'})]
]
-
Połączone rekordy:
[
\text{Brak, ponieważ nie możemy połączyć zamówienia bez informacji o kliencie}
]
5. Wyniki złączenia
Połączone rekordy dla istniejących klientów i ich zamówień:
-
Dla CustomerID=1:
[
\text{CustomerID: } 1, \ \text{CompanyName: } \text{'Alfreds Futterkiste'}, \ \text{ContactName: } \text{'Maria Anders'}, \ \text{Country: } \text{'Germany'}, \ \text{OrderID: } 10308, \ \text{OrderDate: } \text{'2016-09-18'}
]
-
Dla CustomerID=2:
[
\text{CustomerID: } 2, \ \text{CompanyName: } \text{'Ana Trujillo Emparedados y helados'}, \ \text{ContactName: } \text{'Ana Trujillo'}, \ \text{Country: } \text{'Mexico'}, \ \text{OrderID: } 10309, \ \text{OrderDate: } \text{'2016-09-19'}
]
-
Dla CustomerID=3:
[
\begin{align*}
& \text{CustomerID: } 3, \ \text{CompanyName: } \text{'Antonio Moreno Taquería'}, \ \text{ContactName: } \text{'Antonio Moreno'}, \ \text{Country: } \text{'Mexico'}, \ \text{OrderID: } 10310, \ \text{OrderDate: } \text{'2016-09-20'} \
& \text{CustomerID: } 3, \ \text{CompanyName: } \text{'Antonio Moreno Taquería'}, \ \text{ContactName: } \text{'Antonio Moreno'}, \ \text{Country: } \text{'Mexico'}, \ \text{OrderID: } 10311, \ \text{OrderDate: } \text{'2016-09-20'} \
& \text{CustomerID: } 3, \ \text{CompanyName: } \text{'Antonio Moreno Taquería'}, \ \text{ContactName: } \text{'Antonio Moreno'}, \ \text{Country: } \text{'Mexico'}, \ \text{OrderID: } 10312, \ \text{OrderDate: } \text{'2016-09-20'}
\end{align*}
]
Uwagi:
-
Klient CustomerID=4 nie ma żadnych zamówień, więc nie pojawia się w wynikach złączenia.
-
Zamówienie OrderID=10313 z CustomerID=5 nie pojawia się w wynikach, ponieważ nie mamy informacji o kliencie o CustomerID=5.
Podsumowanie
-
Operacje na zbiorach z pliku relation.txt:
- Wykorzystując MapReduce, dokonaliśmy obliczeń sumy, części wspólnej i różnic zbiorów \( A \) i \( B \).
- Wykorzystaliśmy mapowanie wartości na pary klucz-wartość, grupowanie po kluczu oraz filtrowanie.
-
Operacja złączenia danych z pliku join.txt:
- Przy użyciu MapReduce połączyliśmy informacje o klientach z ich zamówieniami na podstawie wspólnego klucza
CustomerID.
- Mapowaliśmy rekordy na pary klucz-wartość, grupowaliśmy po kluczu oraz łączyliśmy dane podczas fazy redukcji.
-
Wykorzystanie notacji matematycznej (LaTeX):
- Wszystkie operacje zostały przedstawione za pomocą wzorów matematycznych, co pozwala na precyzyjne zrozumienie wykonywanych kroków.
- Dzięki temu możemy lepiej zrozumieć, jak działa przetwarzanie danych w modelu MapReduce.
Zastosowanie MapReduce na dostarczonych danych pozwoliło na efektywne przetworzenie i analizę dużych zbiorów danych, a także na wykonanie operacji zbiorowych oraz złączeń między różnymi zestawami danych. Dzięki temu możemy uzyskać wartościowe informacje z danych, które posiadamy.