環境
- JDK 1.8
- Intellij Idea 2018.1
- Hadoop 2.6.0(Hadoopはローカルにインストールされていません)
- メイベン3.5.4
単語数プロジェクトを作成する
- アイデアで新しいmaven Javaプロジェクトを作成します(maven jdkを少し設定します)
pomの依存関係を構成する
- pom.xmlファイル
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-mapreduce-client-jobclient</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies>
-
マッパークラスを作成する
package com.lens.task; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; import java.io.IOException; import java.util.StringTokenizer; /** * @author lens * @create 2020-02-25 10:24 */ public class VoteCountMapper extends Mapper<Object, Text, Text, IntWritable> { private final static IntWritable one = new IntWritable(1); private Text word = new Text(); @Override protected void map(Object key, Text value, Context context) throws IOException, InterruptedException { StringTokenizer words = new StringTokenizer(value.toString()); while (words.hasMoreTokens()) { word.set(words.nextToken()); context.write(word, one); } } }
-
レデューサークラスの作成
package com.lens.task; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Reducer; import java.io.IOException; /** * @author lens * @create 2020-02-25 10:24 */ public class VoteCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> { private IntWritable result = new IntWritable(); public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int count = 0; for (IntWritable value : values) { count += value.get(); } result.set(count); context.write(key, result); } }
-
voteCountドライバークラスの作成
package com.lens.task; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; /** * @author lens * @create 2020-02-25 10:22 */ public class VoteCount extends Configured implements Tool { public static void main(String[] args) throws Exception { int res = ToolRunner.run(new Configuration(),new VoteCount(),args); System.exit(res); } public int run(String[] args) throws Exception { if (args.length !=2){ System.out.println("Incorrect input, expected: [input] [output]"); System.exit(-1); } Configuration conf = this.getConf(); Job job = new Job(conf, "word count"); job.setJarByClass(VoteCount.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(Text.class); job.setMapperClass(VoteCountMapper.class); job.setReducerClass(VoteCountReducer.class); job.setInputFormatClass(TextInputFormat.class); job.setOutputValueClass(TextOutputFormat.class); job.setMapOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.setInputPaths(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.submit(); return job.waitForCompletion(true) ? 0 : 1; } }
注:ここで、ファイル形式はパッケージをlibにインポートする必要があります
5. wordcountファイル入力ディレクトリ入力を作成します。ファイルの文字数をカウントし、カウント結果を出力します。
まず、入力パスを構成する必要があります。ここでは、プロジェクト(src
同じレベルのディレクトリ)の下に新しいフォルダーを作成し、例としてinput
1つ以上のテキストファイルを追加input
(アップロード)します。
需要注意:File
-> Project Structure
、ポップアップダイアログボックスModules
でアイテムを選択します。ここでは、input
フォルダーはとマークされていExcluded
ます。
動作パラメータを構成する
ここでは、プログラムの実行時にMainクラスと投票数で必要な入出力パスを構成する必要があります。
選択のIntelliJメニューバーRun
- > Edit Configurations
ポップアップダイアログボックスで、クリックしてください +
新しい作成するために、Application
コンフィギュレーションを。構成されたMain class
投票カウントを(あなたが右クリックできる...
選択肢を)、Program arguments
であるinput/ output/
ことを作成するための入力パスinput
フォルダ、出力output(可以不配)
実行中
設定後、メニューバーをクリックRun
- > [ Run 'VoteCount'
プログラムの完了を実行した後に、フォルダの左上があるだろう、MapReduceのこのプログラムの実行を開始しoutput
、part-r-00000
実行した結果です!
入力ファイル
運用実績