package cagy.hap;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Before;
import org.junit.Test;
public class HDFSClientS {
private FileSystem fs=null;
public void getFS() throws IOException
{
Configuration conf=new Configuration();
conf.set("fs.defaultFS","hdfs://192.168.0.109:9000/");
conf.set("dfs.replication","2");
fs=FileSystem.get(conf);
}
public void upload() throws IllegalArgumentException, IOException
{
getFS();
Path p=new Path("666.exe");
fs.copyFromLocalFile(p, new Path("/cagy/"));
}
public void deleteFile() throws IllegalArgumentException, IOException
{
getFS();
boolean res= fs.delete(new Path("/cagy"), true);
System.out.println(res?"ok":"failed");
}
//@Test
public void createMkdir() throws IllegalArgumentException, IOException
{
getFS();
fs.mkdirs(new Path("/cagy"));
System.out.println("end!");
}
public void rename() throws IllegalArgumentException, IOException
{
getFS();
fs.rename(new Path("/q.txt"), new Path("/qq.txt"));
System.out.println("end!");
}
@Test
public void ls() throws FileNotFoundException, IllegalArgumentException, IOException
{
getFS();
RemoteIterator<LocatedFileStatus> ls=fs.listFiles(new Path("/"), true);
while(ls.hasNext())
{
//列出所有文件
LocatedFileStatus file= ls.next();
System.out.println(file.getPath().getName());
}
System.out.println("*********************");
FileStatus[] stat=fs.listStatus(new Path("/"));
for(FileStatus file:stat)
{
//列出根目录
System.out.println(file.getPath().getName()+" "+(file.isDirectory()?"文件夹":"文件"));
}
}
}