Hadoop入门案例

来源:互联网 发布:淘宝网站类型有哪些 编辑:程序博客网 时间:2024/05/29 02:29

一、在上一篇的Hadoop环境准备和基本测试完成之后,再进行入门案例包括字符统计主要是为了熟练运用mapperReduce,其他的、最高城市温度查询、订单联合查询会后续写出

在之前的测试环境上还需要引入两个自定义jar包,Hadoop-yarn、Hadoop-mapperReduce,步骤同之前引入common和hdfs一样,不重复说,接下里需要配置下hadoop的yarn配置,

修改etc/hadoop/yarn|mapred-site.xml
      yarn-site.xml
      <property>
        <name>yarn.nodemanager.aux-services</name>
        <value>mapreduce_shuffle</value>
       </property>
       <property>
        <name>yarn.resourcemanager.hostname</name>
        <value>yecy</value>
       </property>
     mapred-site.xml
    
    <property>
        <name>mapreduce.framework.name</name>
        <value>yarn</value>
     </property>
启动|停止yarn命令
  ./sbin/start|stop-yarn.sh

二、字符统计

1.先在本地创建,再上传HDFS一份测试数据:testdata 

this is a demo
he is a boy
she is a girl
good study good
tomror is beatuful day
are you sure
i am not sure what are you speaking
ok bye bye

  用命令 ./bin/hdfs dfs -appendToFile /root/testTeldata /demo/src/testdata 

mapper端:

** *   KEYIN:表示数据在存储文件中的偏移量 * VALUEIN:表示一行文本数据 * KEYOUT:统计的依据 * VALUEOUT:统计的结果 * map======= * 可以理解成mapper端是个很大的map存放很多的键值对,类似于[this:1,is:1,this:1,he:1,a:1] * shuffle====洗牌(合并、排序、下载) *  this:[1,1] *  is:[1] *  he:[1] *  a:[1] * shuffle====洗牌 * * reduce===== * [this:[2],is:[1],he:[1],a[1]] */public class wordMapper extends Mapper<LongWritable, Text, Text, IntWritable> {    @Override    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {        String line = value.toString();//每行数据        String[] words = line.split(" ");        for (String word : words) {            context.write(new Text(word), new IntWritable(1));        }    }}
reduce端:

public class wordReduce extends Reducer<Text, IntWritable, Text, IntWritable> {    @Override    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {        int total =0;        for (IntWritable v : values) {            total+=v.get();        }        context.write(key, new IntWritable(total));    }}
任务提交

public class wordCommit {    public  static  void  main(String[] args) throws Exception{        //1、创建Job        Configuration conf = new Configuration();        conf.addResource("core-site.xml");        conf.addResource("hdfs-site.xml");        conf.addResource("yarn-site.xml");        conf.addResource("mapred-site.xml");        conf.set("mapreduce.job.jar", "wc.jar");        Job job =Job.getInstance(conf);        //设置在哪个jar包下运行        job.setJarByClass(wordCommit.class);        //2、设置数据输入、输出类型        //实现了对切片数据的读取,为mapper提供依据        //实现对切片逻辑的计算规则        job.setInputFormatClass(TextInputFormat.class);        job.setOutputFormatClass(TextOutputFormat.class);        //3、从哪里读,往哪里写        Path src = new Path("/demo/wc");        TextInputFormat.addInputPath(job, src);        Path dst = new Path("/demo/wordCount");//输出文件一定不能存在        TextOutputFormat.setOutputPath(job, dst);        //4、关联数据处理规则        job.setMapperClass(wordMapper.class);        job.setReducerClass(wordReduce.class);        //5、告知hadoop key-value的输出类型        job.setMapOutputKeyClass(Text.class);        job.setMapOutputValueClass(IntWritable.class);        //输出类型是根据需求而定        job.setOutputKeyClass(Text.class);        job.setOutputValueClass(IntWritable.class);        //6提交任务        job.waitForCompletion(true);    }}
为了方便本地操作,需要在src下添加配置文件,在hadoop下都有

core-site.xml
hdfs-site.xml
mapred-site.xml
yarn-site.xml

把上边三个类打成jar包放在项目里,

运行main函数,

去hadoop /demo/wordCount下找

./bin/hdfs dfs -ls /demo/wordCount


这个part-r-00000就是输出结果

 ./bin/hdfs dfs -cat /demo/wordCount/part-r-00000,我们看下结果:和预期的一样



追源码会发现mapperReduce最核心的是InputFormat 和outPutFormat 因为输入和输出,决定了你想要的类型,

FileInputFormat
      1.TextInputFormat 
    a)key :LongWriteable 文件读取字节的偏移量
b)value:Text 标示文本文件的一行记录
C)切片计算:在一个文件为单位,默认切片大小等于block的大小
  2.NLineInputFormat
    a)key :LongWriteable 文件读取字节的偏移量
b)value:Text 标示文本文件的一行记录
C)切片计算:在一个文件为单位,默认切片按照行去计算切片
附注:通过设置mapreduce.input.lineinputformat.linespermap该选项设置一个切片读取多少行数据
  3.KeyValueTextInputFormat
    a)key :Text  
b)value:Text  
C)切片计算:在一个文件为单位,默认切片大小等于block的大小
  4.MultiInputs (重点掌握)
    
  5.CombineTextInputFormat:针对于文本小文件的MR解决方案
    a)key :LongWriteable 文件读取字节的偏移量
b)value:Text 标示文本文件的一行记录
C)切片计算:默认切片大小等于block的大小

,接下来我们会总结解析hadoop的源码以及整个mapper和reduce的过程





原创粉丝点击