Hadoop HDFs file operation implementation upload file to Hdfs_java

Source: Internet
Author: User

HDFs file operation examples, including uploading files to HDFs, downloading files from HDFs, and deleting files on HDFs, refer to the use of

Copy Code code as follows:

Import org.apache.hadoop.conf.Configuration;
Import org.apache.hadoop.fs.*;

Import Java.io.File;
Import java.io.IOException;
public class Hadoopfile {
Private Configuration conf =null;

Public Hadoopfile () {
Conf =new Configuration ();
Conf.addresource (New Path ("/hadoop/etc/hadoop/core-site.xml"));
}

Public Hadoopfile (Configuration conf) {
This.conf =conf;
}

    public boolean sendFile (String path,string localfile) {
         file File=new file (LocalFile);
        if (!file.isfile ()) {
             System.out.println (File.getname ());
            return false;
       }
        try {
             filesystem Localfs =filesystem.getlocal (conf);
            filesystem Hadoopfs =filesystem.get (conf);
            Path hadpath=new path (path);

            Fsdataoutputstream Fsout=hadoopfs.create ( New Path (path+ "/" +file.getname ());
            Fsdatainputstream Fsin=localfs.open (new Path (LocalFile));
            byte[] buf =new byte[1024];
            int readbytes=0;
            while (Readbytes=fsin.read (BUF)) >0) {
                fsout.write (buf, 0,readbytes);
           }
            fsin.close ();
            fsout.close ();

Filestatus[] hadfiles= hadoopfs.liststatus (Hadpath);
for (Filestatus fs:hadfiles) {
System.out.println (Fs.tostring ());
}
return true;
catch (IOException e) {
E.printstacktrace ();
}
return false;
}

public boolean delfile (String hadfile) {
try {

            filesystem Hadoopfs =filesystem.get (conf);
            Path hadpath=new path (hadfile);
            Path p=hadpath.getparent ();
            boolean rtnval= hadoopfs.delete (Hadpath, true);

Filestatus[] hadfiles= hadoopfs.liststatus (p);
for (Filestatus fs:hadfiles) {
System.out.println (Fs.tostring ());
}
return rtnval;
catch (IOException e) {
E.printstacktrace ();
}
return false;
}


public boolean downloadFile (String hadfile,string localpath) {

        try {
             filesystem Localfs =filesystem.getlocal (conf);
            filesystem Hadoopfs =filesystem.get (conf);
            Path hadpath=new path (hadfile);

            Fsdataoutputstream Fsout=localfs.create ( New Path (localpath+ "/" +hadpath.getname ());
            Fsdatainputstream Fsin=hadoopfs.open ( Hadpath);
            byte[] buf =new byte[1024];
            int readbytes=0;
            while (Readbytes=fsin.read (BUF)) >0) {
                fsout.write (buf, 0,readbytes);
           }
            fsin.close ();
            fsout.close ();

return true;
catch (IOException e) {
E.printstacktrace ();
}
return false;
}
}

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.