前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >浅谈mapreduce程序部署

浅谈mapreduce程序部署

作者头像
用户5166556
发布2019-04-16 14:54:55
4630
发布2019-04-16 14:54:55
举报

虽然我们在虚拟机客户端上能很快通过shell命令,进行执行一些已经封装好实例程序,但是在应用中还是是自己写程序,然后部署到服务器中去,下面,我通过程序进行浅谈一个程序的部署过程。

在启动Hadoop之后,然后把程序达成可执行的jar包,并把相应的第三方jar包 包含进去。执行hadoop    jar   XXX. +驱动名称。

代码语言:javascript
复制
package com.mapred;

import java.io.IOException;
import java.io.PrintStream;
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.Mapper.Context;
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 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> <out>");
      System.exit(2);
    }*/
    Job job = new Job(conf, "word count");
    job.setJarByClass(WordCount.class);
    FileInputFormat.addInputPath(job, new Path("hdfs://ubuntu:9000/Input"));
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    job.setMapOutputKeyClass(Text.class);
	job.setMapOutputValueClass(IntWritable.class);
   
    FileOutputFormat.setOutputPath(job, new Path("hdfs://ubuntu:9000/output09"));
    job.waitForCompletion(true);
  }

  public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable>
  {
    private IntWritable result;

    public IntSumReducer()
    {
      this.result = new IntWritable();
    }

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

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

    public TokenizerMapper()
    {
      this.word = new Text();
    }

    public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        this.word.set(itr.nextToken());
        context.write(this.word, one);
      }
    }
  }
}

在执行的过程中要注意以下几个事项:

首先要注意的就是,文件在hdfs上的位置是否正确,记住只需要指定文件夹名称即可,里面有多少具体文件,Hadoop都一并给你处理,注意观察在执行过程中所出现的异常。

因为我在执行和调试过程中也出现很多异常,我认为这些异常是情况很多的,希望有兴趣的同学和我一起交流,共同分析和研究它。

1:注意观察虚拟机终端中报的错误,根据错误进行相应改进,因为关联jar较多,所以当提示你少相应的某一个包时,你要注意引进过来。

2:这里我是部署到虚拟机中执行的,不过在网上看过很多资料说,通过Eclipse也可以直接进行数据的处理,但是我没有调试成功,希望大家谁成功了,告知我一声。我感觉我是版本和虚拟机可能没有绑定好。

3:用Java命令(Java -jar   XXX.jar)也可以执行。而且在这种情况下不需要安装和部署Hadoop环境。但是因为我的Java虚拟机在运行时,老是提示内存不足。没有成功,我还是在Hadoop环境和总成功的。大家可以尝试并交流着去做一下。这个东西,处理数据有点意思。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014年07月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档