Guest User

Untitled

a guest
May 22nd, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.fs.*;
  3. import org.apache.hadoop.io.IOUtils;
  4. import org.junit.After;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7.  
  8. import java.net.URI;
  9. import java.net.URISyntaxException;
  10.  
  11. /**
  12. * HDFS Java API Test
  13. * Created by AhianZhang on 2018/5/17.
  14. */
  15. public class HDFSTest
  16. {
  17. FileSystem fileSystem = null;
  18. Configuration configuration = null;
  19. public static final String HDFS_PATH = "hdfs://hadoop01:9000";
  20.  
  21. @Before
  22. public void setUp() throws Exception
  23. {
  24. configuration = new Configuration();
  25. fileSystem = FileSystem.get(new URI(HDFS_PATH), configuration,"hadoop01");
  26. System.out.println("Start");
  27. }
  28.  
  29. /**
  30. * 创建文件夹
  31. * @throws Exception
  32. */
  33. @Test
  34. public void mkdir()throws Exception{
  35. fileSystem.mkdirs(new Path("/JavaApiOpt/test"));
  36. }
  37.  
  38. /**
  39. * 创建文件
  40. * @throws Exception
  41. */
  42. @Test
  43. public void creatFile()throws Exception{
  44. FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/JavaApiOpt/test/javaAPITest.log"));
  45. fsDataOutputStream.write("This is Java Test".getBytes());
  46. fsDataOutputStream.flush();
  47. fsDataOutputStream.close();
  48. }
  49.  
  50. /**
  51. * 查看文件
  52. * @throws Exception
  53. */
  54. @Test
  55. public void cat()throws Exception{
  56. FSDataInputStream fsDataInputStream = fileSystem.open(new Path("/JavaApiOpt/test/javaAPITest.log"));
  57. IOUtils.copyBytes(fsDataInputStream,System.out,1024);
  58. fsDataInputStream.close();
  59. }
  60.  
  61. /**
  62. * 重命名
  63. * @throws Exception
  64. */
  65. @Test
  66. public void rename()throws Exception{
  67. Path oldPath = new Path("/JavaApiOpt/test/javaAPITest.log");
  68. Path newPath = new Path("/JavaApiOpt/test/javaAPIRename.log");
  69. boolean isSuccess = fileSystem.rename(oldPath,newPath);
  70. System.out.println(isSuccess);
  71. }
  72.  
  73. /**
  74. * 从本地复制文件到HDFS中
  75. * @throws Exception
  76. * 注意:使用 JavaAPI 上传的文件默认采用三个副本策略,而在Hadoop中是一个副本
  77. */
  78. @Test
  79. public void copyFromLocalFile()throws Exception{
  80. Path LocalPath = new Path("C:\\Users\\Videos\\Free YouTube Downloader\\gai.mp4");
  81. Path ToPath = new Path("/JavaApiOpt/test/");
  82.  
  83. fileSystem.copyFromLocalFile(LocalPath,ToPath);
  84. }
  85.  
  86. /**
  87. * 下载
  88. * @throws Exception
  89. */
  90. @Test
  91. public void copyToLocal()throws Exception{
  92. Path ToPath = new Path("D:/gai.mp4");
  93. Path FromPath = new Path("/JavaApiOpt/test/gai.mp4");
  94.  
  95. fileSystem.copyToLocalFile(false,FromPath,ToPath,true);
  96. }
  97.  
  98. /**
  99. * 列出文件/文件夹
  100. * @throws Exception
  101. */
  102. @Test
  103. public void listFiles()throws Exception{
  104. RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"),true);
  105. while (listFiles.hasNext()) {
  106.  
  107. LocatedFileStatus file = listFiles.next();
  108.  
  109. System.out.println(file.getPath().getName());
  110.  
  111. }
  112. System.out.println("=====================================================");
  113. FileStatus[] status = fileSystem.listStatus(new Path("/"));
  114. for (FileStatus file : status) {
  115.  
  116. System.out.println(file.getPath().getName() + " " + (file.isDirectory() ? "d" : "f"));
  117.  
  118. }
  119. }
  120.  
  121. /**
  122. * 删除
  123. * @throws Exception
  124. */
  125. @Test
  126. public void delete()throws Exception{
  127. fileSystem.delete(new Path("/JavaApiOpt/test/gai.mp4"),true);
  128. }
  129. @After
  130. public void tearDown() throws Exception
  131. {
  132. configuration = null;
  133. fileSystem = null;
  134. System.out.println("Close Done");
  135. }
  136. }
Add Comment
Please, Sign In to add comment