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.*;
import static org.junit.Assert.*;
public class TestHDFS {
private FileSystem fileSystem;
private Configuration configuration;
@Before
public void before() throws IOException {
configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://CentOS:9000");
configuration.set("dfs.replication","1");
configuration.set("fs.trash.interval","1");
fileSystem=FileSystem.get(configuration);
assertNotNull(fileSystem);
}
//文件上传
@Test
public void testupload() throws IOException {
InputStream inputStream = new FileInputStream("C:\\Users\\chenda\\Desktop\\wx.txt");
Path path = new Path("xx.txt");
OutputStream outputStream =fileSystem.create(path);
IOUtils.copyBytes(inputStream,outputStream,1024,true);
}
//文件上传
@Test
public void testupload2() throws IOException {
Path path = new Path("file:///C:\\Users\\chenda\\Desktop\\wx.txt");
Path src= new Path("dd.txt");
fileSystem.copyFromLocalFile(path,src);
}
//文件下载
@Test
public void testDownload02() throws IOException {
OutputStream os=new FileOutputStream("C:\\Users\\chenda\\Desktop\\22.txt");
InputStream is = fileSystem.open(new Path("dd.txt"));
IOUtils.copyBytes(is,os,1024,true);
}
//删除文件
@Test
public void deletetest() throws IOException {
Path path = new Path("/dd.txt");
fileSystem.delete(path,true);
}
//展示所有文件
@Test
public void Filelist() throws IOException {
Path path = new Path("/");
RemoteIterator<LocatedFileStatus> iterator=fileSystem.listFiles(path, true);
while(iterator.hasNext()){
LocatedFileStatus status = iterator.next();
System.out.println(status.getPath()+" -- "+status.getLen()+"是不是文件 :"+status.isFile());
}
}
@Test
public void testDeleteWithTrash() throws IOException {
Trash trash=new Trash(configuration);
trash.moveToTrash(new Path("dd.txt"));
}
@After
public void after() throws IOException {
fileSystem.close();
}
}
Hadoop-部分测试API
最新推荐文章于 2025-08-12 11:06:43 发布