Skip to content

Commit 92dd72f

Browse files
committed
Add rename operation for files/directories on HdfsClient
1 parent 679c6f0 commit 92dd72f

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

tempto-core/src/main/java/io/trino/tempto/hadoop/hdfs/HdfsClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ InputStream getInputStream()
4545

4646
void createDirectory(String path);
4747

48+
void rename(String sourcePath, String destinationPath);
49+
4850
void delete(String path);
4951

5052
void saveFile(String path, InputStream input);

tempto-core/src/main/java/io/trino/tempto/internal/hadoop/hdfs/WebHdfsClient.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import com.fasterxml.jackson.core.type.TypeReference;
1818
import com.fasterxml.jackson.databind.ObjectMapper;
19-
import com.google.common.collect.ImmutableList;
2019
import com.google.common.collect.ImmutableMap;
2120
import com.google.inject.Inject;
2221
import com.google.inject.name.Named;
@@ -106,6 +105,21 @@ public void createDirectory(String path)
106105
}
107106
}
108107

108+
@Override
109+
public void rename(String sourcePath, String destinationPath)
110+
{
111+
HttpPut renameRequest = new HttpPut(buildUri(sourcePath, "RENAME", ImmutableMap.of("destination", destinationPath)));
112+
try (CloseableHttpResponse response = httpRequestsExecutor.execute(renameRequest)) {
113+
if (response.getStatusLine().getStatusCode() != SC_OK) {
114+
throw invalidStatusException("RENAME", sourcePath, renameRequest, response);
115+
}
116+
logger.debug("Renamed file/directory {} to {} - username: {}", sourcePath, destinationPath, username);
117+
}
118+
catch (IOException e) {
119+
throw new RuntimeException("Could not rename file/directory " + sourcePath + " to " + destinationPath + " in hdfs, user: " + username, e);
120+
}
121+
}
122+
109123
@Override
110124
public void delete(String path)
111125
{

tempto-examples/src/main/java/io/trino/tempto/examples/HdfsClientTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,10 @@ public void testMetadataOperations() {
6060
hdfsClient.createDirectory(testPath + "/b");
6161
hdfsClient.createDirectory(testPath + "/c");
6262
assertThat(hdfsClient.listDirectory(testPath)).containsAll(ImmutableList.of("a", "b", "c"));
63+
64+
hdfsClient.createDirectory(testPath + "/rename");
65+
hdfsClient.createDirectory(testPath + "/rename/src");
66+
hdfsClient.rename(testPath + "/rename/src", testPath + "/rename/dest");
67+
assertThat(hdfsClient.listDirectory(testPath + "/rename")).containsAll(ImmutableList.of("dest"));
6368
}
6469
}

0 commit comments

Comments
 (0)