Package HDFs;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.net.URI;
Import java.net.URISyntaxException;
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;
public class App2 {
public static void Main (string[] args) throws IOException, URISyntaxException {
Final FileSystem FileSystem = Filesystem.get (New URI ("hdfs://hadoop:9000"), New Configuration ());
Makdir (FileSystem);
Upload
PutData (FileSystem);
Download (FileSystem);
ListFile (FileSystem);
Delete
Delete (FileSystem);
}
public static void ListFile (final FileSystem FileSystem) throws IOException {
System.out.println ("-------file list start-----------");
filestatus[] Liststatus = filesystem.liststatus (New Path ("/"));
for (Filestatus filestatus:liststatus) {
System.out.println (Filestatus.getpath (). toString ());
}
System.out.println ("-------file list End-----------");
}
public static void Delete (final FileSystem FileSystem) throws IOException {
Boolean delete = Filesystem.delete (new Path ("/dir2"), true);
if (delete) {
SYSTEM.OUT.PRINTLN ("-----deletet OK---------! ");
}
}
public static void Download (final FileSystem FileSystem) throws IOException {
Fsdatainputstream in = Filesystem.open (new Path ("/dir2/readme"));
Ioutils.copybytes (in, System.out, 1024x768, false);
In.close ();
System.out.println ("------------getdata OK-------------------");
}
public static void PutData (final FileSystem FileSystem) throws IOException,
FileNotFoundException {
Fsdataoutputstream out=filesystem.create (New Path ("/dir2/readme"));
FileInputStream in=new FileInputStream ("/home/hadoop/hello");
Ioutils.copybytes (in, out, 1024x768, true);
SYSTEM.OUT.PRINTLN ("update OK");
}
public static void Makdir (final FileSystem FileSystem) throws IOException {
Boolean Successful=filesystem.mkdirs (New Path ("/dir2"));
if (successful) {
System.out.println ("mkdir OK");
}
}
}
Java Operation HDFs