hadoop(0.1.1.2)源码学习-org.apache.hadoop.dfs DFSck

DFSCheck 也是个命令行工具类,提供DFS卷的基本检查,以检查是否存在错误

直接看代码吧

public class DFSck extends ToolBase {
  private static final Log LOG = LogFactory.getLog(DFSck.class.getName());

  DFSck() {}
  
  /**
   * Filesystem checker.
   * @param conf current Configuration
   * @throws Exception
   */
  public DFSck(Configuration conf) throws Exception {
    setConf(conf);
  }
  
  private String getInfoServer() throws IOException {
    InetSocketAddress addr = 
      DataNode.createSocketAddr(conf.get("fs.default.name"));
    int infoPort = conf.getInt("dfs.info.port", 50070);
    return addr.getHostName() + ":" + infoPort;
  }
  
  /**
   * @param args
   */
  public int run(String[] args) throws Exception {
    String fsName = getInfoServer();
    if (args.length == 0) {
      System.err.println("Usage: DFSck <path> [-move | -delete] [-files] [-blocks [-locations]]");
      System.err.println("\t<path>\tstart checking from this path");
      System.err.println("\t-move\tmove corrupted files to /lost+found");
      System.err.println("\t-delete\tdelete corrupted files");
      System.err.println("\t-files\tprint out files being checked");
      System.err.println("\t-blocks\tprint out block report");
      System.err.println("\t-locations\tprint out locations for every block");
      return -1;
    }
    StringBuffer url = new StringBuffer("http://"+fsName+"/fsck?path=");
    String dir = "/";
    // find top-level dir first
    for (int idx = 0; idx < args.length; idx++) {
      if (!args[idx].startsWith("-")) { dir = args[idx]; break; }
    }
    url.append(URLEncoder.encode(dir, "UTF-8"));
    for (int idx = 0; idx < args.length; idx++) {
      if (args[idx].equals("-move")) { url.append("&move=1"); }
      else if (args[idx].equals("-delete")) { url.append("&delete=1"); }
      else if (args[idx].equals("-files")) { url.append("&files=1"); }
      else if (args[idx].equals("-blocks")) { url.append("&blocks=1"); }
      else if (args[idx].equals("-locations")) { url.append("&locations=1"); }
    }
    URL path = new URL(url.toString());
    URLConnection connection = path.openConnection();
    InputStream stream = connection.getInputStream();
    InputStreamReader input =
        new InputStreamReader(stream, "UTF-8");
    try {
      int c = input.read();
      while (c != -1) {
        System.out.print((char) c);
        c = input.read();
      }
    } finally {
      input.close();
    }
    return 0;
  }

  public static void main(String[] args) throws Exception {
      int res = new DFSck().doMain(new Configuration(), args);
      System.exit(res);
  }

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值