Lab 10: Nhập môn Apache Hadoop

1. Giới thiệu:
Hadoop Map/Reduce là một khung nền (software framework) mã nguồn mở, hỗ trợ người lập
trình viết các ứng dụng theo mô hình Map/Reduce. Để hiện thực một ứng dụng theo mô hình
Map/Reduce, sinh viên cần sử dụng các interface lập trình do Hadoop cung cấp như: Mapper,
Reducer, JobConf, JobClient, Partitioner, OutputCollector, Reporter, InputFormat, OutputFormat,
v.v..
Yêu cầu sinh viên thực thi ứng dụng WordCount trên hai mô hình: Pseudo-Distributed Operation
và Fully-Distributed Operation để hiểu rõ hoạt động của mô hình Map/Reduce và kiến trúc HDFS
(Hadoop Distributed FileSystem) 
pdf 6 trang thamphan 26/12/2022 2220
Bạn đang xem tài liệu "Lab 10: Nhập môn Apache Hadoop", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.

File đính kèm:

  • pdflab_10_nhap_mon_apache_hadoop.pdf

Nội dung text: Lab 10: Nhập môn Apache Hadoop

  1. Lab 10: NHẬ P MO N ẬPẬCHE HẬDOOP Biên soạn: ThS. Nguyễn Quang Hùng E-mail: hungnq2@cse.hcmut.edu.vn 1. Giới thiệu: Hadoop Map/Reduce là một khung nền (software framework) mã nguồn mở, hỗ trợ người lập trình viết các ứng dụng theo mô hình Map/Reduce. Để hiện thực một ứng dụng theo mô hình Map/Reduce, sinh viên cần sử dụng các interface lập trình do Hadoop cung cấp như: Mapper, Reducer, JobConf, JobClient, Partitioner, OutputCollector, Reporter, InputFormat, OutputFormat, v.v Yêu cầu sinh viên thực thi ứng dụng WordCount trên hai mô hình: Pseudo-Distributed Operation và Fully-Distributed Operation để hiểu rõ hoạt động của mô hình Map/Reduce và kiến trúc HDFS (Hadoop Distributed FileSystem). 2. Tài liệu hướng dẫn cài đặt Apache Hadoop và MapReduce tutorial: - Hadoop: - Tài liệu hướng dẫn cài đặt Apache Hadoop trên 1 máy tính (Single node setup): - MapReduce Tutorial: 3. Chương trình ví dụ: 3.1. Cài đặt và sử dụng MapReduce SV có thể cài đặt mô hình Single Node Mode hay Pseudo-Distributed Operation trên một máy đơn. Các bước thực hiện như sau:  Download hadoop distribution từ một trong các liên kết sau: http:// hadoop.apache.org  Khởi động môi trường hadoop mapreduce bằng các lệnh sau: $ cd $HADOOP_HOME $ bin/hadoop namenode –format $ bin/start-all.sh
  2. mapred.job.tracker localhost:9001 3.2. Chương trình ví dụ: WordCount.java /* Filename: WordCount.java */ package org.myorg; import java.io.IOException; import java.util.*; import org.apache.hadoop.fs.Path; import org.apache.hadoop.conf.*; import org.apache.hadoop.io.*; import org.apache.hadoop.mapred.*; import org.apache.hadoop.util.*; public class WordCount { public static class Map extends MapReduceBase implements Mapper { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(LongWritable key, Text value, OutputCollector output, Reporter reporter) throws IOException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); output.collect(word, one); } } } public static class Reduce extends MapReduceBase implements Reducer { public void reduce(Text key, Iterator values, OutputCollector output, Reporter reporter) throws IOException { int sum = 0; while (values.hasNext()) { sum += values.next().get(); } output.collect(key, new IntWritable(sum)); } } public static void main(String[] args) throws Exception { JobConf conf = new JobConf(WordCount.class); conf.setJobName("wordcount"); conf.setOutputKeyClass(Text.class); conf.setOutputValueClass(IntWritable.class); conf.setMapperClass(Map.class); conf.setCombinerClass(Reduce.class);
  3. $ bin/hadoop dfs -cat wordcount/input/file* Hello Hadoop Goodbye Hadoop Hello World Bye World $ bin/hadoop dfs -ls wordcount/input Found 2 items -rw-r r 1 hung supergroup 28 2012-05-08 08:15 /user/hung/wordcount/input/file01 -rw-r r 1 hung supergroup 22 2012-05-08 08:15 /user/hung/wordcount/input/file02 $ bin/hadoop jar ~/wordcount.jar org.myorg.WordCount wordcount/input wordcount/output 12/05/08 08:17:38 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same. 12/05/08 08:17:38 INFO mapred.FileInputFormat: Total input paths to process : 2 12/05/08 08:17:38 INFO mapred.JobClient: Running job: job_201205080748_0004 12/05/08 08:17:39 INFO mapred.JobClient: map 0% reduce 0% 12/05/08 08:17:57 INFO mapred.JobClient: map 66% reduce 0% 12/05/08 08:18:17 INFO mapred.JobClient: map 100% reduce 100% 12/05/08 08:18:22 INFO mapred.JobClient: Job complete: job_201205080748_0004 12/05/08 08:18:22 INFO mapred.JobClient: Counters: 30 12/05/08 08:18:22 INFO mapred.JobClient: Combine output records=6 12/05/08 08:18:22 INFO mapred.JobClient: Physical memory (bytes) snapshot=471007232 12/05/08 08:18:22 INFO mapred.JobClient: Reduce output records=5 12/05/08 08:18:22 INFO mapred.JobClient: Virtual memory (bytes) snapshot=1495506944 12/05/08 08:18:22 INFO mapred.JobClient: Map output records=8