[ 
https://issues.apache.org/jira/browse/MAPREDUCE-5433?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13731584#comment-13731584
 ] 

rulinma commented on MAPREDUCE-5433:
------------------------------------

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

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.io.hfile.CacheConfig;
import org.apache.hadoop.hbase.io.hfile.HFile;
import org.apache.hadoop.hbase.io.hfile.HFileScanner;
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 org.apache.hadoop.mapreduce.lib.input.FileSplit;

public class NHFileInputFormat extends
                FileInputFormat<ImmutableBytesWritable, Result> {

        private class HFileRecordReader extends
                        RecordReader<ImmutableBytesWritable, Result> {
                private HFile.Reader reader;
                private final HFileScanner scanner;
                private int entryNumber = 0;
                private ImmutableBytesWritable key = null;
                private Result result = null;
                List<KeyValue> tmpList = new ArrayList<KeyValue>();

                public HFileRecordReader(FileSplit split, Configuration conf)
                                throws IOException {
                        final Path path = split.getPath();
                        reader = HFile.createReader(FileSystem.get(conf), path,
                                        new CacheConfig(conf));
                        scanner = reader.getScanner(false, false, false);
                        scanner.seekTo();
                }

                @Override
                public void close() throws IOException {
                        if (reader != null) {
                                reader.close();
                        }
                }

                @Override
                public ImmutableBytesWritable getCurrentKey() throws 
IOException,
                                InterruptedException {
                        return key;
                }

                @Override
                public Result getCurrentValue() throws IOException,
                                InterruptedException {
                        result = new Result(tmpList);
                        return result;
                }

                @Override
                public boolean nextKeyValue() throws IOException, 
InterruptedException {
                        // clear
                        tmpList.clear();
                        // first
                        if (entryNumber == 0) {
                                entryNumber++;
                        }
                        if (entryNumber > reader.getEntries()) {
                                return false;
                        }
                        key = new 
ImmutableBytesWritable(scanner.getKeyValue().getRow());

                        tmpList.add(scanner.getKeyValue());
                        while (scanner.next()) {
                                entryNumber++;
                                // step next replace compare
                                if (compareBytes(key.get(), 
scanner.getKeyValue().getRow())) {
                                        // same row
                                        tmpList.add(scanner.getKeyValue());
                                } else {
                                        return true;
                                }
                        }
                        // last track
                        if (reader.getEntries() > 0 && (entryNumber == 
reader.getEntries())) {
                                entryNumber++;
                                return true;
                        }

                        return false;
                }

                @Override
                public float getProgress() throws IOException, 
InterruptedException {
                        if (reader != null) {
                                return (entryNumber / reader.getEntries());
                        }
                        return 1;
                }

                @Override
                public void initialize(InputSplit arg0, TaskAttemptContext arg1)
                                throws IOException, InterruptedException {
                        // System.out.println("init");
                }

        }

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

        @Override
        public RecordReader<ImmutableBytesWritable, Result> createRecordReader(
                        InputSplit split, TaskAttemptContext context) throws 
IOException,
                        InterruptedException {
                return new HFileRecordReader((FileSplit) split,
                                context.getConfiguration());
        }

        private static boolean compareBytes(byte[] a, byte[] b) {
                int i = compareBytes(a, 0, a.length, b, 0, b.length);
                if (i == 0)
                        return true;
                return false;
        }

        private static int compareBytes(byte[] b1, int s1, int l1, byte[] b2,
                        int s2, int l2) {
                int end1 = s1 + l1;
                int end2 = s2 + l2;
                int i = s1;
                for (int j = s2; (i < end1) && (j < end2); ++j) {
                        int a = b1[i] & 0xFF;
                        int b = b2[j] & 0xFF;
                        if (a != b)
                                return (a - b);
                        ++i;
                }

                return (l1 - l2);
        }

}
                
> use mapreduce to parse hfiles and output keyvalue
> -------------------------------------------------
>
>                 Key: MAPREDUCE-5433
>                 URL: https://issues.apache.org/jira/browse/MAPREDUCE-5433
>             Project: Hadoop Map/Reduce
>          Issue Type: Improvement
>          Components: examples
>            Reporter: rulinma
>            Assignee: rulinma
>            Priority: Minor
>


--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to