Using the Java API Operation hdfs--copy some files to HDFs

Source: Internet
Author: User

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

Related Article

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.