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 {
static final String PATH="hdfs://192.168.2.27:9000/hello";
static final String DIR = "/d1";
static final String FILE = "/d1/hello";
public static void main(String[] args) throws IOException, URISyntaxException {
//用戶代碼操作HDFS時场梆,是直接調(diào)用FileSystem的子類完成的
FileSystem fileSystem = getFileSystem();
//創(chuàng)建文件夾 hadoop fs -mkdir
//mkdir(fileSystem);
//刪除文件夾
//remove(fileSystem);
//上傳文件
//putData(fileSystem);
//下載文件
//getData(fileSystem);
//瀏覽文件夾
list(fileSystem);
}
private static void list(FileSystem fileSystem) throws IOException {
FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
for(FileStatus fileStatus? :listStatus){
String isDir = fileStatus.isDir()?"文件夾":"文件";
String permission = fileStatus.getPermission().toString();
short replication = fileStatus.getReplication();
long len = fileStatus.getLen();
String path = fileStatus.getPath().toString();
System.out.println(isDir+"\t"+permission+"\t"+replication+"\t"+len+"\t"+path);
}
}
private static void getData(FileSystem fileSystem) throws IOException {
FSDataInputStream in = fileSystem.open(new Path(FILE));
IOUtils.copyBytes(in, System.out, 1024);
}
private static void putData(FileSystem fileSystem) throws IOException, FileNotFoundException {
FSDataOutputStream out = fileSystem.create(new Path(FILE));
FileInputStream in = new FileInputStream("E:/readme.txt");
IOUtils.copyBytes(in, out, 1024,true);
}
private static void remove(FileSystem fileSystem) throws IOException {
fileSystem.delete(new Path(DIR), true);
}
private static void mkdir(FileSystem fileSystem) throws IOException {
fileSystem.mkdirs(new Path(DIR));
}
private static FileSystem? getFileSystem() throws IOException, URISyntaxException {
return FileSystem.get(new URI(PATH), new Configuration());
}
}