import org.apache.hadoop.fs.FSDataInputStream; //導入方法依賴的package包/類
public int run(String[] args, PrintStream stream) throws Exception {
out = stream;
List paths = parseArgs(args);
if (paths.size() != 1) {
errorln(USAGE);
return E_USAGE;
}
println("Hadoop %s", getVersion());
println("Compiled by %s on %s", getUser(), getDate());
println("Compiled with protoc %s", getProtocVersion());
println("From source with checksum %s", getSrcChecksum());
Configuration conf = getConf();
Path path = new Path(paths.get(0));
FileSystem fs = path.getFileSystem(conf);
println("Filesystem for %s is %s", path, fs);
// examine the FS
Configuration fsConf = fs.getConf();
for (int i = 0; i < props.length; i++) {
showProp(fsConf, (String) props[i][0], (Boolean) props[i][1]);
}
Path root = fs.makeQualified(new Path("/"));
try (DurationInfo d = new DurationInfo(LOG,
"Listing %s", root)) {
println("%s has %d entries", root, fs.listStatus(root).length);
}
String dirName = "dir-" + UUID.randomUUID();
Path dir = new Path(root, dirName);
try (DurationInfo d = new DurationInfo(LOG,
"Creating a directory %s", dir)) {
fs.mkdirs(dir);
}
try {
Path file = new Path(dir, "file");
try (DurationInfo d = new DurationInfo(LOG,
"Creating a file %s", file)) {
FSDataOutputStream data = fs.create(file, true);
data.writeUTF(HELLO);
data.close();
}
try (DurationInfo d = new DurationInfo(LOG,
"Listing %s", dir)) {
fs.listFiles(dir, false);
}
try (DurationInfo d = new DurationInfo(LOG,
"Reading a file %s", file)) {
FSDataInputStream in = fs.open(file);
String utf = in.readUTF();
in.close();
if (!HELLO.equals(utf)) {
throw new IOException("Expected " + file + " to contain the text "
+ HELLO + " -but it has the text \"" + utf + "\"");
}
}
try (DurationInfo d = new DurationInfo(LOG,
"Deleting file %s", file)) {
fs.delete(file, true);
}
} finally {
try (DurationInfo d = new DurationInfo(LOG,
"Deleting directory %s", dir)) {
try {
fs.delete(dir, true);
} catch (Exception e) {
LOG.warn("When deleting {}: ", dir, e);
}
}
}
// Validate parameters.
return SUCCESS;
}