你好,游客 登录 注册 搜索
背景:
阅读新闻

MapReduce经典案例分享

合并文档

[日期:2014-01-18] 来源:Linux社区  作者:ciznx [字体:大 中 小]
资源文件file1
test
hello
word
资源文件file2
happy
birthday
this
is
a
test
最终的结果
hadoop
test
hello
word
happy
birthday
this
is
a
test
 
分析:将两个文件合并成一个文件,是一个很简单的案例。设想我们可以将value设为空,这样就只有key在输出的时候直接数据就可以了。map过程将两个文件的每一行设为key,值设为空。在Reduce阶段只用将map阶段整理好的数据输出就可以了。
 
实现:

package com.bwzy.hadoop;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configured;
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.Mapper.Context;
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;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import com.bwzy.hadoop.WordCount.Map;
import com.bwzy.hadoop.WordCount.Reduce;
public class HeBing extends Configured implements Tool {
    public static class Map
            extends Mapper<LongWritable, Text, Text, Text> {
        public void map(LongWritable key, Text value, Context context) {
            String line = value.toString();
            try {
                context.write(new Text(line), new Text(""));
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static class Reduce extends
            Reducer<Text, Text, Text, Text> {
        public void reduce(Text key, Iterable<Text> values,
                Context context) throws IOException, InterruptedException {
            context.write(key, new Text(""));
        }
    }
    @Override
    public int run(String[] arg0) throws Exception {
        Job job = new Job(getConf());
        job.setJobName("HeBing");
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);
        job.setMapperClass(Map.class);
        job.setCombinerClass(Reduce.class);
        job.setReducerClass(Reduce.class);
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
        FileInputFormat.setInputPaths(job, new Path(arg0[0]));
        FileOutputFormat.setOutputPath(job, new Path(arg0[1]));
        boolean success = job.waitForCompletion(true);
        return success ? 0 : 1;
    }
    public static void main(String[] args) throws Exception {
        int ret = ToolRunner.run(new HeBing(), args);
        System.exit(ret);
    }
}

运行:
1:将程序打包
选中打包的类-->右击-->Export-->java-->JAR file--填入保存路径-->完成
2:将jar包拷贝到hadoop的目录下。(因为程序中用到来hadoop的jar包)
3:将资源文件上传到定义的hdfs目录下
创建hdfs目录命令(在hadoop已经成功启动的前提下):hadoop fs -mkdir /自定义/自定义/input
上传本地资源文件到hdfs上:hadop fs -put -copyFromLocal /home/user/Document/file1 /自定义/自定义/input
hadop fs -put -copyFromLocal /home/user/Document/file2 /自定义/自定义/input
4:运行MapReduce程序:
hadoop jar /home/user/hadoop-1.0.4/HeBing.jar com.bwzy.hadoop.HeBing /自定义/自定义/input /自定义/自定义/output
 
说明:hadoop运行后会自动创建/自定义/自定义/output目录,在该目录下会有两个文件,其中一个文件中存放来MapReduce运行的结果。如果重新运行该程序,需要将/自定义/自定义/output目录删除,否则系统认为该结果已经存在了。
5:运行的结果为
hadoop
test
hello
word
happy
birthday
this
is
a
test
 
更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13
linux
【内容导航】
第1页:求平均分数 第2页:统计单词数
第3页:合并文档
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款