hadoop linuxコミットタスク

3925 ワード

jobcommit
package WordCount;

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;

public class LinuxS {

    public static void main(String[] args) throws Exception {
        System.setProperty("HADOOP_USER_NAME","root");//  jvm  


        Configuration conf = new Configuration();//

        //  hdfs   
        conf.set("fs.defaultFS", "hdfs://192.168.100.10:9000");
        conf.set("mapreduce.framework.name", "yarn");
        conf.set("yarn.resourcemanager.hostname","192.168.100.10");
        //           ,     
        conf.set("yarn.scheduler.maximum-allocation-mb","200");
        conf.set("yarn.app.mapreduce.am.resource.mb","200");
        conf.set("yarn.nodemanager.resource.memory-mb","200");

        Job job = Job.getInstance(conf);

        //mapper,reducer,submitter   
        job.setJarByClass(LinuxS.class);
        job.setMapperClass(CountMapper.class);
        job.setReducerClass(CountReducer.class);
        //mapper,reducer  key value   
        job.setMapOutputKeyClass(Text.class);//mapper
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);//reducer
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job, new Path("/wordcount/input"));
        FileOutputFormat.setOutputPath(job, new Path("/wordcount/output"));

        job.setNumReduceTasks(2);
        boolean res = job.waitForCompletion(true);
        System.exit(res?0:1);
    }
}



mapper
package WordCount;

import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

/**
 *   , mapreduce ,map          reduce,            ,java          ,
 *   hadoop jdk        Long String Integer Float              hadoop        :
 * LongWritable,Text,IntWritable,FloatWritable
 *
 */
public class CountMapper extends Mapper{
    //KEYIN : map task            Long,hadoop    LongWritable     
    //VALUEIN: map task         ,   String,hadoop  Text   
    //KEYOUT:   key, Text,   
    //VALUEOUT:   value, Integer,      
    @Override
    protected void map(LongWritable key, Text value, Context context)//Context    key-value   ,           
            throws IOException, InterruptedException {
        String line = value.toString(); //   Text     String
        String[] words = line.split(" ");//    
        for(String word:words){//              
            context.write(new Text(word), new IntWritable(1));
        }
    }
}


reducer
package WordCount;


import java.io.IOException;
import java.util.Iterator;

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

public class CountReducer extends Reducer{
    @Override
    protected void reduce(Text key, Iterable values,Context context) throws IOException, InterruptedException {
        int count = 0;
        Iterator iterator = values.iterator();
        while(iterator.hasNext()){
            IntWritable value = iterator.next();
            count += value.get();
        }
        context.write(key, new IntWritable(count));
    }
}


この3つのファイルをjarパッケージに作成し、クラスタに配置してhadoop jarコマンドでhadoop jarコマンドを実行すると、ローカル依存ファイルとプロファイルが自動的に呼び出されます.
関連資料:Linuxがhadoopタスクを提出