hadoop WordCount实例

环境:ubuntu14、JAVA_HOME、HADOOP_HOME
环境搭建可见:Ubuntu安装hadoop

1.编写WordCount.java

包含Mapper类和Reducer类

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.LongWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class WordCount {
    public static class WordCountMap extends
            Mapper<LongWritable, Text, Text, IntWritable> {
        private final IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(LongWritable key, Text value, Context context)
                throws IOException, InterruptedException {
            String line = value.toString();
            StringTokenizer token = new StringTokenizer(line);
            while (token.hasMoreTokens()) {
                word.set(token.nextToken());
                context.write(word, one);
            }
        }
    }

    public static class WordCountReduce extends
            Reducer<Text, IntWritable, Text, IntWritable> {
        public void reduce(Text key, Iterable<IntWritable> values,
                Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            context.write(key, new IntWritable(sum));
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = new Job(conf);
        job.setJarByClass(WordCount.class);
        job.setJobName("wordcount");
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setMapperClass(WordCountMap.class);
        job.setReducerClass(WordCountReduce.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        job.waitForCompletion(true);
    }
}

2.编译WordCount.java

语法:

javac
-classpath [包路径1]:[包路径2]
-d [编译的路径] [java的路径]

文件:

java文件:
/opt/data/hadoop/WordCount/WordCount.java
class文件目录 :
/opt/data/hadoop/WordCount/class

命令:

> javac -classpath  /opt/hadoop-1.2.1/hadoop-core-1.2.1.jar:/opt/hadoop-1.2.1/lib/commons-cli-1.2.jar  -d class/  WordCount.java

编译后文件:

3.打包

> jar -cvf wordcount.jar *.class

4.作业提交

文件:

两个输入文件:
/opt/data/hadoop/WordCount/input/file1
/opt/data/hadoop/WordCount/input/file2
file1:
hello world hello hadoop hadoop file system hadoop java api hello java
file2:
new file hadoop file hadoop new world hadoop free home hadoop free school

a.hdfs创建路径
> hadoop fs -mkdir input_wordcount
b.传文件到hdfs
> hadoop fs -put input/* input_wordcount/
c.提交作业
> hadoop jar class/wordcount.jar WordCount input_wordcount output_wordcount
d.看看结果
> hadoop fs -s output_wordcount/part-r-00000

结果:

api     1
file    3
free    2
hadoop  7
hello   3
home    1
java    2
new     2
school  1
system  1
world   2

附 命令行:

root@senselyan-virtual-machine: hadoop jar class/wordcount.jar WordCount input_wordcount output_wordcount
17/12/17 16:33:07 WARN mapred.JobClient: Use GenericOptionsParser for parsing the arguments. Applications should implement Tool for the same.
17/12/17 16:33:07 INFO input.FileInputFormat: Total input paths to process : 2
17/12/17 16:33:07 INFO util.NativeCodeLoader: Loaded the native-hadoop library
17/12/17 16:33:07 WARN snappy.LoadSnappy: Snappy native library not loaded
17/12/17 16:33:07 INFO mapred.JobClient: Running job: job_201712171254_0001
17/12/17 16:33:08 INFO mapred.JobClient:  map 0% reduce 0%
17/12/17 16:33:17 INFO mapred.JobClient:  map 100% reduce 0%
17/12/17 16:33:25 INFO mapred.JobClient:  map 100% reduce 33%
17/12/17 16:33:27 INFO mapred.JobClient:  map 100% reduce 100%
17/12/17 16:33:28 INFO mapred.JobClient: Job complete: job_201712171254_0001
17/12/17 16:33:28 INFO mapred.JobClient: Counters: 29
17/12/17 16:33:28 INFO mapred.JobClient:   Job Counters
17/12/17 16:33:28 INFO mapred.JobClient:     Launched reduce tasks=1
17/12/17 16:33:28 INFO mapred.JobClient:     SLOTS_MILLIS_MAPS=13623
17/12/17 16:33:28 INFO mapred.JobClient:     Total time spent by all reduces waiting after reserving slots (ms)=0
17/12/17 16:33:28 INFO mapred.JobClient:     Total time spent by all maps waiting after reserving slots (ms)=0
17/12/17 16:33:28 INFO mapred.JobClient:     Launched map tasks=2
17/12/17 16:33:28 INFO mapred.JobClient:     Data-local map tasks=2
17/12/17 16:33:28 INFO mapred.JobClient:     SLOTS_MILLIS_REDUCES=9900
17/12/17 16:33:28 INFO mapred.JobClient:   File Output Format Counters
17/12/17 16:33:28 INFO mapred.JobClient:     Bytes Written=83
17/12/17 16:33:28 INFO mapred.JobClient:   FileSystemCounters
17/12/17 16:33:28 INFO mapred.JobClient:     FILE_BYTES_READ=301
17/12/17 16:33:28 INFO mapred.JobClient:     HDFS_BYTES_READ=383
17/12/17 16:33:28 INFO mapred.JobClient:     FILE_BYTES_WRITTEN=156859
17/12/17 16:33:28 INFO mapred.JobClient:     HDFS_BYTES_WRITTEN=83
17/12/17 16:33:28 INFO mapred.JobClient:   File Input Format Counters
17/12/17 16:33:28 INFO mapred.JobClient:     Bytes Read=147
17/12/17 16:33:28 INFO mapred.JobClient:   Map-Reduce Framework
17/12/17 16:33:28 INFO mapred.JobClient:     Map output materialized bytes=307
17/12/17 16:33:28 INFO mapred.JobClient:     Map input records=11
17/12/17 16:33:28 INFO mapred.JobClient:     Reduce shuffle bytes=307
17/12/17 16:33:28 INFO mapred.JobClient:     Spilled Records=50
17/12/17 16:33:28 INFO mapred.JobClient:     Map output bytes=245
17/12/17 16:33:28 INFO mapred.JobClient:     Total committed heap usage (bytes)=350224384
17/12/17 16:33:28 INFO mapred.JobClient:     CPU time spent (ms)=2510
17/12/17 16:33:28 INFO mapred.JobClient:     Combine input records=0
17/12/17 16:33:28 INFO mapred.JobClient:     SPLIT_RAW_BYTES=236
17/12/17 16:33:28 INFO mapred.JobClient:     Reduce input records=25
17/12/17 16:33:28 INFO mapred.JobClient:     Reduce input groups=11
17/12/17 16:33:28 INFO mapred.JobClient:     Combine output records=0
17/12/17 16:33:28 INFO mapred.JobClient:     Physical memory (bytes) snapshot=615907328
17/12/17 16:33:28 INFO mapred.JobClient:     Reduce output records=11
17/12/17 16:33:28 INFO mapred.JobClient:     Virtual memory (bytes) snapshot=2537697280
17/12/17 16:33:28 INFO mapred.JobClient:     Map output records=25
root@senselyan-virtual-machine: hadoop fs -ls output_wordcount
Found 3 items
-rw-r--r--   3 root supergroup          0 2017-12-17 16:33 /user/root/output_wordcount/_SUCCESS
drwxr-xr-x   - root supergroup          0 2017-12-17 16:33 /user/root/output_wordcount/_logs
-rw-r--r--   3 root supergroup         83 2017-12-17 16:33 /user/root/output_wordcount/part-r-00000
root@senselyan-virtual-machine: hadoop fs -cat output_wordcount/part-r-00000
Warning: $HADOOP_HOME is deprecated.

api     1
file    3
free    2
hadoop  7
hello   3
home    1
java    2
new     2
school  1
system  1
world   2
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 201,924评论 5 474
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,781评论 2 378
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 148,813评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,264评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,273评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,383评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,800评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,482评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,673评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,497评论 2 318
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,545评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,240评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,802评论 3 304
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,866评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,101评论 1 258
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,673评论 2 348
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,245评论 2 341

推荐阅读更多精彩内容