美文网首页
07.Hadoop:MapReduce Helloworld实验

07.Hadoop:MapReduce Helloworld实验

作者: 負笈在线 | 来源:发表于2020-06-26 00:04 被阅读0次

    本节主要内容:

    MapReduce Helloworld实验:

    运行wordcount单词计数案例,计算词语出现的次数

    1.系统环境:

    OS:CentOS Linux release 7.5.1804 (Core)

    CPU:2核心

    Memory:1GB

    运行用户:root

    JDK版本:1.8.0_252

    Hadoop版本:cdh5.16.2

    2.集群各节点角色规划为:

    172.26.37.245 node1.hadoop.com---->namenode,zookeeper,journalnode,hadoop-hdfs-zkfc,resourcenode,historyserver

    172.26.37.246 node2.hadoop.com---->datanode,zookeeper,journalnode,nodemanager,hadoop-client,mapreduce

    172.26.37.247  node3.hadoop.com---->datanode,nodemanager,hadoop-client,mapreduce

    172.26.37.248  node4.hadoop.com---->namenode,zookeeper,journalnode,hadoop-hdfs-zkfc

    实验步骤

    1.在HDFS文件系统上建立input文件夹(Node1节点)

           # sudo -u hdfs hadoop fs -mkdir -p /user/cloudera/wordcount/input

    2.建立测试文本(Node1节点)

    在resourcenode上创建一个空文件夹,并进入

           # cd /

           # echo "Hello World Bye World" > file0

           # echo "Hello Hadoop Goodbye Hadoop" > file1

    将文件上传到hdfs中

           # sudo -u hdfs hadoop fs -put file* /user/cloudera/wordcount/input

    3.编译WordCount.jave

           # vim 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<LongWritable, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);

        private Text word = new Text();

        public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> 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<Text, IntWritable, Text, IntWritable> {

        public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> 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);

        conf.setReducerClass(Reduce.class);

        conf.setInputFormat(TextInputFormat.class);

        conf.setOutputFormat(TextOutputFormat.class);

        FileInputFormat.setInputPaths(conf, new Path(args[0]));

        FileOutputFormat.setOutputPath(conf, new Path(args[1]));

        JobClient.runJob(conf);

      }

    }

    编译

           # mkdir wordcount_classes

           # javac -cp /usr/lib/hadoop/*:/usr/lib/hadoop/client-0.20/* -d wordcount_classes WordCount.java

    成功的话没有任何回应,但是在 wordcount_classes 里面出现了org文件夹

           # ll wordcount_classes

    total 0

    drwxr-xr-x 3 root root 19 Jun 25 22:41 org

    4.创建jar

           # jar -cvf wordcount.jar -C wordcount_classes/ .

    added manifest

    adding: org/(in = 0) (out= 0)(stored 0%)

    adding: org/myorg/(in = 0) (out= 0)(stored 0%)

    adding: org/myorg/WordCount$Map.class(in = 1938) (out= 797)(deflated 58%)

    adding: org/myorg/WordCount$Reduce.class(in = 1611) (out= 647)(deflated 59%)

    adding: org/myorg/WordCount.class(in = 1534) (out= 753)(deflated 50%)

    把这个 wordcount.jar移动到 /data/

           # mv wordcount.jar /data/

    因为hdfs用户的根目录是/var/lib/hadoop-hdfs,所以我们要cd到刚刚有jar文件的目录

           # cd /data

           # sudo -u hdfs  hadoop jar wordcount.jar org.myorg.WordCount /user/cloudera/wordcount/input /user/cloudera/wordcount/output

    20/06/25 22:49:32 INFO client.RMProxy: Connecting to ResourceManager at node1.hadoop.com/172.26.37.245:8032

    20/06/25 22:49:33 INFO client.RMProxy: Connecting to ResourceManager at node1.hadoop.com/172.26.37.245:8032

    20/06/25 22:49:36 WARN mapreduce.JobResourceUploader: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.

    20/06/25 22:49:37 INFO mapred.FileInputFormat: Total input paths to process : 2

    20/06/25 22:49:37 INFO mapreduce.JobSubmitter: number of splits:3

    20/06/25 22:49:38 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1593100696092_0001

    20/06/25 22:49:41 INFO impl.YarnClientImpl: Submitted application application_1593100696092_0001

    20/06/25 22:49:41 INFO mapreduce.Job: The url to track the job: http://node1.hadoop.com:8088/proxy/application_1593100696092_0001/

    20/06/25 22:49:41 INFO mapreduce.Job: Running job: job_1593100696092_0001

    20/06/25 22:50:42 INFO mapreduce.Job: Job job_1593100696092_0001 running in uber mode : false

    20/06/25 22:50:42 INFO mapreduce.Job:  map 0% reduce 0%

    20/06/25 22:52:28 INFO mapreduce.Job:  map 33% reduce 0%

    20/06/25 22:52:50 INFO mapreduce.Job:  map 33% reduce 11%

    20/06/25 22:53:19 INFO mapreduce.Job:  map 67% reduce 11%

    20/06/25 22:53:22 INFO mapreduce.Job:  map 67% reduce 22%

    20/06/25 22:53:46 INFO mapreduce.Job:  map 100% reduce 22%

    20/06/25 22:53:48 INFO mapreduce.Job:  map 100% reduce 100%

    20/06/25 22:53:50 INFO mapreduce.Job: Job job_1593100696092_0001 completed successfully

    20/06/25 22:53:50 INFO mapreduce.Job: Counters: 50

            File System Counters

                    FILE: Number of bytes read=79

                    FILE: Number of bytes written=585283

                    FILE: Number of read operations=0

                    FILE: Number of large read operations=0

                    FILE: Number of write operations=0

                    HDFS: Number of bytes read=362

                    HDFS: Number of bytes written=41

                    HDFS: Number of read operations=12

                    HDFS: Number of large read operations=0

                    HDFS: Number of write operations=2

            Job Counters

                    Killed map tasks=2

                    Launched map tasks=5

                    Launched reduce tasks=1

                    Data-local map tasks=5

                    Total time spent by all maps in occupied slots (ms)=448613

                    Total time spent by all reduces in occupied slots (ms)=77135

                    Total time spent by all map tasks (ms)=448613

                    Total time spent by all reduce tasks (ms)=77135

                    Total vcore-milliseconds taken by all map tasks=448613

                    Total vcore-milliseconds taken by all reduce tasks=77135

                    Total megabyte-milliseconds taken by all map tasks=459379712

                    Total megabyte-milliseconds taken by all reduce tasks=78986240

            Map-Reduce Framework

                    Map input records=2

                    Map output records=8

                    Map output bytes=82

                    Map output materialized bytes=91

                    Input split bytes=309

                    Combine input records=8

                    Combine output records=6

                    Reduce input groups=5

                    Reduce shuffle bytes=91

                    Reduce input records=6

                    Reduce output records=5

                    Spilled Records=12

                    Shuffled Maps =3

                    Failed Shuffles=0

                    Merged Map outputs=3

                    GC time elapsed (ms)=2101

                    CPU time spent (ms)=47590

                    Physical memory (bytes) snapshot=672563200

                    Virtual memory (bytes) snapshot=10217422848

                    Total committed heap usage (bytes)=379858944

            Shuffle Errors

                    BAD_ID=0

                    CONNECTION=0

                    IO_ERROR=0

                    WRONG_LENGTH=0

                    WRONG_MAP=0

                    WRONG_REDUCE=0

            File Input Format Counters

                    Bytes Read=53

            File Output Format Counters

                    Bytes Written=41

    5.查看结果

           # sudo -u hdfs hdfs dfs -cat /user/cloudera/wordcount/output/part-00000

    Bye    1

    Goodbye 1

    Hadoop  2

    Hello  2

    World  2

    6.删除结果

    如果你想再运行一次教程就要先删除掉结果

           # sudo -u hdfs dfs -rm -r /user/cloudera/wordcount/output

    7.JobHistory

    http://172.26.37.245:19888/jobhistory/

    可以看到执行过的任务

    2020.06.25 22:49:39 EDT 2020.06.25 22:50:33 EDT 2020.06.25 22:53:47 EDT job_1593100696092_0001 wordcount hdfs root.hdfs SUCCEEDED 3 3 1 1

    相关文章

      网友评论

          本文标题:07.Hadoop:MapReduce Helloworld实验

          本文链接:https://www.haomeiwen.com/subject/wawyfktx.html