はじめに
この実験では、Hadoop MapReduce における出力と入力形式のカスタマイズ方法を学び、データを効果的に処理する方法を学びます。ゴースト チューターからのガイド付きの指示により、さまざまな種類のデータを扱うスキルを身につけ、Hadoop エコシステムの潜在力を最大限に引き出すことができます。超常的な領域でのコンピューティングの技術を習得するための刺激的な旅に出ましょう!
この実験では、Hadoop MapReduce における出力と入力形式のカスタマイズ方法を学び、データを効果的に処理する方法を学びます。ゴースト チューターからのガイド付きの指示により、さまざまな種類のデータを扱うスキルを身につけ、Hadoop エコシステムの潜在力を最大限に引き出すことができます。超常的な領域でのコンピューティングの技術を習得するための刺激的な旅に出ましょう!
このステップでは、Hadoop MapReduce の核心部分に突入し、独自の Mapper と Reducer クラスを作成します。
まず、su - hadoop
コマンドを使用して ID を切り替えます。データ ファイル data.txt
は、何人かの会話の内容が格納されている HDFS の /user/hadoop/input
ディレクトリに保存されており、次のコマンドを使用して内容を表示します。
hdfs dfs -cat /user/hadoop/input/data.txt
次に、Mapper
を拡張する WordCountMapper
という名前のカスタム Mapper クラスを作成します。この Mapper は入力データを処理してキーと値のペアを emit しますが、処理するデータは会話の各行の内容であり、人物の名前ではありません。/home/hadoop/
以下の WordCountMapper.java
の map
メソッドを次の例に従って補完します。
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// Convert the Text object to a String
String line = value.toString();
// Tokenize the string using StringTokenizer
StringTokenizer tokenizer = new StringTokenizer(line);
// Iterate through each token and write it to the context
while (tokenizer.hasMoreTokens()) {
// Set the current word
word.set(tokenizer.nextToken().trim());
// Write the word and its count to the context
context.write(word, one);
}
}
次に、次のコマンドを使用して java 8 バージョンのコードをコンパイルします。
javac -source 8 -target 8 -cp $HADOOP_HOME/share/hadoop/common/hadoop-common-3.3.6.jar:$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-3.3.6.jar:. WordCountMapper.java
最後に、Reducer
を拡張する WordCountReducer
という名前のカスタム Reducer クラスを作成します。この Reducer は、各キーの値を集計して最終結果を emit します。/home/hadoop/
以下の WordCountReducer.java
の reduce
メソッドを次の例に従って補完します。
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
// Initialize a variable to store the sum of counts for each word
int sum = 0;
// Iterate through all counts for the current word and calculate the total sum
for (IntWritable value : values) {
sum += value.get();
}
// Set the final count for the current word
result.set(sum);
// Write the word and its final count to the context
context.write(key, result);
}
次に、次のコマンドを使用して java 8 バージョンのコードをコンパイルします。
javac -source 8 -target 8 -cp $HADOOP_HOME/share/hadoop/common/hadoop-common-3.3.6.jar:$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-3.3.6.jar:. WordCountReducer.java
このステップでは、MapReduce ジョブの入出力形式を指定します。
まず、特定のソースからデータを読み取るためのカスタム入力形式を作成しましょう。TextInputFormat
を拡張し、入力形式を処理するために getCurrentValue
メソッドをオーバーライドする PersonInputFormat
というクラスを定義します。/home/hadoop/
以下の PersonInputFormat.java
の getCurrentValue
メソッドを次の例に従って補完します。
@Override
public synchronized Text getCurrentValue() {
// Return the value of the current record, split it according to ":", remove the first and last blanks and set it to the Text object.
Text value = new Text();
Text line = super.getCurrentValue();
String[] parts = line.toString().split(":");
if (parts.length == 2) {
value.set(parts[1].trim());
}
return value;
}
次に、次のコマンドを使用して java 8 バージョンのコードをコンパイルします。
javac -source 8 -target 8 -cp $HADOOP_HOME/share/hadoop/common/hadoop-common-3.3.6.jar:$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-3.3.6.jar:. PersonInputFormat.java
次に、カスタム出力形式を作成しましょう。TextOutputFormat
を拡張する CSVOutputFormat
という名前のクラスを定義します。/home/hadoop/
以下の CSVOutputFormat.java
の write
メソッドを次の例に従って補完します。
// Write the key-value pair to the output stream in CSV format
@Override
public void write(Text key, IntWritable value) throws IOException, InterruptedException {
out.writeBytes(key.toString() + "," + value.toString() + "\n");
}
次に、次のコマンドを使用して java 8 バージョンのコードをコンパイルします。
javac -source 8 -target 8 -cp $HADOOP_HOME/share/hadoop/common/hadoop-common-3.3.6.jar:$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-3.3.6.jar:. CSVOutputFormat.java
この最後のステップでは、以前に作成したカスタム入出力形式を利用するように WordCountDriver
クラスを修正します。
/home/hadoop/
以下の WordCountDriver.java
の main
関数を次の例に従って補完します。
// Set Mapper and Reducer classes
job.setMapperClass(WordCountMapper.class);
job.setReducerClass(WordCountReducer.class);
// Set input format class to custom input format PersonInputFormat
job.setInputFormatClass(PersonInputFormat.class);
// Set output format class to custom output format CSVOutputFormat
job.setOutputFormatClass(CSVOutputFormat.class);
カスタム入出力形式で Hadoop MapReduce ジョブを実行するには、次の手順に従います。
適切な Hadoop 依存関係を使用して WordCountDriver.java
をコンパイルします。
javac -source 8 -target 8 -cp $HADOOP_HOME/share/hadoop/common/hadoop-common-3.3.6.jar:$HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-3.3.6.jar:. WordCountDriver.java
コンパイル済みのクラスを含む JAR ファイルを作成します。
jar -cvf mywordcount.jar *.class
適切な入出力パスで WordCountDriver
クラスを実行します。出力パスが以前に存在しないことを確認してください。
hadoop jar./mywordcount.jar WordCountDriver /user/hadoop/input /output
このコマンドは、定義したカスタム入出力形式を使用して Hadoop MapReduce ジョブを実行します。
結果ファイルが正常に生成されたかどうかを確認するには、次のコマンドを使用します。
hdfs dfs -ls /output
hdfs dfs -cat /output/part.csv
おめでとうございます!この実験では、ゴースト チューターのガイダンスの下、Hadoop MapReduce の複雑さを成功裏に解き明かし、出力と入力形式のカスタマイズを身につけました。マッパーとリデューサー クラスの作成から、分割不可能な入力ファイルの処理などの課題を克服するまで、貴重な洞察を得ました。実践的な演習を通じて、Hadoop MapReduce の機能に対する理解を深め、今や自信を持ってデータの課題に取り組む準備ができています。この旅は、技術力を向上させるだけでなく、Hadoop MapReduce のビッグ データ処理における可能性に対する視点を広げました。