Tiếp cận thứ hai – MapReduce

Một phần của tài liệu Tiểu luận môn điện toán lưới và đám mây XỬ LÝ DỮ LIỆU PHÂN TÁN VỚI HADOOP MAPREDUCE (Trang 39)

CHƯƠNG IV Bài toán Word count với MapReduce

5.2.Tiếp cận thứ hai – MapReduce

MapReduce giải quyết bài toán bằng cách chia nó thành 2 giai đoạn độc lập nhau: giai đoạn map (ánh xạ) và reduce (trả kết quả). Trên thực tế có thêm giai đoạn thứ 3 là pre-

reduce (tiền trả kết quả) được gọi là giai đoạn grouping (nhóm các kết quả). Hai giai đoạn map và reduce được thực hiện phân tán trên các máy cluster.

MAP

Map<String, Map<String, Integer>> output = new HashMap<String, Map<String, Integer>>(); List<MappedItem> mappedItems = new LinkedList<MappedItem>();

Iterator<Map.Entry<String, String>> inputIter = input.entrySet().iterator(); while(inputIter.hasNext()) {

Map.Entry<String, String> entry = inputIter.next(); String file = entry.getKey();

String contents = entry.getValue(); map(file, contents, mappedItems); }

public static void map(String file, String contents, List<MappedItem> mappedItems) { String[] words = contents.trim().split("\\s+");

for(String word: words) {

mappedItems.add(new MappedItem(word, file)); }

}

private static class MappedItem { private final String word;

private final String file;

public MappedItem(String word, String file) { this.word = word;

this.file = file; }

public String getWord() { return word;

}

public String getFile() { return file;

} }

Một danh sách các cặp key/value sẽ được trả về trong giai đoạn Map. Trong vi dụ này, key là từ, value là tên file chứa nó. Sẽ có trường hợp trùng lắp như từ “foo” xuất hiện trong file3.txt đến 3 lần. Kết quả công việc của giai đoạn này cần cho như sau:

[["foo","file2.txt"], ["house","file2.txt"], ["cat","file2.txt"], ["cat","file2.txt"], ["dog","file2.txt"], ["foo","file1.txt"], ["foo","file1.txt"], ["bar","file1.txt"], ["cat","file1.txt"], ["dog","file1.txt"], ["dog","file1.txt"], ["foo","file3.txt"], ["foo","file3.txt"], ["foo","file3.txt"], ["bird","file3.txt"]]

Group

Thực thi việc nhóm các kết quả của giai đoạn map lại, làm đầu vào cho giai đoạn reduce.

Map<String, List<String>> groupedItems = new HashMap<String, List<String>>(); Iterator<MappedItem> mappedIter = mappedItems.iterator();

while(mappedIter.hasNext()) {

MappedItem item = mappedIter.next(); String word = item.getWord();

String file = item.getFile();

List<String> list = groupedItems.get(word); if (list == null) {

list = new LinkedList<String>(); groupedItems.put(word, list); }

list.add(file); }

Các trường hợp trùng lắp sẽ được loại bỏ trong giai đoạn này như trường hợp của từ “foo”. Kết quả như sau: (adsbygoogle = window.adsbygoogle || []).push({});

{bird=[file3.txt], cat=[file2.txt, file2.txt, file1.txt],

house=[file2.txt], bar=[file1.txt], dog=[file2.txt, file1.txt, file1.txt]}

Reduce

Là giai đoạn cuối, xử lý các thông tin đầu vào từ giai đoạn group để tổng hợp và trả kết quả về.

Iterator<Map.Entry<String, List<String>>> groupedIter = groupedItems.entrySet().iterator();

while(groupedIter.hasNext()) {

Map.Entry<String, List<String>> entry = groupedIter.next(); String word = entry.getKey();

List<String> list = entry.getValue();

reduce(word, list, output); }

public static void reduce(String word, List<String> list, Map<String, Map<String, Integer>> output) {

Map<String, Integer> reducedList = new HashMap<String, Integer>(); for(String file: list) {

Integer occurrences = reducedList.get(file); if (occurrences == null) { reducedList.put(file, 1); } else { reducedList.put(file, occurrences.intValue() + 1); } } output.put(word, reducedList); }

Kết quả đầu ra:

{bird={file3.txt=1}, cat={file2.txt=2, file1.txt=1},

dog={file2.txt=1, file1.txt=2}, bar={file1.txt=1}}

Một phần của tài liệu Tiểu luận môn điện toán lưới và đám mây XỬ LÝ DỮ LIỆU PHÂN TÁN VỚI HADOOP MAPREDUCE (Trang 39)