How to use Combinefileinputformat to import Netflix data set into HBase

Source: Internet
Author: User
Tags split tostring
Package com.mr.test;  
      
Import java.io.IOException;  
Import org.apache.hadoop.io.BytesWritable;  
Import org.apache.hadoop.io.LongWritable;  
Import Org.apache.hadoop.mapreduce.InputSplit;  
Import Org.apache.hadoop.mapreduce.RecordReader;  
Import Org.apache.hadoop.mapreduce.TaskAttemptContext;  
Import Org.apache.hadoop.mapreduce.lib.input.CombineFileInputFormat;  
Import Org.apache.hadoop.mapreduce.lib.input.CombineFileRecordReader;  
      
Import Org.apache.hadoop.mapreduce.lib.input.CombineFileSplit;  
      
    public class Combinesmallfileinputformat extends Combinefileinputformat<longwritable, byteswritable> { @Override public recordreader<longwritable, byteswritable> Createrecordreader (Inputsplit split, Taskattemptcontext context) throws IOException {Combinefilesplit combinefilesplit = (combinefilesplit) s  
        Plit; Combinefilerecordreader<longwritable, byteswritable> recordreader = new combinefilerecordreader<Longwritable, byteswritable> (combinefilesplit, context, combinesmallfilerecordreader.class);  
        try {recordreader.initialize (combinefilesplit, context); The catch (Interruptedexception e) {new RuntimeException ("Error to initialize Combinesmallfilerecordreader.")  
        );  
    return recordreader; }  
      
}
Package com.mr.test;  
      
Import java.io.IOException;  
Import Org.apache.hadoop.fs.Path;  
Import org.apache.hadoop.io.BytesWritable;  
Import org.apache.hadoop.io.LongWritable;  
Import Org.apache.hadoop.mapreduce.InputSplit;  
Import Org.apache.hadoop.mapreduce.RecordReader;  
Import Org.apache.hadoop.mapreduce.TaskAttemptContext;  
Import Org.apache.hadoop.mapreduce.lib.input.CombineFileSplit;  
Import Org.apache.hadoop.mapreduce.lib.input.FileSplit;  
      
Import Org.apache.hadoop.mapreduce.lib.input.LineRecordReader; public class Combinesmallfilerecordreader extends Recordreader<longwritable, byteswritable> {private C  
    Ombinefilesplit Combinefilesplit;  
    Private Linerecordreader Linerecordreader = new Linerecordreader ();  
    Private path[] paths;  
    private int totallength;  
    private int currentindex;  
    private float currentprogress = 0;  
    Private Longwritable Currentkey; Private byteswritable CurrentValue = new BYTESWRItable (); Public Combinesmallfilerecordreader (Combinefilesplit combinefilesplit, Taskattemptcontext context, Integer index)  
        Throws IOException {super ();  
        This.combinefilesplit = Combinefilesplit; This.currentindex = index; The small file block currently being processed is indexed in Combinefilesplit} @Override public void Initialize (Inputsplit split, Taskatt Emptcontext context) throws IOException, interruptedexception {this.combinefilesplit = (combinefilesplit) split  
        ; Handles a small file block in Combinefilesplit, because using Linerecordreader, you need to construct a Filesplit object before you can read the data filesplit filesplit = new Fil Esplit (Combinefilesplit.getpath (Currentindex), Combinefilesplit.getoffset (Currentindex),  
        Combinefilesplit.getlength (Currentindex), combinefilesplit.getlocations ());  
      
        Linerecordreader.initialize (filesplit, context);  
        This.paths = Combinefilesplit.getpaths ();  
        Totallength = Paths.length; Context.getconfiguration (). Set ("Map.input.file.name", Combinefilesplit.getpath (Currentindex). GetName ()); @Override public longwritable Getcurrentkey () throws IOException, interruptedexception {Curr  
        Entkey = Linerecordreader.getcurrentkey ();  
    return currentkey; } <strong><span style= "color: #ff0000;" > @Override public byteswritable getcurrentvalue () throws IOException, interruptedexception {System.ou  
        T.println ("Linerecordreader:" +linerecordreader.getcurrentvalue (). toString ());  
        byte[] content = Linerecordreader.getcurrentvalue (). toString (). GetBytes ();  
        System.out.println ("Content:" +new String (content));  
        CurrentValue = new Byteswritable ();  
        Currentvalue.set (content, 0, content.length);  
        System.out.println ("CurrentValue:" +new String (Currentvalue.getbytes ()));  
    return currentvalue; }</span></strong> public static void Main (String args[]) {byteswritable CV = new byteswritable ();  
        String str1 = "1234567";  
        String str2 = "123450";  
        Cv.set (Str1.getbytes (), 0, Str1.getbytes (). length);  
              
        System.out.println (New String (Cv.getbytes ()));  
              
        Cv.setcapacity (0);  
        Cv.set (Str2.getbytes (), 0, Str2.getbytes (). length);  
    System.out.println (New String (Cv.getbytes ())); @Override public boolean Nextkeyvalue () throws IOException, interruptedexception {if (currentindex  
        >= 0 && Currentindex < totallength) {return linerecordreader.nextkeyvalue ();  
        else {return false; @Override public float getprogress () throws IOException {if Currentindex >= 0 &A  
            mp;& Currentindex < totallength) {currentprogress = (float) currentindex/totallength;  
        return currentprogress; Return to currentProgress;  
    @Override public void Close () throws IOException {linerecordreader.close (); }  
}

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/database/extra/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.