zoukankan      html  css  js  c++  java
  • hadoop得知;block数据块;mapreduce实现样例;UnsupportedClassVersionError变态;该项目的源代码相关联

    对于开源的东西。特别是刚出来不久。我认为最好的学习方法是能够看到源代码,doc,样品测试

    为了方便查看源代码,导入与项目相关的源代码


    先前的项目导入源代码是关联了源代码文件


    block数据块,在配置文件hdfs-default.xml中能够查看到,记住要改动不是在这里


    block文件存储块是最主要的单位


    查看block存放位置,配置文件里查看



    假设文件大于64M会占两个块,meta文件是校验文件,第二个文件大于64M,删除文件后,则相应block不在


    datanode存放文件,一个文件能够存放在不同机器上datanode



    mapreduce本身有默认的类,当什么都不写的时候,原样输出

    package com.kane.mr.minidefault;


    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.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    import org.apache.hadoop.util.GenericOptionsParser;

    public class TestDefault {
    public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    //GenericOptionsParser辅助工具类
       //String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
       String[] otherArgs = {"hdfs://centos:9000/kane/mini.txt","hdfs://centos:9000/kane/output"};
       if (otherArgs.length != 2) {
         System.err.println("Usage: wordcount <in> <out>");
         System.exit(2);
       }
       Job job = new Job(conf, "word count");
       job.setJarByClass(TestDefault.class);
       //中间的内容省略就採用默认的类操作,应该是原样输出
      
       FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//输入參数,相应hadoop jar 相应类执行时在后面加的第一个參数
       FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//输出參数
       System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
    }

    然后到处该类为jar包,放到hadoop文件下,执行


    接下来自己创建须要mr执行的源文件,并导入hdfs中


    当我们执行hadoop命令执行时 可能出异常。由于你编写代码的jdk可能和hadoop用到的JVM不匹配


    解决java.lang.UnsupportedClassVersionError问题 
    Jbuilder2006自带了1.5的JDK,但假设你还想继续用JDK1.42。假设仅仅是在Jbuilder2006的tool->configure->JDKs中加入一个JDK1.42或更低版本号,那么即使你仅仅写一个最简单的HelloWorld程序,Jbuilder2006都会给你报出长长一串错误。编写的代码在Jbuilder2005中也无法执行。JBuilder2006在执行编译project时,会针对特定版本号的VM进行编译,默认的是Java 2 SDK, v 5.0 And Late。因此。假设project用的是1.5下面的JDK,碰到都是java.lang.UnsupportedClassVersionError这个错误。



    解决的办法事实上非常easy,仅仅要更改这个选项即可了。详细过程例如以下:
    ----------------------------------------------------------
    1、右键点击project文件,选择属性(properties),
    2、在属性窗体中选择 Build-->Java,在右边的选项中有四个下拉框,就能够看到编译选项了,
    3、当中Compiler和Debug Option能够不用管。仅仅在Languege features和Target VM中选择对应的JDK版本号就能够了,然后确定,一切OK。
    附件中是配置的图片。
    -----------------------------------------------------------
    假设在Target VM中选择了All Java SDKs。那么你的class文件在使用JDK1.1的VM上都能够执行(Jbuilder2006帮助中是这么说的。预计没几个人的机子上还在用JDK1.1吧 :-)

    2.
    Eclipse3.1相同会出现相同的问题。这时候仅仅须要把项目用的JDK与你安装的JDK搞成版本号一样就攻克了.
    步骤:右击你的项目-->属性-->Java Compiler,设置合适的版本号!


    默认的mr程序原样输出



    測试wordcount

    package com.kane.mr;


    import java.io.IOException;
    import java.util.StringTokenizer;


    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Mapper;


    public class MapperClass extends Mapper<Object,Text,Text,IntWritable>{
    public Text keyText=new Text("key");
    public IntWritable intValue=new IntWritable(1);
    @Override
    protected void map(Object key, Text value,
    Context context)
    throws IOException, InterruptedException {
    //获取输入的值
    String str=value.toString();
    //用什么分隔键值,默认空格或 或
    StringTokenizer sTokenizer=new StringTokenizer(str);
    //循环输出。假如是My name is kane 则分四次输出四个单词
    while (sTokenizer.hasMoreElements()) {
    Object object = (Object) sTokenizer.nextElement();
    //这里每一个单词能够看做一个key
    keyText.set(str);
    context.write(keyText, intValue);//匹配一个就加value比如(“My”,1)
    }
    }

    }

    package com.kane.mr;
    import java.io.IOException;
    import org.apache.hadoop.io.IntWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Reducer;
    //map传来的键值就是text 和 intwritable
    public class ReducerClass extends Reducer<Text,IntWritable, Text,IntWritable>{
    public IntWritable intValue= new IntWritable(0);


    @Override
    protected void reduce(Text key, Iterable<IntWritable> values,//假如name出现两次。这里得到的values是 name [1,1]
    Context context)
    throws IOException, InterruptedException {
    int sum=0;
    while (values.iterator().hasNext()) {
    sum+=values.iterator().next().get();

    }
    //这里值用intwritable输出是由于非常多情况下一个mapreduce的输出是下一个mapreduce的输入
    intValue.set(sum);
    context.write(key, intValue);
    }

    }

    package com.kane.mr;
    import org.apache.hadoop.conf.Configuration;
    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.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    import org.apache.hadoop.util.GenericOptionsParser;


    public class WordCounter {
    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(WordCounter.class);
       job.setMapperClass(MapperClass.class);
       //job.setCombinerClass(IntSumReducer.class);
       job.setReducerClass(ReducerClass.class);
       job.setOutputKeyClass(Text.class);
       job.setOutputValueClass(IntWritable.class);
       FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//输入參数,相应hadoop jar 相应类执行时在后面加的第一个參数
       FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//输出參数
       System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
    }



    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    任何抛开业务谈大数据量的sql优化都是瞎扯
    关于优化for循环的注意的事项
    sql调优《二》
    页面加载通过javascript来修改控件属性
    。net内存优化
    oracle调优使用到相关sql
    oracle插入或更新某一个指定列来执行触发器
    webpack4基础入门操作(二)(讲解下webpack的配置内容)
    webpack4基础入门操作(一)
    关于一个WCF调用的服务端和客户端的配置信息集合
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4849688.html
Copyright © 2011-2022 走看看