大数据学习日记day1

复习

  • hdfs的读写
  • secondary namenode的工作原理
  • shell脚本定时采集数据到hdfs

mapreduce

  • 是一个编程框架
  • 分为两个阶段:
    1. map阶段,task并发实例各司其职
    2. reduce阶段,task并发实例依然各司其职,但依赖第一阶段的task并发实例
  • mapreduce只能分为1个map阶段和1个reduce阶段
  • 可以通过多个mapreduce串联解决大型复杂问题
  • mr application master的作用

实践

  • wordcount案例
    1. mapper类
public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
    //map方法的生命周期:  框架每传一行数据就被调用一次
    //key :  这一行的起始点在文件中的偏移量
    //value: 这一行的内容
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //拿到一行数据转换为string
        String line = value.toString();
        //将这一行切分出各个单词
        String[] words = line.split(" ");
        //遍历数组,输出<单词,1>
        for(String word:words){
            context.write(new Text(word), new IntWritable(1));
        }
    }
}
2. reducer类
//生命周期:框架每传递进来一个kv 组,reduce方法被调用一次
    @Override
    protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
        //定义一个计数器
        int count = 0;
        //遍历这一组kv的所有v,累加到count中
        for(IntWritable value:values){
            count += value.get();
        }
        context.write(key, new IntWritable(count));
    }
}
3.  定义一个主类,用来描述job并提交job
    //把业务逻辑相关的信息(哪个是mapper,哪个是reducer,要处理的数据在哪里,输出的结果放哪里……)描述成一个job对象
    //把这个描述好的job提交给集群去运行
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job wcjob = Job.getInstance(conf);
        //指定我这个job所在的jar包
//      wcjob.setJar("/home/hadoop/wordcount.jar");
        wcjob.setJarByClass(WordCountRunner.class);

        wcjob.setMapperClass(WordCountMapper.class);
        wcjob.setReducerClass(WordCountReducer.class);
        //设置我们的业务逻辑Mapper类的输出key和value的数据类型
        wcjob.setMapOutputKeyClass(Text.class);
        wcjob.setMapOutputValueClass(IntWritable.class);
        //设置我们的业务逻辑Reducer类的输出key和value的数据类型
        wcjob.setOutputKeyClass(Text.class);
        wcjob.setOutputValueClass(IntWritable.class);

        //指定要处理的数据所在的位置
        FileInputFormat.setInputPaths(wcjob, "hdfs://hdp-server01:9000/wordcount/data/big.txt");
        //指定处理完成之后的结果所保存的位置
        FileOutputFormat.setOutputPath(wcjob, new Path("hdfs://hdp-server01:9000/wordcount/output/"));

        //向yarn集群提交这个job
        boolean res = wcjob.waitForCompletion(true);
        System.exit(res?0:1);
    }
  • 用debug追踪FileInputFormat()的执行

你可能感兴趣的:(大数据学习笔记)