Requirements are as follows:
Generate an approximately 100-byte text file on your local filesystem, write a program (which can take advantage of the Java API or C API), read the file, and write its 第101-120 byte content to HDFs as a new file.
ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.OutputStream; Public classShengchen { Public Static voidMain (string[] args)throwsIOException {//TODO auto-generated Method StubFile File=NewFile ("/home/weiguohui/shengchen.txt"); if(!file.exists ()) {File.createnewfile (); } byte[] bytes=New byte[130]; for(inti = 0; i < bytes.length; i++) {Bytes[i]=(byte) I; } FileOutputStream Fos=Newfileoutputstream (file); Fos.write (bytes); }}
Exactly 20 bytes written.
Code:
ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.InputStream;Importorg.apache.hadoop.io.IOUtils;ImportJava.io.BufferedInputStream;ImportOrg.apache.hadoop.fs.FileSystem;Importorg.apache.hadoop.conf.Configuration;ImportJava.net.URI;ImportJava.io.OutputStream;ImportOrg.apache.hadoop.fs.Path; Public classCopyfiletohdfs { Public Static voidMain (string[] args) {//TODO auto-generated Method StubFile File=NewFile ("/home/weiguohui/shengchen.txt"); InputStream in=NULL; String DST=args[0]; Configuration conf=NewConfiguration (); byte[] bytes=New byte[1024]; intoffset=100; intLen=20; intNumberread=0; OutputStream OS=NULL; Try{FileSystem fs=Filesystem.get (Uri.create (DST), conf); Inch=NewBufferedinputstream (Newfileinputstream (file)); OS=fs.create (NewPath (DST)); while((Numberread=in.read (bytes))!=-1) {os.write (bytes, offset, len); } //ioutils.copybytes (in, OS, 4096, false); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }finally{ioutils.closestream (in); Ioutils.closestream (OS); } }}
Using the Java API Operation hdfs--copy some files to HDFs