Hadoop之MapReduce开发

以经典的WordCount为例,介绍如何开发MapReduce程序,所使用的开发环境为Eclipse 4.3,Hadoop 2.6。

一、环境配置

首先在Eclipse里创建一个空的maven工程,然后编辑pom.xml文件引入hadoop所需依赖项。这里分两种情况,一般使用标准hadoop就够了;我这次由于集群环境使用的是CDH发行版,所以使用CDH提供的repository。

情况1:标准hadoop的pom配置:

<dependency>
 <groupId>org.apache.hadoop</groupId>
 <artifactId>hadoop-client</artifactId>
 <version>2.6.0</version>
</dependency>

情况2:CDH发行版的pom配置(参考cloudera blog的文章,全部版本号可以在这里看到):

<repositories>
 <repository>
 <id>cloudera-releases</id>
 <url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
 <releases>
 <enabled>true</enabled>
 </releases>
 <snapshots>
 <enabled>false</enabled>
 </snapshots>
 </repository>
</repositories>

<dependency>
 <groupId>org.apache.hadoop</groupId>
 <artifactId>hadoop-client</artifactId>
 <version>2.6.0-mr1-cdh5.5.0</version>
</dependency>

二、编写代码

WordCount相当于MapReduce的HelloWord,基本上是一个最小的MapReduce程序,完整代码可以在Hadoop官网教程里找到,为方便查看复制如下,并添加了若干注释:

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;

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();

    /**
    * 这里的value表示文件中的一行文本
    * 这里的key表示这行文本相对文件开头的偏移量
    */
    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();

    /**
    * 这里的values表示单词对应的计数值(即one)列表
    * 这里的key表示单词
    */
    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();
    Job job = Job.getInstance(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);
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

mapper和recuder的代码看起来很简洁,实际上是因为hadoop为我们做了很多工作:

在调用map之前,hadoop会先将input目录中的文件切分为若干个FileSplit,对小文件每个文件就是一个FileSplit,对超过64M(hadoop的缺省块大小)的文件,每个文件会产生大于1个的FileSplit。

对每个FileSplit,hadoop再使用RecordReader处理为<key, value>对。

由于hadoop缺省使用TextInputFormat处理文件(见JobContextImpl#getInputFormatClass()方法),而TextInputFormat对应的RecordReader类型为LineRecordReader,因此处理的结果是按行的,具体来讲,是<偏移量, 一行文本>的形式。

以上是map阶段hadoop对数据的一些处理,更完整的mapreduce过程在这里先不详细展开了。

参考链接:

Git强制使用远程代码覆盖本地代码

一、若不想保留本地更改:

方法1

git reset --hard
git pull

方法2

git fetch --all  
git reset --hard origin/master 

其实上面两个方法是一样的,因为pull命令等于fetch+merge,所以经过reset以后的pull和fetch命令的效果没差别。

二、若还想保留本地更改:

git stash
git pull
git stash pop

stash是把当前工作目录下的修改(未commit的所有文件)暂存起来,stash pop是从暂存区里恢复这些修改。

参考:

Maven配置不同平台下的SWT依赖

通常在pom.xml里我们可以像对其他类库一样为项目引入SWT依赖:

<dependency>
 <groupId>org.eclipse.swt</groupId>
 <artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
 <version>4.3</version>
</dependency>

但是SWT特殊在于,不同平台(platform)下需要使用不同的artifactId,例如上面的代码只适用于windows开发环境,在linux环境下可以编译但无法运行。

好在Maven支持根据os.family定义不同的属性值,所以我们可以像下面这样定义两个profile,然后让Maven自动选择合适的artifactId:

<profiles>
 <profile>
 <id>os_linux</id>
 <activation>
 <os>
 <family>unix</family>
 </os>
 </activation>
 <properties>
 <swt-artifactId>org.eclipse.swt.gtk.linux.x86_64</swt-artifactId>
 </properties>
 </profile>
 <profile>
 <id>os_windows</id>
 <activation>
 <os>
 <family>windows</family>
 </os>
 </activation>
 <properties>
 <swt-artifactId>org.eclipse.swt.win32.win32.x86_64</swt-artifactId>
 </properties>
 </profile>
</profiles>

<dependency>
 <groupId>org.eclipse.swt</groupId>
 <artifactId>${swt-artifactId}</artifactId>
 <version>4.3</version>
</dependency>

参考链接: