当前位置:   article > 正文

Hadoop-HDFS的增删改查_hadoop zhengshanchagai

hadoop zhengshanchagai

hadoop-HDFS除了在linux上以shell的方式进行操作外,还可以利用java来操作,接下来我们就来实现吧

1.新建java工程,引入hadoop的源码包


                                                java操作hadoop1.png

当然,也可以新建lib包,复制hadoop/下的jar

2 代码操作

方式一:FileSystem与IOUtils

  1. /**
  2. *
  3. * @author ph
  4. * 使用systemfile
  5. */
  6. public class app2 {
  7. //查看hadoop的根目录下的hello文件
  8. public static final String HDFS_PATH = "hdfs://hadoop:9000/hello";
  9. public static final String DIR_PATH = "/DIR001";
  10. public static final String FILE_PATH = "/DIR001/F1";
  11. private static FsPermission permission;
  12. public static void main(String[] args) throws Exception {
  13. final FileSystem file = FileSystem.get(new URI(HDFS_PATH ), new Configuration());
  14. //创建文件夹
  15. //file.mkdirs(f)
  16. file.mkdirs(new Path(DIR_PATH));
  17. //file.mkdirs(f, permission)
  18. file.mkdirs(new Path("/DIR002"), permission);
  19. //file.mkdirs(fs, dir, permission)
  20. //上传文件
  21. final FSDataOutputStream out = file.create(new Path(FILE_PATH));
  22. final FileInputStream in = new FileInputStream("E:/Demo.py");
  23. IOUtils.copyBytes(in, out, 1024,true);
  24. //下载文件
  25. final FSDataInputStream in = file.open(new Path(FILE_PATH));
  26. IOUtils.copyBytes(in, System.out, 1024,true);
  27. //删除文件(夹)
  28. file.delete(new Path(DIR_PATH), true);
  29. //后边的boolean值是表示 递归与否(如果是删除文件夹就需要用true,问建true或false)
  30. //文件路径是否存在
  31. // boolean falg = file.exists(new Path("/DIR003/file/Demo.py"));
  32. // System.out.println(falg);
  33. //重命名
  34. // file.rename(new Path("/DIR003/Demo1.py"), new Path("/DIR003/Demo.py"));
  35. //是否是文件
  36. // boolean falg1 = file.isFile(new Path("/DIR003/Demo.py"));
  37. // System.out.println(falg1);
  38. //剪切本地文件到hdfs
  39. // file.moveFromLocalFile(new Path("E:/Demo.py"), new Path("/Demo.py"));
  40. //复制本地文件到hdfs
  41. // file.copyFromLocalFile(new Path("E:/Demo.py"), new Path("/Demo.py"));
  42. ... ...
  43. }
  44. }

方式二:FileSystem

  1. public class app3 extends TestCase {
  2. public static String hdfsUrl = "hdfs://hadoop:9000";
  3. @Test
  4. // create HDFS folder 创建一个文件夹
  5. public void testHDFSMkdir() throws Exception {
  6. //一般url只认识http协议
  7. //URL.setURLStreamHandlerFactory(new FsUrlStreamHandlerFactory());//保证url也认识hdfs协议,这样就可以解析HDFS_PATH了
  8. //普通操作
  9. Configuration conf = new Configuration();
  10. FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  11. Path path = new Path("/test5");
  12. fs.mkdirs(path);
  13. //简化
  14. FileSystem fs = FileSystem.get(new URI(hdfsUrl), new Configuration());
  15. fs.mkdirs(new Path("/dir01"));
  16. }
  17. @Test
  18. // create a file 创建一个文件
  19. public void testCreateFile() throws Exception {
  20. //标准操作
  21. Configuration conf = new Configuration();
  22. FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  23. Path path = new Path("/test/a.txt");
  24. FSDataOutputStream out = fs.create(path);
  25. out.write("hello hadoop!".getBytes());
  26. //简单操作
  27. FileSystem fs = FileSystem.get(URI.create(hdfsUrl), new Configuration());
  28. FSDataOutputStream out = fs.create(new Path("/dir01/deom2.txt"));
  29. out.write("hadoop 小试牛刀!".getBytes("utf-8"));
  30. }
  31. @Test
  32. // rename a file 重命名
  33. public void testRenameFile() throws Exception { // rename a file 重命名
  34. //标准操作
  35. Configuration conf = new Configuration();
  36. FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  37. Path path = new Path("/test/a.txt");
  38. Path newPath = new Path("/test/bb.txt");
  39. System.out.println(fs.rename(path, newPath));
  40. //简洁操作
  41. FileSystem fs = FileSystem.get(URI.create(hdfsUrl), new Configuration());
  42. System.out.print(fs.rename(new Path("/dir01/deom2.txt"), new Path("/dir01/demo03.txt")));
  43. }
  44. @Test
  45. // 上传文件
  46. public void testUploadLocalFile1() throws Exception { // upload a local file
  47. //标准操作
  48. Configuration conf = new Configuration();
  49. FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  50. Path src = new Path("E:/Demo.py");
  51. Path dst = new Path("/test");
  52. fs.copyFromLocalFile(src, dst);
  53. //简洁操作
  54. FileSystem fs = FileSystem.get(URI.create(hdfsUrl), new Configuration());
  55. fs.copyFromLocalFile(new Path("E:/Demo.py"), new Path("/dir01"));
  56. }
  57. @Test
  58. //上传文件
  59. public void testUploadLocalFile2() throws Exception {
  60. //标准书写
  61. Configuration conf = new Configuration();
  62. FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  63. Path src = new Path("E:/Demo.py");
  64. Path dst = new Path("/test1");
  65. InputStream in = new BufferedInputStream(new FileInputStream(new File(
  66. "/home/hadoop/hadoop-1.2.1/bin/rcc")));
  67. FSDataOutputStream out = fs.create(new Path("/test/rcc1"));
  68. IOUtils.copyBytes(in, out, 4096);
  69. //简单
  70. FileSystem fs = FileSystem.get(URI.create(hdfsUrl), new Configuration());
  71. final FSDataOutputStream out = fs.create(new Path("/dir01/files"));
  72. final FileInputStream in = new FileInputStream("E:/Demo.py");
  73. IOUtils.copyBytes(in, out, 1024,true);
  74. }
  75. @Test
  76. // 列出文件
  77. public void testListFIles() throws Exception { // list files under folder
  78. //标准书写
  79. Configuration conf = new Configuration();
  80. FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  81. Path dst = new Path("/test");
  82. FileStatus[] files = fs.listStatus(dst);
  83. for (FileStatus file : files) {
  84. System.out.println(file.getPath().toString());
  85. }
  86. //简洁书写
  87. FileSystem fs = FileSystem.get(URI.create(hdfsUrl),new Configuration());
  88. FileStatus[] files = fs.listStatus(new Path("/dir01"));
  89. for (FileStatus file : files) {
  90. System.out.println(file.getPath().toString());
  91. }
  92. }
  93. @Test
  94. // 查找文件所在的数据块
  95. public void testGetBlockInfo() throws Exception { // list block info of file
  96. Configuration conf = new Configuration();
  97. FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  98. Path dst = new Path("/dir01/Demo.py");
  99. FileStatus fileStatus = fs.getFileStatus(dst);
  100. BlockLocation[] blkloc = fs.getFileBlockLocations(fileStatus, 0,
  101. fileStatus.getLen()); // 查找文件所在数据块
  102. for (BlockLocation loc : blkloc) {
  103. for (int i = 0; i < loc.getHosts().length; i++)
  104. System.out.println(loc.getHosts()[i]);
  105. }
  106. }
  107. @Test
  108. //删除文件、文件夹
  109. public void testRemoveFile () throws Exception {
  110. FileSystem fs = FileSystem.get(URI.create(hdfsUrl), new Configuration());
  111. fs.delete(new Path("/test5"), true);//是否递归删除
  112. }
  113. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/510616
推荐阅读
相关标签
  

闽ICP备14008679号