My file mapper class is not executing when parsing xml in hadoop using XMLInputFormat

I am new to hadoop using Hadoop version 2.6.0 and trying to parse complex XML. After searching for a while, I find out that in order to parse the XML, we need to write our own InputFormat, which is the mahout XMLInputFormat. I also used in this example

But when I run my code after passig the XMLInputformat class, it won't call my own Mapper class and the output file has 0 data in it if I use the XMLInputFormat given in the example.

Surprisingly, if I don't pass my XMLInputFormat class to my JOB, then my mapper works fine and outputs the result correctly. Will anyone please help here to point out what I am missing here.

My work config class:

public static void runParserJob(String inputPath, String outputPath) throws IOException {
    LOGGER.info("-----runParserJob()-----Start");
    Configuration configuration = new Configuration();         configuration.set("xmlinput.start",Constants.XML_INPUT_START_TAG_PRODUCT);
    configuration.set("xmlinput.end",Constants.XML_INPUT_END_TAG_PRODUCT);
    configuration.set("io.serializations",Constants.XML_IO_SERIALIZATIONS);

    Job job = new Job(configuration,Constants.JOB_TITLE);
    FileInputFormat.setInputPaths(job, inputPath);
    job.setJarByClass(ParserDriver.class);
    job.setMapperClass(XMLMapper.class);
    job.setNumReduceTasks(0);
    job.setInputFormatClass(XmlInputFormat.class);
    job.setOutputKeyClass(NullWritable.class);
    job.setOutputValueClass(Text.class);
    Path hdfsOutputPath = new Path(outputPath);
    FileOutputFormat.setOutputPath(job, hdfsOutputPath);
    FileSystem dfs = FileSystem.get(hdfsOutputPath.toUri(),configuration);
    /**Using this condition it will create output at same location 
     * by deleting older data in that location**/
    if(dfs.exists(hdfsOutputPath)){
        dfs.delete(hdfsOutputPath,true);
    }
    try{
        job.waitForCompletion(true);
    }catch(InterruptedException ie){
        LOGGER.error("-----Process interrupted in between Exception-----", ie);
    }catch(ClassNotFoundException ce){
        LOGGER.error("-----Class not found while running the job-----",ce);
    }
}

      

My XMLInputFormat class:

public class XmlInputFormat extends TextInputFormat{

public static final String START_TAG_KEY = "xmlinput.start";
public static final String END_TAG_KEY = "xmlinput.end";

@Override
public RecordReader<LongWritable,Text> createRecordReader(InputSplit is, TaskAttemptContext tac)  {
    return new XmlRecordReader();
}

public static class XmlRecordReader extends RecordReader<LongWritable, Text>{
    private byte[] startTag;
    private byte[] endTag;
    private long start;
    private long end;
    private FSDataInputStream fsin;
    private DataOutputBuffer buffer = new DataOutputBuffer();
    private LongWritable key = new LongWritable();
    private Text value = new Text();

    @Override
    public void initialize(InputSplit inputSplit, TaskAttemptContext taskAttemptContext)
            throws IOException, InterruptedException {
        FileSplit fileSplit = (FileSplit)inputSplit;
        startTag = taskAttemptContext.getConfiguration().get(START_TAG_KEY).getBytes("utf-8");
        endTag = taskAttemptContext.getConfiguration().get(END_TAG_KEY).getBytes("utf-8");

        start = fileSplit.getStart();
        end = start + fileSplit.getLength();
        Path file = fileSplit.getPath();

        FileSystem hdfs = file.getFileSystem(taskAttemptContext.getConfiguration());
         fsin = hdfs.open(fileSplit.getPath());
         fsin.seek(start);
    }

    @Override
    public boolean nextKeyValue() throws IOException, InterruptedException {
        if(fsin.getPos() < end){
            if(readUntilMatch(startTag,false)){
              try {
                    buffer.write(startTag);
                    if (readUntilMatch(endTag, true)) {
                        value.set(buffer.getData(), 0, buffer.getLength());
                        key.set(fsin.getPos());
                        return true;
                    }
                  } finally {
                    buffer.reset();
                  }
            }
        }
        return false;
    }

    @Override
    public void close() throws IOException {

    }

    @Override
    public LongWritable getCurrentKey() throws IOException,InterruptedException {
        return null;
    }

    @Override
    public Text getCurrentValue() throws IOException, InterruptedException {
        return null;
    }

    @Override
    public float getProgress() throws IOException, InterruptedException {
        return 0;
    }

    private boolean readUntilMatch(byte[] match, boolean withinBlock) throws IOException{
        int i = 0;
        while(true){
            int b = fsin.read();
            //If reaches to EOF
            if(b == -1){
                return false;
            }   
            //If not then save into the buffer.
            if(withinBlock){
                buffer.write(b);
            }
            // check if we're matching:
            if (b == match[i]) {
              i++;
              if (i >= match.length) return true;
            } else i = 0;
            // see if we've passed the stop point:
            if (!withinBlock && i == 0 && fsin.getPos() >= end) return false;
        }
    }

}

      

}


Can anyone help me here? Thank you in advance. Correct me if I am wrong somewhere.

+3


source to share


1 answer


I'm not sure what your XML structure looks like, but for example if you have an XML structure:

<data>
   <product id="101" itemCategory="BER" transaction="PUR">
       <transaction-id>102A5RET</transaction-id>
       <item-name>Blue-Moon-12-PK-BTTLE</item-name>
       <item-purchased>2</item-purchased>
       <item-price>12.99</item-price>
       <time-stamp>2015-04-20 11:12:13 102301</time-stamp>
   </product>
   .
   .
   .
</data>

      

Your XMLInputFormat class needs to know which XML node you want to work with:



configuration.set("xmlinput.start", "<product") //note only <product
configuration.set("xmlinput.end", "</product>") //note only </product>

      

Hope this helps!

+1


source







All Articles