Hadoop sequencefile using Hadoop 2 Apis

Source: Internet
Author: User

 Public classsequencefilestest {@Test Public voidTestseqfilereadwrite ()throwsIOException {Configuration conf=NewConfiguration (); FileSystem FS=filesystem.getlocal (conf); Path Seqfilepath=NewPath ("File.seq"); Sequencefile.writer Writer=sequencefile.createwriter (conf, Writer.file (Seqfilepath), Writer.keyclass (Text.class), Writer.valueclass (intwritable.class)); Writer.append (NewText ("Key1"),NewIntwritable (1)); Writer.append (NewText ("Key2"),NewIntwritable (2));    Writer.close (); Sequencefile.reader Reader=Newsequencefile.reader (conf, Reader.file (Seqfilepath)); Text Key=NewText (); Intwritable Val=Newintwritable ();  while(Reader.next (Key, Val)) {System.err.println ( key+ "\ T" +val);  } reader.close (); }}

OR

ImportJava.io.File;Importjava.io.IOException;ImportJava.io.RandomAccessFile;Importorg.apache.hadoop.conf.Configuration;ImportOrg.apache.hadoop.fs.Path;Importorg.apache.hadoop.io.BytesWritable;Importorg.apache.hadoop.io.IOUtils;ImportOrg.apache.hadoop.io.SequenceFile;Importorg.apache.hadoop.io.SequenceFile.Reader.Option;ImportOrg.apache.hadoop.io.Text;Importorg.apache.hadoop.io.Writable;Importorg.apache.hadoop.util.ReflectionUtils; Public classSequencefileoperator {PrivateConfiguration conf =NewConfiguration (); /*private FileSystem FS;        {try {fs = Filesystem.get (Uri.create ("hdfs://cldx-1336-1202:9000"), conf);        } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }    }*/     Public Static voidMain (string[] args)throwsIOException {//TODO auto-generated Method Stub        if(args = =NULL|| Args.length < 2) {System.out. println ("Following is the possible invocations <operation id> <arg1> <arg2> ..."); System.out. println ("1 <absolute path of directory containing documents> ); System.out.println ("2 ); return; }        intOperation = Integer.valueof (args[0]); Sequencefileoperator Doctoseqfilewriter=NewSequencefileoperator (); Switch(operation) { Case1: {String Docdirectorypath= Args[1]; String Sequencefilepath= Args[2]; System.out.println ("Writing files present at" +Docdirectorypath+ "to the sequence file" +Sequencefilepath);            Doctoseqfilewriter.loaddocumentstosequencefile (Docdirectorypath, Sequencefilepath);  Break; }         Case2: {String Sequencefilepath= Args[1]; System.out.println ("Reading the sequence file" +Sequencefilepath);            Doctoseqfilewriter.readsequencefile (Sequencefilepath);  Break; }        }    }    Private voidReadsequencefile (String Sequencefilepath)throwsIOException {//TODO auto-generated Method Stub        /** Sequencefile.reader sequencefilereader = new Sequencefile.reader (FS, * new Path (Sequencefilepath),         conf); */Option FilePath= SequenceFile.Reader.file (NewPath (Sequencefilepath)); Sequencefile.reader Sequencefilereader=Newsequencefile.reader (conf, filePath); Writable Key=(writable) reflectionutils.newinstance (Sequencefilereader.getkeyclass (), Conf); Writable Value=(writable) reflectionutils.newinstance (Sequencefilereader.getvalueclass (), Conf); Try {             while(Sequencefilereader.next (key, value)) {System.out. printf ("[%s]%s%s \ n", Sequencefilereader.getposition (), Key, Value.getclass (            )); }        } finally{ioutils.closestream (sequencefilereader); }    }    Private voidloaddocumentstosequencefile (String docdirectorypath, string sequencefilepath)throwsIOException {//TODO auto-generated Method StubFile docdirectory=NewFile (Docdirectorypath); if(!docdirectory.isdirectory ()) {System.out. println ("Provide an absolute path of a directory that contains the documents to be added to the sequence file"); return; }        /** Sequencefile.writer sequencefilewriter = * Sequencefile.createwriter (FS, Conf, new Path (Sequencefil         Epath), * text.class, Byteswritable.class); */org.apache.hadoop.io.SequenceFile.Writer.Option FilePath=sequencefile.writer. File (NewPath (Sequencefilepath)); Org.apache.hadoop.io.SequenceFile.Writer.Option Keyclass=sequencefile.writer. Keyclass (Text.class); Org.apache.hadoop.io.SequenceFile.Writer.Option Valueclass=sequencefile.writer. Valueclass (byteswritable.class); Sequencefile.writer Sequencefilewriter=sequencefile.createwriter (conf, FilePath, Keyclass, Valueclass); File[] Documents=Docdirectory.listfiles (); Try {             for(File document:documents) {Randomaccessfile RAF=NewRandomaccessfile (document, "R"); byte[] content =New byte[(int) Raf.length ()];                raf.readfully (content); Sequencefilewriter.append (NewText (Document.getname ()),Newbyteswritable (content));            Raf.close (); }        } finally{ioutils.closestream (sequencefilewriter); }    }}

Hadoop sequencefile using Hadoop 2 Apis

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.