2016-05-22 10 views
-2

ich mapreduce Code in Java müssen in hadoop Doppel Wortzählimpuls Lösung, um herauszufinden,Wie Doppelwortanzahl in Hadoop, um herauszufinden,

Eingang:

„Was ist Ihr Name, was Sie wollen? von mir? Sie wissen, der beste Weg, um Geld zu verdienen, ist Hardwork Was ist Ihr Ziel? "

Doppel-W.C. Ausgang: was 2 ist Ihr 2 Ihr Name 1 was Sie 1

eine schnellere Reaktion ist sehr spürbar.

danke im voraus.

Antwort

2

Der folgende Code funktioniert für mich.

package hadoop; 
import java.io.IOException; 
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.Mapper; 
import org.apache.hadoop.mapreduce.Reducer; 
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 

public class doubleWc { 
public static class doubMapper extends Mapper<LongWritable,Text,Text,IntWritable> 
{ 
    Text outkey=new Text(); 
    IntWritable outvalue=new IntWritable(); 
    public void map(LongWritable key,Text values,Context context) throws IOException, InterruptedException 
    { 
     String []cols=values.toString().split(","); 
     for(int i=0;i<(cols.length) - 1 ;i++) 
     { 
      outkey.set(cols[i]+","+cols[i+1]); 
      outvalue.set(1); 
      context.write(outkey, outvalue); 
     } 
    } 
} 
public static class douReducer extends Reducer<Text,IntWritable,Text,IntWritable> 
{ 
    IntWritable outvalue=new IntWritable(); 
    public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException, InterruptedException 
    { 
     int sum=0; 
     for(IntWritable t:values) 
     { 
      sum=sum+t.get(); 
     } 
     outvalue.set(sum); 
     context.write(key, outvalue); 
    } 
} 
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { 
    Configuration conf=new Configuration(); 
    @SuppressWarnings("deprecation") 
    Job job=new Job(conf,"double program"); 

    job.setJarByClass(doubleWc.class); 
    job.setMapperClass(doubMapper.class); 
    job.setReducerClass(douReducer.class); 

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

    FileInputFormat.addInputPath(job, new Path(args[0])); 
    FileOutputFormat.setOutputPath(job, new Path(args[1])); 

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

} 

} 

BITTE LASSEN SIE MICH WISSEN, WENN ES HILFE !!!!

+0

Ihre Lösung ist nett. Fügen Sie einfach eine Zusammenfassung hinzu, die die Map- und Reduce-Funktionen erklärt :) –

+0

Dieser Code lief ohne Fehler, gab aber eine leere Ausgabedatei ..! – adarikrishna

+0

erstmal danke für deine schnelle antwort ..! – adarikrishna

Verwandte Themen