刚开始学hadoop,大家一起加油,再接再厉
package com.atguigu.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
public class HdfsClient {
@Test
public void testMkdirs() throws IOException, URISyntaxException, InterruptedException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration,"atguigu");
fs.mkdirs(new Path("/xiyou/huaguoshan/"));
fs.close();
}
@Test
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
Configuration configuration = new Configuration();
configuration.set("dfs.replication", "2");
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
fs.copyFromLocalFile(new Path("F:/local_text/00.txt"), new Path("/xiyou/huaguoshan"));
fs.close();
}
@Test
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
fs.copyToLocalFile(false, new Path("/xiyou/huaguoshan/sunwukong.txt"), new Path("F:/local_text/Y23M03"), true);
fs.close();
}
@Test
public void testRename() throws IOException, InterruptedException, URISyntaxException{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
fs.rename(new Path("/xiyou/huaguoshan/sunwukong.txt"), new Path("/xiyou/huaguoshan/自己手动修改名称.txt"));
fs.close();
}
@Test
public void testDelete() throws IOException, InterruptedException, URISyntaxException{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
fs.delete(new Path("/xiyou"), true);
fs.close();
}
@Test
public void testListFiles() throws IOException, InterruptedException, URISyntaxException {
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/xiyou/huaguoshan"), true);
while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
System.out.println("**************************************************************************************");
System.out.println("文件路径以及当前处理的文件名称");
System.out.println("========" + fileStatus.getPath() + "=========");
System.out.println("文件类型及其UserGroupOther权限:");
System.out.println(fileStatus.getPermission());
System.out.println("文件的所有人:");
System.out.println(fileStatus.getOwner());
System.out.println("文件的所属组:");
System.out.println(fileStatus.getGroup());
System.out.println("文件大小(单位B):");
System.out.println(fileStatus.getLen());
System.out.println("文件的修改时间--距离格林威治:");
System.out.println(fileStatus.getModificationTime());
System.out.println(timechange());
System.out.println("当前处理该文件的Replication块的编号");
System.out.println(fileStatus.getReplication());
System.out.println("该文件处所的内存块大小");
System.out.println(fileStatus.getBlockSize());
System.out.println("文件名");
System.out.println(fileStatus.getPath().getName());
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
System.out.println(Arrays.toString(blockLocations));
}
fs.close();
}
public static int timechange(){
Date date = new Date();
Long time = date.getTime();
Date d = new Date(time);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
System.out.println(sdf.format(d));
return 0;
}
@Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException{
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:8020"), configuration, "atguigu");
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
if (fileStatus.isFile()) {
System.out.println("f:"+fileStatus.getPath().getName());
}else {
System.out.println("d:"+fileStatus.getPath().getName());
}
}
fs.close();
}
}