8. HDFS
ブロックの
DataNode 保存
ブロック
状態監視
管理 Heartbeat ① ① ③ ②
メタ情報
管理 ② ① ②
NameNode ③ ① ② ①
(Master)
3 1 2
② ① ③ ①
ファイル ① ① ②
クライアント DataNode (Slave)
8 1つのブロックを複数のDataNodeで保存
→ 任意のDataNodeが故障してもデータを失わない
10. Hadoop MapReduce
MapReduceジョブ
TaskTracker Map Map Reduce
管理
状態監視
タスク
① ① ③ ②
管理
Heartbeat
データのローカリティを意識した
JobTracker
タスクの実行
(MASTER)
R M M M
MapReduce
ジョブ
同じ処理の投機的実行
M R R
クライアント
TaskTracker タスク実行待ち
Map,Reduce
10
(SLAVE) タスク実行中
タスクの実行
タスク実行(競争)中
17. WordCountの
public class WordCount { ソースコード
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{ public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
private final static IntWritable one = new IntWritable(1); String[] otherArgs =
private Text word = new Text(); new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
public void map(Object key, Text value, Context context System.err.println("Usage: wordcount <in> <out>");
) throws IOException, InterruptedException { System.exit(2);
StringTokenizer itr = new StringTokenizer(value.toString()); }
while (itr.hasMoreTokens()) { Job job = new Job(conf, "word count");
word.set(itr.nextToken()); job.setJarByClass(WordCount.class);
context.write(word, one); job.setMapperClass(TokenizerMapper.class);
}
Map
job.setCombinerClass(IntSumReducer.class);
}
} job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
public static class IntSumReducer job.setOutputValueClass(IntWritable.class);
extends Reducer<Text,IntWritable,Text,IntWritable> { FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
private IntWritable result = new IntWritable(); FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
public void reduce(Text key, Iterable<IntWritable> values, }
Context context }
) throws IOException, InterruptedException {
Job
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
約50行の記述で分散処理を
result.set(sum);
context.write(key, result);
Reduce
}
17
}
実行できる!
18. Pigによる
WordCount
Rawdata = LOAD '/tmp/' USING PigStorage(',') AS (row:chararray);
Words = FOREACH Rawdata
GENERATE FLATTEN (TOKENIZE((chararray)$0));
Grouped = GROUP Words BY $0;
Counts = FOREACH Grouped GENERATE COUNT(Words), group;
Ordered = ORDER Counts by $0 DESC;
STORE Ordered INTO 'pig-wordcount';
6行でWordCountを実現できる
18
MapReduceを意識しない記述である