hadoop 2.x 编译打包

11 篇文章 0 订阅

1. 需要编译打包的代码如下:

 

package org.apache.hadoop.examples;

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;
import org.apache.hadoop.util.GenericOptionsParser;

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();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if (otherArgs.length < 2) {
      System.err.println("Usage: wordcount <in> [<in>...] <out>");
      System.exit(2);
    }
    Job job = new Job(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);
    for (int i = 0; i < otherArgs.length - 1; ++i) {
      FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
    }
    FileOutputFormat.setOutputPath(job,
      new Path(otherArgs[otherArgs.length - 1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

 

2.编译

编译之前需要设置classpath,有两种方式一种是修改 /etc/profile文件,另一种是在编译时动态指定classpath

a .修改/etc/profile添加如下代码:

export CLASSPATH=$HDOOP_HOME/share/hadoop/common/hadoop-common-2.5.0.jar:$HDOOP_HOME/share/hadoop/common/lib/commons-cli-1.2.jar:$HDOOP_HOME/share/hadoop/common/lib/hadoop-annotations-2.5.0.jar:$HDOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.5.0.jar

保存并生效 source /etc/profile

javac  -d /root/test/bin/ /root/test/org/apache/hadoop/examples/WordCount.java

注意:$HDOOP_HOME  为hadoop的根目录

          /root/test/bin/  为生成class文件的存放路径

b.动态设置classpath

javac -classpath $HDOOP_HOME/share/hadoop/common/hadoop-common-2.5.0.jar:$HDOOP_HOME/share/hadoop/common/lib/commons-cli-1.2.jar:$HDOOP_HOME/share/hadoop/common/lib/hadoop-annotations-2.5.0.jar:$HDOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.5.0.jar -d /root/test/bin/ /root/test/org/apache/hadoop/examples/WordCount.java

3.打包

切换到打包目录下 cd /root/test/bin/

jar -cvf WordCount.jar org/apache/hadoop/examples/*.class

 

4.运行

hadoop jar WordCount.jar org/apache/hadoop/examples/WordCount hdfs://hadoop1:9000/home/input/ hdfs://hadoop1:9000/home/output/result

注意:hdfs 的/home/input/目录下存放需要统计的文件。

           在hdfs上创建/home/output/目录result目录不用创建,运行程序时会自己创建,否则会提示给文件已经存在

5.查看

hdfs dfs -cat /home/output/result/part-r-00000

         

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值