2017-05-27 90 views
0

我的Java版本來計數文件中的單詞:編譯錯誤使用Hadoop

➜ test git:(dev) ✗ java -version 
java version "1.8.0_131" 
Java(TM) SE Runtime Environment (build 1.8.0_131-b11) 
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode) 

我想下面這個簡單的java程序使用Hadoop地圖從here減少這給字數從文件來運行。以下是整個Java代碼:

import org.apache.hadoop.conf.Configuration; 
import org.apache.hadoop.conf.Configured; 
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.mapred.*; 
import org.apache.hadoop.util.Tool; 
import org.apache.hadoop.util.ToolRunner; 

import java.io.IOException; 
import java.util.ArrayList; 
import java.util.Iterator; 
import java.util.List; 
import java.util.StringTokenizer; 

public class WordCount extends Configured implements Tool { 
    private final static LongWritable ONE = new LongWritable(1L); 

// Mapper Class, Counts words in each line. For each line, break the line into words and emits them as (word, 1) 

public static class MapClass 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 itr = new StringTokenizer(line); 
    while (itr.hasMoreTokens()) { 
    word.set(itr.nextToken()); 
    output.collect(word, one); 
    } 
} 
} 

// Reducer class that just emits the sum of the input values. 

public static class Reduce extends MapReduceBase implements Reducer< Text, IntWritable, Text, IntWritable > { 
public void reduce(Text key, Iterator 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)); 
    } 
    } 

static int printUsage() { 
System.out.println("wordcount [-m #mappers ] [-r #reducers] input_file output_file"); 
    ToolRunner.printGenericCommandUsage(System.out); 
    return -1; 
    } 

public int run(String[] args) throws Exception { 

    JobConf conf = new JobConf(getConf(), WordCount.class); 
    conf.setJobName("wordcount"); 

// the keys are words (strings) 
    conf.setOutputKeyClass(Text.class); 
// the values are counts (ints) 
    conf.setOutputValueClass(IntWritable.class); 

    conf.setMapperClass(MapClass.class); 
// Here we set the combiner!!!! 
    conf.setCombinerClass(Reduce.class); 
    conf.setReducerClass(Reduce.class); 

    List other_args = new ArrayList(); 
    for(int i=0; i < args.length; ++i) { 
    try { 
     if ("-m".equals(args[i])) { 
conf.setNumMapTasks(Integer.parseInt(args[++i])); 
     } else if ("-r".equals(args[i])) { 
conf.setNumReduceTasks(Integer.parseInt(args[++i])); 
     } else { 
      other_args.add(args[i]); 
     } 
     } catch (NumberFormatException except) { 
     System.out.println("ERROR: Integer expected instead of " + args[i]); 
     return printUsage(); 
     } catch (ArrayIndexOutOfBoundsException except) { 
     System.out.println("ERROR: Required parameter missing from " + 
      args[i-1]); 
     return printUsage(); 
     } 
    } 
// Make sure there are exactly 2 parameters left. 
    if (other_args.size() != 2) { 
     System.out.println("ERROR: Wrong number of parameters: " + 
      other_args.size() + " instead of 2."); 
     return printUsage(); 
    } 
    FileInputFormat.setInputPaths(conf, other_args.get(0)); 
    FileOutputFormat.setOutputPath(conf, new Path(other_args.get(1))); 

    JobClient.runJob(conf); 
    return 0; 
    } 

public static void main(String[] args) throws Exception { 
    int res = ToolRunner.run(new Configuration(), new WordCount(), args); 
    System.exit(res); 
    } 
} 

但在上面的代碼的下方段:

// Reducer class that just emits the sum of the input values. 

public static class Reduce extends MapReduceBase implements Reducer< Text, IntWritable, Text, IntWritable > { 
public void reduce(Text key, Iterator 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)); 
    } 
    } 

我得到以下編譯錯誤:

➜ test git:(dev) ✗ javac WordCount.java WordCount.java:43: error: > expected OutputCollector output, ^

WordCount.java:43: error: ')' expected OutputCollector output, ^

WordCount.java:43: error: ';' expected OutputCollector output, ^

WordCount.java:43: error: expected OutputCollector output, ^

WordCount.java:44: error: ';' expected Reporter reporter) throws IOException { ^

WordCount.java:44: error: expected Reporter reporter) throws IOException { ^ WordCount.java:44: error: illegal start of type Reporter reporter) throws IOException { ^

WordCount.java:44: error: ';' expected Reporter reporter) throws IOException { ^

8 errors

我相信這是一些非常基本的錯誤,但無法弄清楚。

回答

1

我不確定這是什麼,但它不是Java。例如:

OutputCollector<text, intwritable=""> 

textintwrtiable不類型。那應該是TextIntWritable。而默認的分配對我來說是一個完整的謎。 Java不支持默認的泛型,即使它是這樣,我不確定在這種情況下空字符串的意思。

+0

你正在寫,同樣讓我感到困惑。 – Saurabh