Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Hadoop教程中运行实现自定义输出格式_Java_Hadoop_Mapreduce - Fatal编程技术网

Java 在Hadoop教程中运行实现自定义输出格式

Java 在Hadoop教程中运行实现自定义输出格式,java,hadoop,mapreduce,Java,Hadoop,Mapreduce,为了在Hadoop中定制输出格式,我想运行本文中描述的代码。更准确地说,本教程显示了两个java文件: WordCount:是word count java应用程序(类似于本教程中MapReduce教程的WordCount v1.0) XMLOutputFormat:java类,它扩展FileOutputFormat并实现自定义输出的方法 我所做的是采用MapReduce教程的WordCount v1.0(而不是使用教程中显示的WordCount),并添加驱动程序job.setOutputFor

为了在Hadoop中定制输出格式,我想运行本文中描述的代码。更准确地说,本教程显示了两个java文件:

  • WordCount:是word count java应用程序(类似于本教程中MapReduce教程的WordCount v1.0)
  • XMLOutputFormat:java类,它扩展FileOutputFormat并实现自定义输出的方法
  • 我所做的是采用MapReduce教程的WordCount v1.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\u文件夹

    注:
    /home/luis/Desktop/mytest/input/
    /output\u文件夹
    分别是输入和输出文件夹

    不幸的是,终端显示以下错误:

    WordCount.java:57:错误:找不到符号
    setOutputFormatClass(XMLOutputFormat.class);
    ^
    符号:类XMLOutputFormat
    位置:类字数
    1错误

    为什么??WordCount.java和XMLOutputFormat.java存储在同一文件夹中

    下面是我的代码

    WordCount
    code:

    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);
    
    
    
      }
    }
    
    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);
    
        }
    
    }
    

    您需要添加
    包testpackage
    字数
    课程开始时

    import testpackage.xmlooutputformatWordCount
    类中的code>


    因为它们在同一个目录中,这并不意味着它们在同一个包中。

    您需要添加
    包testpackage
    字数
    课程开始时

    import testpackage.xmlooutputformatWordCount
    类中的code>


    因为它们在同一个目录中,这并不意味着它们在同一个包中。

    我们需要先将xmloputformat.jar文件添加到HADOOP_类路径中,驱动程序代码才能找到它。并在-libjars选项中传递它以添加到映射的类路径中,并减少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
    

    我们需要首先将XMLOutputFormat.jar文件添加到HADOOP_类路径中,以便驱动程序代码找到它。并在-libjars选项中传递它以添加到映射的类路径中,并减少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
    

    我试过了,但没有成功。我通过运行
    /usr/local/hadoop/bin/hadoop com.sun.tools.javac.Main-d创建了测试包。java
    ,之后,我添加了
    包testpackage
    WordCount
    的开头,使用
    /usr/local/hadoop/bin/hadoop com.sun.tools.javac.Main WordCount.java
    编译它,但我得到了相同的错误。然后,我删除了
    包testpackage
    WordCount
    开始,我添加了
    importtestpackage.XMLOutputFormat
    WordCount
    中,但我得到了
    错误:找不到symbol import testpackage.XMLOutputFormat
    并且找不到symbol
    job.setOutputFormatClass
    我试过了,但没有成功。我通过运行
    /usr/local/hadoop/bin/hadoop com.sun.tools.javac.Main-d创建了测试包。java
    ,之后,我添加了
    包testpackage
    WordCount
    的开头,使用
    /usr/local/hadoop/bin/hadoop com.sun.tools.javac.Main WordCount.java
    编译它,但我得到了相同的错误。然后,我删除了
    包testpackage
    WordCount
    开始,我添加了
    importtestpackage.XMLOutputFormat
    WordCount
    中,但我得到了
    错误:找不到symbol import testpackage.XMLOutputFormat并且找不到符号
    作业。setOutputFormatClass