java在hadoop教程中实现自定义输出格式

ua4mk5z4  于 2021-05-29  发布在  Hadoop
关注(0)|答案(2)|浏览(429)

我想运行本教程中描述的代码,以便在hadoop中自定义输出格式。更准确地说,本教程显示了两个java文件:
wordcount:是wordcount java应用程序(类似于此链接中mapreduce教程的wordcountv1.0)
xmloutputformat:java类,它扩展fileoutputformat并实现自定义输出的方法。
我所做的是使用mapreduce教程的wordcountv1.0(而不是使用教程中显示的wordcount)并添加驱动程序 job.setOutputFormatClass(XMLOutputFormat.class); 并按以下方式执行hadoop应用程序: /usr/local/hadoop/bin/hadoop com.sun.tools.javac.Main WordCount.java && jar cf wc.jar WordCount*.class && /usr/local/hadoop/bin/hadoop jar wc.jar WordCount /home/luis/Desktop/mytest/input/ ./output_folder 注: /home/luis/Desktop/mytest/input/ 以及 ./output_folder 分别是输入和输出文件夹。
不幸的是,终端显示以下错误: WordCount.java:57: error: cannot find symbol job.setOutputFormatClass(XMLOutputFormat.class); ^ symbol: class XMLOutputFormat location: class WordCount 1 error 为什么?wordcount.java和xmloutputformat.java存储在同一文件夹中。
下面是我的代码。 WordCount 代码:

import java.io.IOException;
import java.util.StringTokenizer;

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.output.FileOutputFormat;

public class WordCount {

  public static class TokenizerMapper
       extends Mapper<Object, Text, Text, IntWritable>{

    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(Object key, Text value, Context context
                    ) throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      while (itr.hasMoreTokens()) {
        word.set(itr.nextToken());
        context.write(word, one);
      }
    }
  }

  public static class IntSumReducer
       extends Reducer<Text,IntWritable,Text,IntWritable> {
    private IntWritable result = new IntWritable();

    public void reduce(Text key, Iterable<IntWritable> values,
                       Context context
                       ) throws IOException, InterruptedException {
      int sum = 0;
      for (IntWritable val : values) {
        sum += val.get();
      }
      result.set(sum);
      context.write(key, result);
    }
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "word count");
    job.setJarByClass(WordCount.class);
    job.setMapperClass(TokenizerMapper.class);
    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    job.setOutputFormatClass(XMLOutputFormat.class);
    FileInputFormat.addInputPath(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    System.exit(job.waitForCompletion(true) ? 0 : 1);

  }
}
``` `XMLOutputFormat` 代码:

import java.io.DataOutputStream;
import java.io.IOException;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class XMLOutputFormat extends FileOutputFormat<Text, IntWritable> {

protected static class XMLRecordWriter extends RecordWriter<Text, IntWritable> {

    private DataOutputStream out;

    public XMLRecordWriter(DataOutputStream out) throws IOException{

        this.out = out;
        out.writeBytes("<Output>\n");

    }

    private void writeStyle(String xml_tag,String tag_value) throws IOException {

        out.writeBytes("<"+xml_tag+">"+tag_value+"</"+xml_tag+">\n");

    }

    public synchronized void write(Text key, IntWritable value) throws IOException {

        out.writeBytes("<record>\n");
        this.writeStyle("key", key.toString());
        this.writeStyle("value", value.toString());
        out.writeBytes("</record>\n");

    }

    public synchronized void close(TaskAttemptContext job) throws IOException {

        try {

            out.writeBytes("</Output>\n");

        } finally {

            out.close();

        }

    }

}

public RecordWriter<Text, IntWritable> getRecordWriter(TaskAttemptContext job) throws IOException {

    String file_extension = ".xml";
    Path file = getDefaultWorkFile(job, file_extension);
    FileSystem fs = file.getFileSystem(job.getConfiguration());
    FSDataOutputStream fileOut = fs.create(file, false);
    return new XMLRecordWriter(fileOut);

}

}

to94eoyn

to94eoyn1#

你要么 package testpackage; 在你人生的开始 WordCount
import testpackage.XMLOutputFormat; 在你的 WordCount 班级。
因为它们在同一个目录中,这并不意味着它们在同一个包中。

kulphzqa

kulphzqa2#

我们需要先将xmloputformat.jar文件添加到hadoop\u类路径中,驱动程序代码才能找到它。并在-libjars选项中传递它,以添加到Map的类路径中并减少JVM。

export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:/abc/xyz/XMLOutputFormat.jar

yarn jar wordcount.jar com.sample.test.Wordcount 
-libjars /path/to/XMLOutputFormat.jar 
/lab/mr/input /lab/output/output

相关问题