2010-01-22 6 views
22

Je ne trouve pas un seul exemple de soumission d'un travail Hadoop qui n'utilise pas la classe obsolète JobConf. JobClient, qui n'a pas été déprécié, prend toujours en charge uniquement les méthodes qui prennent un paramètre JobConf.Exécution d'un travail Hadoop sans utiliser JobConf

Quelqu'un peut-il s'il vous plaît me montrer un exemple de code Java soumettant une carte Hadoop/réduire l'emploi en utilisant uniquement la classe Configuration (non JobConf), et en utilisant le package mapreduce.lib.input au lieu de mapred.input?

Répondre

23

Hope this utile

import java.io.File; 

import org.apache.commons.io.FileUtils; 
import org.apache.hadoop.conf.Configured; 
import org.apache.hadoop.fs.Path; 
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.lib.input.FileInputFormat; 
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 
import org.apache.hadoop.util.Tool; 
import org.apache.hadoop.util.ToolRunner; 

public class MapReduceExample extends Configured implements Tool { 

    static class MyMapper extends Mapper<LongWritable, Text, LongWritable, Text> { 
     public MyMapper(){ 

     } 

     protected void map(
       LongWritable key, 
       Text value, 
       org.apache.hadoop.mapreduce.Mapper<LongWritable, Text, LongWritable, Text>.Context context) 
       throws java.io.IOException, InterruptedException { 
      context.getCounter("mygroup", "jeff").increment(1); 
      context.write(key, value); 
     }; 
    } 

    @Override 
    public int run(String[] args) throws Exception { 
     Job job = new Job(); 
     job.setMapperClass(MyMapper.class); 
     FileInputFormat.setInputPaths(job, new Path(args[0])); 
     FileOutputFormat.setOutputPath(job, new Path(args[1])); 

     job.waitForCompletion(true); 
     return 0; 
    } 

    public static void main(String[] args) throws Exception { 
     FileUtils.deleteDirectory(new File("data/output")); 
     args = new String[] { "data/input", "data/output" }; 
     ToolRunner.run(new MapReduceExample(), args); 
    } 
} 
+6

Les trois constructeurs 'job' sont maintenant déconseillés. La méthode correcte est la suivante: 'Job job = Job.getInstance (getConf());' –

+1

Sur quelle version? J'utilise la version 1.0.4 mais je n'ai pas trouvé ce constructeur. –

9

Je crois this tutorial illustre la suppression de la classe JobConf dépréciée en utilisant Hadoop 0.20.1.

1

Dans l'API précédente, il y avait trois façons de soumettre le travail et l'une d'entre elles consiste à soumettre le travail et à obtenir une référence à RunningJob et à obtenir un ID du RunningJob.

submitJob(JobConf) : only submits the job, then poll the returned handle to the RunningJob to query status and make scheduling decisions. 

Comment peut-on utiliser la nouvelle API et obtenir une référence au RunningJob et obtenir une carte d'identité du runningJob comme aucun des APIs renvoyer une référence à RunningJob

http://hadoop.apache.org/docs/current/api/org/apache/hadoop/mapreduce/Job.html 

grâce

1

Essayez d'utiliser Configuration et Job. Voici un exemple:

(Remplacez vos Mapper, Combiner, Reducer classes et autre configuration)

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.Mapper; 
import org.apache.hadoop.mapreduce.Reducer; 
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; 

public class WordCount { 
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { 
    Configuration conf = new Configuration(); 
    if(args.length != 2) { 
     System.err.println("Usage: <in> <out>"); 
     System.exit(2); 
    } 
    Job job = Job.getInstance(conf, "Word Count"); 

    // set jar 
    job.setJarByClass(WordCount.class); 

    // set Mapper, Combiner, Reducer 
    job.setMapperClass(TokenizerMapper.class); 
    job.setCombinerClass(IntSumReducer.class); 
    job.setReducerClass(IntSumReducer.class); 

    /* Optional, set customer defined Partioner: 
    * job.setPartitionerClass(MyPartioner.class); 
    */ 

    // set output key 
    job.setMapOutputKeyClass(Text.class); 
    job.setMapOutputValueClass(IntWritable.class); 
    job.setOutputKeyClass(Text.class); 
    job.setOutputValueClass(IntWritable.class); 

    // set input and output path 
    FileInputFormat.addInputPath(job, new Path(args[0])); 
    FileOutputFormat.setOutputPath(job, new Path(args[1])); 

    // by default, Hadoop use TextInputFormat and TextOutputFormat 
    // any customer defined input and output class must implement InputFormat/OutputFormat interface 
    job.setInputFormatClass(TextInputFormat.class); 
    job.setOutputFormatClass(TextOutputFormat.class); 

    System.exit(job.waitForCompletion(true) ? 0 : 1); 
    } 
} 
Questions connexes