import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class HdfsClient {
//代码重构
//1定义
Configuration conf = null;
FileSystem fs =null;
private static final String HDFS_PATH="hdfs://abner:9000";
private static final String USER_NAME="abner";
//下面这样的结构方便加入类
@Before//在它之前要做的
public void before() throws IOException, InterruptedException {
conf=new Configuration();
conf.set("dfs.replication","2");//设置副本数(在网页50070页面的显示规格)
conf.set("dfs.blocksize","5m");
fs=FileSystem.get(URI.create(HDFS_PATH),conf,USER_NAME);
System.out.println("Set Configuration Successfully!!!");
}
@Test
public void testMkdir() throws IOException {
System.out.println("Execute Mkdir Command !!!");
fs.mkdirs(new Path("/test0/ltf_test"));
}
@Test
public void testCopyFromLocalFile() throws IOException {
System.out.println("Execute CopyFromLocalFile Command !!!");
fs.copyFromLocalFile(new Path("D:\\BigData\\Hadoop\\hadoopTest\\copyTest\\test.txt"), new Path("/test0/test00/test001.txt"));
}
@Test
public void testRename() throws IOException {
System.out.println("Execute Rename Command !!!");
fs.rename(new Path("/test0/test00/test001.txt"),new Path("/test0/rename_ltf.txt"));
}
@Test
public void testDelete() throws IOException {
System.out.println("Execute Delete Command !!!");
//fs.delete(new Path("/rename_test.txt"));
fs.delete(new Path("/rename_test.txt"),true);//recursive 递归向下删除
}
@Test
public void testListFiles() throws IOException {
//迭代器:Java泛型,<>里是泛型。远程迭代器。
RemoteIterator<LocatedFileStatus> listFils = fs.listFiles(new Path("/test0"),true);
while(listFils.hasNext()){ //list之后还有就运行
LocatedFileStatus status = listFils.next();
//输出详情
//输出文件名称,块大小,权限,用户组
System.out.println("文件名称:"+status.getPath().getName());
System.out.println("块大小:"+status.getBlockSize());
System.out.println("权限:"+status.getPermission());
System.out.println("用户组:"+status.getGroup());
//获取存储的块信息
BlockLocation [] blockLocations = status .getBlockLocations();
for (BlockLocation blockLocation: blockLocations){
String[] blockLocationHosts= blockLocation.getHosts();
for(String blockLocationHost:blockLocationHosts){
System.out.println("节点:"+blockLocationHost);
}
}
System.out.println("----------分割线---------------");
}
}
//判断是否是文件夹
@Test
public void testListStatus() throws IOException {
System.out.println("Execute ListStatus Command !!!");
FileStatus[] listStatus = fs.listStatus(new Path("/test0"));
for (FileStatus fileStatus:listStatus){
if (fileStatus.isFile()){
System.out.println("File: "+fileStatus.getPath().getName());
System.out.println("------------------ ");
}
else {
System.out.println("Directory: "+fileStatus.getPath().getName());
System.out.println("------------------------------");
}
}
}
@After
public void after() throws IOException {
System.out.println("Close Resource !!!");
fs.close();
}
}
10-19
10-19