Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.net.URI;
- import java.net.URISyntaxException;
- /**
- * HDFS Java API Test
- * Created by AhianZhang on 2018/5/17.
- */
- public class HDFSTest
- {
- FileSystem fileSystem = null;
- Configuration configuration = null;
- public static final String HDFS_PATH = "hdfs://hadoop01:9000";
- @Before
- public void setUp() throws Exception
- {
- configuration = new Configuration();
- fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration,"hadoop01");
- System.out.println("Start");
- }
- /**
- * 创建文件夹
- * @throws Exception
- */
- @Test
- public void mkdir()throws Exception{
- fileSystem.mkdirs(new Path("/JavaApiOpt/test"));
- }
- /**
- * 创建文件
- * @throws Exception
- */
- @Test
- public void creatFile()throws Exception{
- FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/JavaApiOpt/test/javaAPITest.log"));
- fsDataOutputStream.write("This is Java Test".getBytes());
- fsDataOutputStream.flush();
- fsDataOutputStream.close();
- }
- /**
- * 查看文件
- * @throws Exception
- */
- @Test
- public void cat()throws Exception{
- FSDataInputStream fsDataInputStream = fileSystem.open(new Path("/JavaApiOpt/test/javaAPITest.log"));
- IOUtils.copyBytes(fsDataInputStream,System.out,1024);
- fsDataInputStream.close();
- }
- /**
- * 重命名
- * @throws Exception
- */
- @Test
- public void rename()throws Exception{
- Path oldPath = new Path("/JavaApiOpt/test/javaAPITest.log");
- Path newPath = new Path("/JavaApiOpt/test/javaAPIRename.log");
- boolean isSuccess = fileSystem.rename(oldPath,newPath);
- System.out.println(isSuccess);
- }
- /**
- * 从本地复制文件到HDFS中
- * @throws Exception
- * 注意:使用 JavaAPI 上传的文件默认采用三个副本策略,而在Hadoop中是一个副本
- */
- @Test
- public void copyFromLocalFile()throws Exception{
- Path LocalPath = new Path("C:\\Users\\Videos\\Free YouTube Downloader\\gai.mp4");
- Path ToPath = new Path("/JavaApiOpt/test/");
- fileSystem.copyFromLocalFile(LocalPath,ToPath);
- }
- /**
- * 下载
- * @throws Exception
- */
- @Test
- public void copyToLocal()throws Exception{
- Path ToPath = new Path("D:/gai.mp4");
- Path FromPath = new Path("/JavaApiOpt/test/gai.mp4");
- fileSystem.copyToLocalFile(false,FromPath,ToPath,true);
- }
- /**
- * 列出文件/文件夹
- * @throws Exception
- */
- @Test
- public void listFiles()throws Exception{
- RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"),true);
- while (listFiles.hasNext()) {
- LocatedFileStatus file = listFiles.next();
- System.out.println(file.getPath().getName());
- }
- System.out.println("=====================================================");
- FileStatus[] status = fileSystem.listStatus(new Path("/"));
- for (FileStatus file : status) {
- System.out.println(file.getPath().getName() + " " + (file.isDirectory() ? "d" : "f"));
- }
- }
- /**
- * 删除
- * @throws Exception
- */
- @Test
- public void delete()throws Exception{
- fileSystem.delete(new Path("/JavaApiOpt/test/gai.mp4"),true);
- }
- @After
- public void tearDown() throws Exception
- {
- configuration = null;
- fileSystem = null;
- System.out.println("Close Done");
- }
- }
Add Comment
Please, Sign In to add comment