package test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
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 Test {
private FileSystem fs;
private URI uri;
Configuration cf;
//private static String rec="hdfs://localhost:9000/test";
private static String rec="hdfs://localhost:9000/";
Test(String resource){
cf=new Configuration();
try {
uri=new URI(resource);
try {
fs=FileSystem.newInstance(uri, cf);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void createDir(String src){
try {
fs.mkdirs(new Path(src));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void readFile(){
InputStream input=null;
ByteArrayOutputStream ouput=null;
try {
input=fs.open(new Path(rec+"/test"));
ouput=new ByteArrayOutputStream(input.available());
IOUtils.copyBytes(input, ouput, cf);
System.out.print(ouput.toString());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void listAll(String src){
try {
FileStatus[] status=fs.listStatus(new Path(src));
for(int i=0;i<status.length;i++){
System.out.println(status[i].getPath().toString());
}
} catch (IllegalArgumentException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void copyFromLocalDir(String src,String dst){
try {
fs.copyFromLocalFile(new Path(src), new Path(dst));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void copyToLocalDir(String src,String dst){
try {
boolean isExist=fs.exists(new Path(src));
if(isExist){
fs.copyToLocalFile(new Path(src),new Path(dst));
}
else{
System.out.println("文件不存在!");
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void copyAllToLocalDir(String src,String dst){
try {
boolean isExist=fs.exists(new Path(src));
if(isExist){
FileStatus[] status=fs.listStatus(new Path(src));
for(int i=0;i<status.length;i++){
fs.copyToLocalFile(new Path(status[i].getPath().toString()),new Path(dst));
}
}
else{
System.out.println("文件不存在!");
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deleteFile(String src){
try {
boolean isExist=fs.exists(new Path(src));
if(isExist){
fs.delete(new Path(src));
}
else{
System.out.println("文件不存在!");
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Test ts=new Test(rec);
System.out.println("All Files");
ts.listAll(rec);
ts.copyFromLocalDir("/home/wangzun/文檔/hadoopts.pdf", rec+"/test");
ts.listAll(rec+"/test");
ts.copyAllToLocalDir(rec+"/test", "/home/wangzun/hadooptest/");
//ts.deleteFile(rec+"/test/test");
//ts.listAll(rec+"/test");
//ts.copyToLocalDir(rec+"/test/大數據技術原理與應用.pdf", "/home/wangzun/hadooptest/");
//ts.readFile();
}
}