001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hdfs.tools;
019
020import org.apache.hadoop.conf.Configured;
021import org.apache.hadoop.fs.FileSystem;
022import org.apache.hadoop.hdfs.DistributedFileSystem;
023import org.apache.hadoop.hdfs.protocol.BlockStoragePolicy;
024import org.apache.hadoop.util.Tool;
025import org.apache.hadoop.util.ToolRunner;
026
027import java.io.IOException;
028
029/**
030 * A tool listing all the existing block storage policies. No argument is
031 * required when using this tool.
032 */
033public class GetStoragePolicies extends Configured implements Tool {
034
035  @Override
036  public int run(String[] args) throws Exception {
037    FileSystem fs = FileSystem.get(getConf());
038    if (!(fs instanceof DistributedFileSystem)) {
039      System.err.println("GetStoragePolicies can only be used against HDFS. " +
040          "Please check the default FileSystem setting in your configuration.");
041      return 1;
042    }
043    DistributedFileSystem dfs = (DistributedFileSystem) fs;
044
045    try {
046      BlockStoragePolicy[] policies = dfs.getStoragePolicies();
047      System.out.println("Block Storage Policies:");
048      for (BlockStoragePolicy policy : policies) {
049        if (policy != null) {
050          System.out.println("\t" + policy);
051        }
052      }
053    } catch (IOException e) {
054      String[] content = e.getLocalizedMessage().split("\n");
055      System.err.println("GetStoragePolicies: " + content[0]);
056      return 1;
057    }
058    return 0;
059  }
060
061  public static void main(String[] args) throws Exception {
062    int rc = ToolRunner.run(new GetStoragePolicies(), args);
063    System.exit(rc);
064  }
065}