hdfs-javaAPI

20 篇文章 1 订阅
10 篇文章 0 订阅

1.delete

1.1 如果是文件,args1设置是否recursive。

1.2 如果是目录,只能删除最后一级目录和里面的文件,如果设为true,删除目录失败时,会抛异常。

1.3 delete(Path f)已经deprecated,delete(Path f, boolean recursive)

  /** Delete a file.
   *
   * @param f the path to delete.
   * @param recursive if path is a directory and set to 
   * true, the directory is deleted else throws an exception.In
   * case of a file the recursive can be set to either true or false. 
   * @return  true if delete is successful else false. 
   * @throws IOException
   */
  public abstract boolean delete(Path f, boolean recursive) throws IOException;  

2.deleteOnExit(Path f)

给一个文件打个标记,等fs关闭,会自动删除,这个文件必须存在,如果删除失败,fs关闭时会返回错误

3.rename

须知:

  1. 类似于mv,根据目录是否改变改名或更换位置。执行后,源文件就没了
  2. 目标文件不能存在,目标目录必须存在。一般先用exists和mkdir创建一下。
  3. 源路径是目录时,目标路径必须为目录,且必须存在。
  4. 跨文件系统的移动(local到hdfs或者反过来)都是不允许的
  5. rename时如果dstPath已经有,不会执行rename,不会覆盖,而是返回false
  /**
   * Renames Path src to Path dst.  Can take place on local fs
   * or remote DFS.
   * @param src path to be renamed
   * @param dst new path after rename
   * @throws IOException on failure
   * @return true if rename is successful
   */
  public abstract boolean rename(Path src, Path dst) throws IOException;
  /**
   * Renames Path src to Path dst
   * <ul>
   * <li
   * <li>Fails if src is a file and dst is a directory.
   * <li>Fails if src is a directory and dst is a file.
   * <li>Fails if the parent of dst does not exist or is a file.
   * </ul>
   * <p>
   * If OVERWRITE option is not passed as an argument, rename fails
   * if the dst already exists.
   * <p>
   * If OVERWRITE option is passed as an argument, rename overwrites
   * the dst if it is a file or an empty directory. Rename fails if dst is
   * a non-empty directory.
   * <p>
   * Note that atomicity of rename is dependent on the file system
   * implementation. Please refer to the file system documentation for
   * details. This default implementation is non atomic.
   * <p>
   * This method is deprecated since it is a temporary method added to 
   * support the transition from FileSystem to FileContext for user 
   * applications.
   * 
   * @param src path to be renamed
   * @param dst new path after rename
   * @throws IOException on failure
   */
  @Deprecated
  protected void rename(final Path src, final Path dst,
      final Rename... options)

4.mkdirs

须知:

  1. mkdirs(Path f) == mkdirs(f, FsPermission.getDirDefault())
  2. FsPermission是要创建的文件的权限
  3. mkdirs相当于mkdir -p,会创建多级目录

合并文件

一行一行的操作

BufferedReader
BufferedWriter
PrintWriter

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答有关HDFS-JAVA接口上传文件的问题。 首先,您需要在HDFS-JAVA接口中实现一个上传文件的方法。这个方法可以使用Hadoop中的FileSystem和FSDataOutputStream类来实现。以下是一个示例代码: ``` public void uploadFile(String localFilePath, String hdfsFilePath) { Configuration conf = new Configuration(); try { // 获取HDFS文件系统对象 FileSystem fs = FileSystem.get(URI.create(hdfsFilePath), conf); // 创建HDFS文件输出流 FSDataOutputStream outputStream = fs.create(new Path(hdfsFilePath)); // 创建本地文件输入流 FileInputStream inputStream = new FileInputStream(localFilePath); // 将本地文件内容写入HDFS文件 byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } // 关闭流 inputStream.close(); outputStream.close(); fs.close(); } catch (IOException e) { e.printStackTrace(); } } ``` 在这个方法中,您需要传入本地文件的路径和HDFS文件的路径,然后将本地文件内容写入到HDFS文件中。 接下来,您可以在HDFS-JAVA接口的API中调用这个上传文件的方法。在API中,您需要传入本地文件的路径和HDFS文件的路径。以下是一个示例代码: ``` @PostMapping("/upload") public ResponseEntity<String> uploadFile(@RequestParam("localFilePath") String localFilePath, @RequestParam("hdfsFilePath") String hdfsFilePath) { try { // 调用上传文件的方法 uploadFile(localFilePath, hdfsFilePath); return ResponseEntity.ok("文件上传成功"); } catch (IOException e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("文件上传失败"); } } ``` 在这个API中,我们首先从请求参数中获取本地文件的路径和HDFS文件的路径,然后调用之前实现的上传文件的方法将本地文件内容写入到HDFS文件中。 这样,您就可以在HDFS-JAVA接口中实现上传文件的功能了。希望能对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值