2011-05-28 1 views
8

मेरा प्रश्न mapreduce programming in java है।मैपर्रिड गिनती उदाहरण

मान लीजिए मेरे पास WordCount.java उदाहरण है, एक मानक mapreduce program। मैं नक्शा फ़ंक्शन को कुछ जानकारी एकत्र करने के लिए चाहता हूं, और <slaveNode_id,some_info_collected>,

पर I can know what slave node collected what data .. कोई विचार कैसे ??

public class WordCount { 

    public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { 
     private final static IntWritable one = new IntWritable(1); 
     private Text word = new Text(); 

     public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { 
     String line = value.toString(); 
     StringTokenizer tokenizer = new StringTokenizer(line); 
     while (tokenizer.hasMoreTokens()) { 
      word.set(tokenizer.nextToken()); 
      output.collect(word, one); 
     } 
     } 
    } 

    public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> { 
     public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { 
     int sum = 0; 
     while (values.hasNext()) { 
      sum += values.next().get(); 
     } 
     output.collect(key, new IntWritable(sum)); 
     } 
    } 

    public static void main(String[] args) throws Exception { 
     JobConf conf = new JobConf(WordCount.class); 
     conf.setJobName("wordcount"); 

     conf.setOutputKeyClass(Text.class); 
     conf.setOutputValueClass(IntWritable.class); 

     conf.setMapperClass(Map.class); 
     conf.setCombinerClass(Reduce.class); 
     conf.setReducerClass(Reduce.class); 

     conf.setInputFormat(TextInputFormat.class); 
     conf.setOutputFormat(TextOutputFormat.class); 

     FileInputFormat.setInputPaths(conf, new Path(args[0])); 
     FileOutputFormat.setOutputPath(conf, new Path(args[1])); 

     JobClient.runJob(conf); 
    } 
} 

धन्यवाद !!

उत्तर

5

आप जो पूछ रहे हैं वह एप्लिकेशन (आपके मानचित्र को कम करने वाली चीज़) को उस बुनियादी ढांचे के बारे में जानना है जो यह चल रहा था।

आम तौर पर उत्तर यह है कि आपके आवेदन को इस जानकारी की आवश्यकता नहीं है। मैपर को प्रत्येक कॉल और रेड्यूसर को प्रत्येक कॉल को एक अलग नोड या सभी एक ही नोड पर निष्पादित किया जा सकता है। MapReduce की सुंदरता यह है कि परिणाम एक जैसा है, इसलिए आपके आवेदन के लिए: इससे कोई फर्क नहीं पड़ता।

परिणामस्वरूप एपीआई में आपके इस अनुरोध का समर्थन करने के लिए विशेषताएं नहीं हैं।

मज़ा सीखने Hadoop :)


पी.एस. एकमात्र तरीका जिसे मैं सोच सकता हूं (जो कम से कम कहने के लिए बुरा है) यह है कि आप मैपर में किसी प्रकार का सिस्टम कॉल शामिल करते हैं और इसके नाम/गुण/आदि के बारे में अंतर्निहित ओएस से पूछते हैं। इस प्रकार का निर्माण आपके एप्लिकेशन को बहुत पोर्टेबल बना देगा; यानी यह विंडोज या अमेज़ॅन में हैडोप पर नहीं चलेगा।

+0

नहीं वास्तव में, वह अपने डेटा में गुलामों बारे में जानकारी है: । वर्डकाउंट इसे पर वापस ले जाता है, वह एक दास नोड एकत्र की गई सभी जानकारी प्राप्त करने के लिए दूसरे तरीके से चाहता है। –

+0

हां, बिल्कुल, मैं प्रत्येक स्लैवेनोड की जानकारी एकत्र करना चाहता हूं .. –

+0

एमआर आवेदन के भीतर से स्लैवेनोड की आईडी जानने का कोई तरीका नहीं है। –

1

वर्डकाउंट आपके लिए गलत उदाहरण है। आप बस एक साथ सभी जानकारी मर्ज करना चाहते हैं। यह चीजों को शब्द गणना में उलटा करता है।

असल में आप अपने गुलामNode_id को IntWritable (यदि यह संभव है) और Text के रूप में जानकारी दे रहे हैं।

public static class Map extends MapReduceBase implements Mapper<LongWritable, Text,IntWritable, Text> { 
    private Text word = new Text(); 

    public void map(LongWritable key, Text value, OutputCollector<IntWritable, Text> output, Reporter reporter) throws IOException { 
    String line = value.toString(); 
    StringTokenizer tokenizer = new StringTokenizer(line); 
    while (tokenizer.hasMoreTokens()) { 
     word.set(tokenizer.nextToken()); 
     // you have to split your data here: ID and value 
     IntWritable id = new IntWritable(YOUR_ID_HERE); 

     output.collect(id, word); 
    } 
    } 
} 

और कम करने में उसी तरह जाना होगा:

public static class Reduce extends MapReduceBase implements Reducer<IntWritable, Text,IntWritable, Text> { 
    public void reduce(IntWritable key, Iterator<Text> values, OutputCollector<IntWritable,Text> output, Reporter reporter) throws IOException { 

     // now you have all the values for a slaveID as key. Do whatever you like with that... 
     for(Text value : values) 
     output.collect(key, value) 
    } 
} 
+0

दिलचस्प .. लेकिन मेरा सवाल बनी हुई है .. मैं प्रोग्राम से slave_node_id कैसे प्राप्त कर सकता हूं ?? –

+0

आपका डेटा कैसा दिखता है? –

+0

नक्शा रेड्यूसर मानचित्र जहां जानकारी इंटरनेट के साथ मिली कुछ जानकारी के साथ टाइप क्लास क्लास देगा .. –