9.Hadoop MapReduce例子(单词计数)-Java

    xiaoxiao2022-07-04  108

    1.编写java代码

    (1)创建wordcount测试目录

              mkdir -p ~/wordcount/input

    (2)切换至wordcount测试目录

               cd ~/wordcount

    (3)复制java代码

             sudo gedit WordCount.java

           

            https://hadoop.apache.org/docs/r2.7.7/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html

            import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.conf.Configuration; 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.Mapper; import org.apache.hadoop.mapreduce.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class WordCount { public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>{ private final static IntWritable one = new IntWritable(1); private Text word = new Text(); public void map(Object key, Text value, Context context ) throws IOException, InterruptedException { StringTokenizer itr = new StringTokenizer(value.toString()); while (itr.hasMoreTokens()) { word.set(itr.nextToken()); context.write(word, one); } } } public static class IntSumReducer 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 sum = 0; for (IntWritable val : values) { sum += val.get(); } result.set(sum); context.write(key, result); } } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); Job job = Job.getInstance(conf, "word count"); job.setJarByClass(WordCount.class); job.setMapperClass(TokenizerMapper.class); job.setCombinerClass(IntSumReducer.class); job.setReducerClass(IntSumReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1); } }        

    2.编译java代码

    (1)修改~/.bashrc

              sudo gedit ~/.bashrc

             export PATH=${JAVA_HOME}/bin:${PATH}

             export HADOOP_CLASSPATH=${JAVA_HOME}/lib/tools.jar

    (2)~/.bashrc生效

            source ~/.bashrc

    (3)编译

            hadoop com.sun.tools.javac.Main WordCount.java

    (4)打包

            jar cf wc.jar WordCount*.class

    3.编写测试文本

    (1)以LICENSE.txt为例

            cp /usr/local/hadoop/LICENSE.txt ~/wordcount/input

            ll ~/wordcount/input

    4.上传测试文件至HDFS

    (1)在HDFS创建目录

              hadoop fs -mkdir -p /user/hduser/wordcount/input

    (2)切换至~/wordcount/input目录

              cd ~/wordcount/input

    (3)上传文件到HDFS

              hadoop fs -copyFromLocal LICENSE.txt /user/hduser/wordcount/input

    (4)列出HDFS文件

              hadoop fs -ls  /user/hduser/wordcount/input

    5.运行

    (1)切换目录

              cd ~/wordcount

    (2)运行程序

               hadoop jar wc.jar WordCount /user/hduser/wordcount/input/LICENSE.txt /user/hduser/wordcount/output

    6.查看运行结果

    (1)查看HDFS目录

              hadoop fs -ls /user/hduser/wordcount/output

    (2)查看运行结果

              hadoop fs -cat /user/hduser/wordcount/output/part-r-00000|more

              或

              hadoop fs -get /user/hduser/wordcount/output/part-r-00000

                       

    最新回复(0)