From 039a7f4730653a8264c092845b5602ccb692a7ef Mon Sep 17 00:00:00 2001 From: Russell Mora Date: Mon, 23 Sep 2024 23:58:23 -0400 Subject: [PATCH] Allow overwrites during rename (#210) This is required for doing atomic puts. --- hdfs/client.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/hdfs/client.py b/hdfs/client.py index 6c70feb..ffe8323 100644 --- a/hdfs/client.py +++ b/hdfs/client.py @@ -924,7 +924,7 @@ def delete(self, hdfs_path, recursive=False, skip_trash=True): _logger.info('%r moved to trash at %r.', hdfs_path, dst_path) return True - def rename(self, hdfs_src_path, hdfs_dst_path): + def rename(self, hdfs_src_path, hdfs_dst_path, overwrite=False): """Move a file or folder. :param hdfs_src_path: Source path. @@ -932,11 +932,14 @@ def rename(self, hdfs_src_path, hdfs_dst_path): a directory, the source will be moved into it. If the path exists and is a file, or if a parent destination directory is missing, this method will raise an :class:`HdfsError`. + :param overwrite: overwrite the hdfs_dst_path if it already exists, rather + than raising an :class:`HdfsError`. """ _logger.info('Renaming %r to %r.', hdfs_src_path, hdfs_dst_path) hdfs_dst_path = self.resolve(hdfs_dst_path) - res = self._rename(hdfs_src_path, destination=hdfs_dst_path) + kwargs = {"renameoptions": "overwrite"} if overwrite else {} + res = self._rename(hdfs_src_path, destination=hdfs_dst_path, **kwargs) if not res.json()['boolean']: raise HdfsError( 'Unable to rename %r to %r.',