hadoop MR maven级代码模板是怎样的
创始人
2024-12-26 00:22:36
0次

关于Maven的使用就不再啰嗦了,网上很多,并且这么多年变化也不大,这里仅介绍怎么搭建Hadoop的开发环境。

1. 首先创建工程

mvn archetype:generate -DgroupId=my.hadoopstudy -DartifactId=hadoopstudy -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

2. 然后在pom.xml文件里添加hadoop的依赖包hadoop-common, hadoop-client, hadoop-hdfs,添加后的pom.xml文件如下

  4.0.0  my.hadoopstudy  hadoopstudy  jar  1.0-SNAPSHOT  hadoopstudy  http://maven.apache.org            org.apache.hadoop      hadoop-common      2.5.1              org.apache.hadoop      hadoop-hdfs      2.5.1              org.apache.hadoop      hadoop-client      2.5.1              junit      junit      3.8.1      test      

3. 测试3.1 首先我们可以测试一下hdfs的开发,这里假定使用上一篇Hadoop文章中的hadoop集群,类代码如下

package my.hadoopstudy.dfs;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.fs.FSDataOutputStream;import org.apache.hadoop.fs.FileStatus;import org.apache.hadoop.fs.FileSystem;import org.apache.hadoop.fs.Path;import org.apache.hadoop.io.IOUtils;import java.io.InputStream;import java.net.URI;public class Test {  public static void main(String[] args) throws Exception {    String uri = "hdfs://9.111.254.189:9000/";    Configuration config = new Configuration();    FileSystem fs = FileSystem.get(URI.create(uri), config);    // 列出hdfs上/user/fkong/目录下的所有文件和目录    FileStatus[] statuses = fs.listStatus(new Path("/user/fkong"));    for (FileStatus status : statuses) {      System.out.println(status);    }    // 在hdfs的/user/fkong目录下创建一个文件,并写入一行文本    FSDataOutputStream os = fs.create(new Path("/user/fkong/test.log"));    os.write("Hello World!".getBytes());    os.flush();    os.close();    // 显示在hdfs的/user/fkong下指定文件的内容    InputStream is = fs.open(new Path("/user/fkong/test.log"));    IOUtils.copyBytes(is, System.out, 1024, true);  }
}

3.2 测试MapReduce作业测试代码比较简单,如下:

package my.hadoopstudy.mapreduce;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;import java.io.IOException;public class EventCount {  public static class MyMapper extends Mapper{    private final static IntWritable one = new IntWritable(1);    private Text event = new Text();    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {      int idx = value.toString().indexOf(" ");      if (idx > 0) {        String e = value.toString().substring(0, idx);        event.set(e);        context.write(event, one);      }    }  }  public static class MyReducer extends Reducer {    private IntWritable result = new IntWritable();    public void reduce(Text key, Iterable 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: EventCount  ");      System.exit(2);    }    Job job = Job.getInstance(conf, "event count");    job.setJarByClass(EventCount.class);    job.setMapperClass(MyMapper.class);    job.setCombinerClass(MyReducer.class);    job.setReducerClass(MyReducer.class);    job.setOutputKeyClass(Text.class);    job.setOutputValueClass(IntWritable.class);    FileInputFormat.addInputPath(job, new Path(otherArgs[0]));    FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));    System.exit(job.waitForCompletion(true) ? 0 : 1);  }
}

运行“mvn package”命令产生jar包hadoopstudy-1.0-SNAPSHOT.jar,并将jar文件复制到hadoop安装目录下

这里假定我们需要分析几个日志文件中的Event信息来统计各种Event个数,所以创建一下目录和文件

/tmp/input/event.log.1/tmp/input/event.log.2/tmp/input/event.log.3

因为这里只是要做一个列子,所以每个文件内容可以都一样,假如内容如下

JOB_NEW ...
JOB_NEW ...
JOB_FINISH ...
JOB_NEW ...
JOB_FINISH ...

然后把这些文件复制到HDFS上

$ bin/hdfs dfs -put /tmp/input /user/fkong/input

运行mapreduce作业

$ bin/hadoop jar hadoopstudy-1.0-SNAPSHOT.jar my.hadoopstudy.mapreduce.EventCount /user/fkong/input /user/fkong/output

查看执行结果

$ bin/hdfs dfs -cat /user/fkong/output/part-r-00000

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...