Operation of the HDFs file system

Source: Internet
Author: User

Package com.bank.utils;

Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.net.URI;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

Import org.apache.hadoop.conf.Configuration;
Import Org.apache.hadoop.fs.FSDataInputStream;
Import Org.apache.hadoop.fs.FSDataOutputStream;
Import Org.apache.hadoop.fs.FileStatus;
Import Org.apache.hadoop.fs.FileSystem;
Import Org.apache.hadoop.fs.Path;
Import Org.apache.hadoop.io.IOUtils;

/**
* HDFS General operation
* @author Mengyao
*
*/
public class Hdfsutils {

Private final static String Dfs_path = "hdfs://ns1";
Private final static String USER = "root";

public static void Main (string[] args) throws Exception {
Configuration conf = new configuration ();
Conf.set ("Fs.defaultfs", Dfs_path);
Conf.set ("Dfs.nameservices", "ns1");
Conf.set ("dfs.ha.namenodes.ns1", "nn1,nn2");
Conf.set ("Dfs.namenode.rpc-address.ns1.nn1", "h1:9000");
Conf.set ("Dfs.namenode.rpc-address.ns1.nn2", "h2:9000");
Conf.set ("Dfs.client.failover.proxy.provider.ns1", " Org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider ");

FileSystem fs = Filesystem.get (New URI ("Hdfs://ns1"), conf, USER);

Create a folder on HDFs
Createdir (FS, "/hkd/hongkong");

Delete the folder or file on HDFs, the folder is True
Deletefileordir (FS, "/HKD");

Upload local file to HDFs, overwrite if file exists
Upload (FS, "D:/data", "/data.dat");

Download files from HDFs to local
Download (FS, "/cny/data/data", "D:/data");

Delete files on HDFs, if present
DeleteFile (FS, "/data.dat");

Read all file and folder information in the specified directory on HDFs
Readdfspath (FS, "/CNY");
}

public static Boolean Createdir (FileSystem FS, String Dfsnewdir) {
Boolean status = FALSE;
try {
if (Fs.exists (new Path (Dfsnewdir))) {
System.err.println ("This dir exist!");
return status;
}
Status = Fs.mkdirs (new Path (Dfsnewdir));
} catch (IllegalArgumentException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
Fs.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

return status;
}

public static Boolean Deletefileordir (FileSystem FS, String Dfspath) {
Boolean status = FALSE;
try {
Status = Fs.delete (new Path (Dfspath), true);
} catch (IllegalArgumentException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
Fs.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

return status;
}

public static Boolean upload (FileSystem FS, String LocalPath, String dfspath) {
Boolean status = FALSE;
try {
Fsdataoutputstream out = fs.create (new Path (Dfspath), true);
Bufferedinputstream in = new Bufferedinputstream (new FileInputStream (New File (LocalPath));
Ioutils.copybytes (in, out, 4096, true);
} catch (IllegalArgumentException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
Fs.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

return status;
}

public static Boolean Download (FileSystem FS, String Dfspath, String localPath) {
Boolean status = FALSE;
try {
Fsdatainputstream in = Fs.open (new Path (Dfspath));
Bufferedoutputstream out = new Bufferedoutputstream (new FileOutputStream (New File (LocalPath)));
Ioutils.copybytes (in, out, 4096, true);
Status = TRUE;
} catch (IllegalArgumentException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
Fs.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

return status;
}

public static Boolean DeleteFile (FileSystem FS, String Dfspath) {
Boolean status = FALSE;
try {
if (Fs.exists (new Path (Dfspath))) {
Status = Fs.delete (new Path (Dfspath), true);
}
} catch (IllegalArgumentException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
try {
Fs.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}

return status;
}


public static void Readdfspath (FileSystem FS, String Dfspath) {
try {
filestatus[] Liststatus = fs.liststatus (new Path (Dfspath));
for (Filestatus fsstat:liststatus) {
String isdir = Fsstat.isdirectory ()? " Folder ":" File ";
Final String Permission = Fsstat.getpermission (). toString ();
Final short replication = Fsstat.getreplication ();
Final Long len = Fsstat.getlen ();
Final String datestr = new SimpleDateFormat ("Yyyy-mm-dd hh:MM:ss"). Format (New Date (Fsstat.getaccesstime ()));
Final String Path = Fsstat.getpath (). toString ();
System.out.println (isdir+ "\ t" +permission+ "\ T" +replication+ "\ T" +len+ "\ T" +datestr+ "\ T" +path);
}
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IllegalArgumentException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}

Operation of the HDFs file system

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.