15 package com.cloudera.impala.common;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.io.InputStream;
21 import java.util.UUID;
23 import org.apache.commons.io.IOUtils;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.fs.FileStatus;
26 import org.apache.hadoop.fs.FileSystem;
27 import org.apache.hadoop.fs.FileUtil;
28 import org.apache.hadoop.fs.LocalFileSystem;
29 import org.apache.hadoop.fs.Path;
30 import org.apache.hadoop.fs.s3.S3FileSystem;
31 import org.apache.hadoop.fs.s3a.S3AFileSystem;
32 import org.apache.hadoop.fs.s3native.NativeS3FileSystem;
33 import org.apache.hadoop.hdfs.DistributedFileSystem;
34 import org.apache.hadoop.hdfs.client.HdfsAdmin;
35 import org.apache.hadoop.hdfs.protocol.EncryptionZone;
36 import org.apache.log4j.Logger;
38 import com.google.common.base.Preconditions;
44 private static final Configuration
CONF =
new Configuration();
45 private static final Logger
LOG = Logger.getLogger(FileSystemUtil.class);
53 FileSystem fs = directory.getFileSystem(
CONF);
54 Preconditions.checkState(fs.getFileStatus(directory).isDirectory());
55 int numFilesDeleted = 0;
56 for (FileStatus fStatus: fs.listStatus(directory)) {
58 if (fStatus.isFile() && !
isHiddenFile(fStatus.getPath().getName())) {
59 LOG.debug(
"Removing: " + fStatus.getPath());
60 fs.delete(fStatus.getPath(),
false);
64 return numFilesDeleted;
71 FileSystem fs = directory.getFileSystem(
CONF);
72 Preconditions.checkState(fs.getFileStatus(directory).isDirectory());
74 for (FileStatus fStatus: fs.listStatus(directory)) {
76 if (fStatus.isFile() && !
isHiddenFile(fStatus.getPath().getName())) {
87 Path p2)
throws IOException {
88 HdfsAdmin hdfsAdmin =
new HdfsAdmin(fs.getUri(),
CONF);
89 EncryptionZone z1 = hdfsAdmin.getEncryptionZoneForPath(p1);
90 EncryptionZone z2 = hdfsAdmin.getEncryptionZoneForPath(p2);
91 if (z1 == null && z2 == null)
return true;
92 if (z1 == null || z2 == null)
return false;
107 FileSystem fs = destDir.getFileSystem(
CONF);
108 Preconditions.checkState(fs.isDirectory(destDir));
109 Preconditions.checkState(fs.isDirectory(sourceDir));
114 UUID uuid = UUID.randomUUID();
117 int numFilesMoved = 0;
118 for (FileStatus fStatus: fs.listStatus(sourceDir)) {
119 if (fStatus.isDirectory()) {
120 LOG.debug(
"Skipping copy of directory: " + fStatus.getPath());
126 Path destFile =
new Path(destDir, fStatus.getPath().getName());
127 if (fs.exists(destFile)) {
128 destFile =
new Path(destDir,
131 FileSystemUtil.relocateFile(fStatus.getPath(), destFile,
false);
134 return numFilesMoved;
148 boolean renameIfAlreadyExists)
throws IOException {
149 FileSystem fs = dest.getFileSystem(
CONF);
153 Path destFile = fs.isDirectory(dest) ?
new Path(dest, sourceFile.getName()) : dest;
156 if (renameIfAlreadyExists && fs.exists(destFile)) {
157 Path destDir = fs.isDirectory(dest) ? dest : dest.getParent();
158 destFile =
new Path(destDir,
163 LOG.debug(String.format(
164 "Moving '%s' to '%s'", sourceFile.toString(), destFile.toString()));
166 fs.rename(sourceFile, destFile);
171 LOG.info(String.format(
172 "Copying source '%s' to '%s' because HDFS encryption zones are different",
173 sourceFile, destFile));
174 FileUtil.copy(sourceFile.getFileSystem(
CONF), sourceFile, fs, destFile,
182 public static String
readFile(Path file)
throws IOException {
183 FileSystem fs = file.getFileSystem(
CONF);
184 InputStream fileStream = fs.open(file);
186 return IOUtils.toString(fileStream);
188 IOUtils.closeQuietly(fileStream);
201 StringBuilder sb =
new StringBuilder(baseFileName);
203 int extensionIdx = baseFileName.lastIndexOf(
'.');
204 if (extensionIdx != -1) {
205 sb.replace(extensionIdx, extensionIdx + 1,
"_" + appendStr +
".");
207 sb.append(
"_" + appendStr);
209 return sb.toString();
216 throws FileNotFoundException, IOException {
217 FileSystem fs = directory.getFileSystem(
CONF);
219 for (FileStatus fStatus: fs.listStatus(directory)) {
220 if (fStatus.isDirectory()) {
231 FileSystem fs = directory.getFileSystem(
CONF);
232 Path tmpDir =
new Path(directory,
".tmp_" + UUID.randomUUID().toString());
241 String lcFileName = fileName.toLowerCase();
242 return lcFileName.startsWith(
".") || lcFileName.startsWith(
"_") ||
243 lcFileName.endsWith(
".copying") || lcFileName.endsWith(
".tmp");
253 return !(fs instanceof S3AFileSystem || fs instanceof NativeS3FileSystem ||
254 fs instanceof S3FileSystem || fs instanceof LocalFileSystem);
261 return fs instanceof DistributedFileSystem;
272 Path
path =
new Path(FileSystem.getDefaultUri(
CONF));
273 FileSystem fs = path.getFileSystem(
CONF);
274 Preconditions.checkState(fs instanceof DistributedFileSystem);
275 return (DistributedFileSystem) fs;
282 URI defaultUri = FileSystem.getDefaultUri(
CONF);
283 URI locationUri = location.toUri();
287 if (locationUri.getScheme() == null ||
288 locationUri.getScheme().equalsIgnoreCase(defaultUri.getScheme())) {
289 return location.makeQualified(defaultUri, location);
302 fs.makeQualified(
path);
304 }
catch (IllegalArgumentException e) {
316 if (fs.exists(path)) {
319 error_msg.append(
"Path does not exist.");
321 }
catch (Exception e) {
322 error_msg.append(e.getMessage());
static boolean arePathsInSameEncryptionZone(FileSystem fs, Path p1, Path p2)
static boolean containsSubdirectory(Path directory)
string path("/usr/lib/sasl2:/usr/lib64/sasl2:/usr/local/lib/sasl2:/usr/lib/x86_64-linux-gnu/sasl2")
static boolean hasGetFileBlockLocations(FileSystem fs)
static Path createFullyQualifiedPath(Path location)
static String appendToBaseFileName(String baseFileName, String appendStr)
static String readFile(Path file)
static int deleteAllVisibleFiles(Path directory)
static boolean isDistributedFileSystem(Path path)
static final Configuration CONF
static DistributedFileSystem getDistributedFileSystem()
static boolean isDistributedFileSystem(FileSystem fs)
static int relocateAllVisibleFiles(Path sourceDir, Path destDir)
static Configuration getConfiguration()
static boolean isHiddenFile(String fileName)
static Boolean isPathReachable(Path path, FileSystem fs, StringBuilder error_msg)
static Path makeTmpSubdirectory(Path directory)
static void relocateFile(Path sourceFile, Path dest, boolean renameIfAlreadyExists)
static int getTotalNumVisibleFiles(Path directory)
static Boolean isPathOnFileSystem(Path path, FileSystem fs)