All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.l3s.concatgz.io.warc.WarcGzInputFormat Maven / Gradle / Ivy

The newest version!
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016 Helge Holzmann (L3S)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package de.l3s.concatgz.io.warc;

import de.l3s.concatgz.data.WarcRecord;
import de.l3s.concatgz.io.ConcatGzipInputFormat;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.JobContext;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import java.io.IOException;


/**
 * This method can't be used properly with Spark Dataset[T] type,
 * since it requires the key/value types to be serializable,
 * which is not possible due to the stream to the actual data file
 */
public class WarcGzInputFormat extends FileInputFormat {
    private static class WarcRecordReader extends RecordReader {
        private ConcatGzipInputFormat.ConcatGzipRecordReader gzip = new ConcatGzipInputFormat.ConcatGzipRecordReader();

        private WarcWritable value = new WarcWritable();

        @Override
        public void initialize(InputSplit genericSplit, TaskAttemptContext context) throws IOException, InterruptedException {
            gzip.initialize(genericSplit, context);
        }

        public void initialize(String path) throws IOException, InterruptedException {
            gzip.initialize(path);
        }

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

        @Override
        public boolean nextKeyValue() throws IOException, InterruptedException {
            boolean hasNext = gzip.nextKeyValue();
            while (hasNext) {
                value.set(gzip.getCurrentValue());
                value.setLocation(gzip.getFilename(), gzip.getPos());
                if (value.isValid()) break;
                hasNext = gzip.nextKeyValue();
            }
            return hasNext;
        }

        @Override
        public NullWritable getCurrentKey() throws IOException, InterruptedException {
            return NullWritable.get();
        }

        @Override
        public WarcWritable getCurrentValue() throws IOException, InterruptedException {
            return value;
        }

        @Override
        public void close() throws IOException {
            gzip.close();
        }
    }

    @Override
    public RecordReader createRecordReader(InputSplit inputSplit, TaskAttemptContext taskAttemptContext) throws IOException, InterruptedException {
        return new WarcRecordReader();
    }

    @Override
    protected boolean isSplitable(JobContext context, Path filename) {
        return true;
    }

    public static void main(String[] args) throws Exception {
        WarcGzInputFormat.WarcRecordReader reader = new WarcGzInputFormat.WarcRecordReader();
        reader.initialize(args[1]);

        int count = 0;
        while (reader.nextKeyValue()) {
            count++;

            WarcRecord record = reader.getCurrentValue().getRecord();
            System.out.println("Count:" + count + ", header: " + record.getHeader());
            if (record.isHttp()) System.out.println("Count:" + count + ", http: " + record.getHttpResponse().getMessage());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy