Java Code Examples for org.apache.hadoop.util.StringUtils#popOption()

The following examples show how to use org.apache.hadoop.util.StringUtils#popOption() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DFSAdmin.java    From hadoop with Apache License 2.0 5 votes vote down vote up
public int triggerBlockReport(String[] argv) throws IOException {
  List<String> args = new LinkedList<String>();
  for (int j = 1; j < argv.length; j++) {
    args.add(argv[j]);
  }
  boolean incremental = StringUtils.popOption("-incremental", args);
  String hostPort = StringUtils.popFirstNonOption(args);
  if (hostPort == null) {
    System.err.println("You must specify a host:port pair.");
    return 1;
  }
  if (!args.isEmpty()) {
    System.err.print("Can't understand arguments: " +
      Joiner.on(" ").join(args) + "\n");
    return 1;
  }
  ClientDatanodeProtocol dnProxy = getDataNodeProxy(hostPort);
  try {
    dnProxy.triggerBlockReport(
        new BlockReportOptions.Factory().
            setIncremental(incremental).
            build());
  } catch (IOException e) {
    System.err.println("triggerBlockReport error: " + e);
    return 1;
  }
  System.out.println("Triggering " +
      (incremental ? "an incremental " : "a full ") +
      "block report on " + hostPort + ".");
  return 0;
}
 
Example 2
Source File: XAttrCommands.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Override
protected void processOptions(LinkedList<String> args) throws IOException {
  name = StringUtils.popOptionWithArgument("-n", args);
  String en = StringUtils.popOptionWithArgument("-e", args);
  if (en != null) {
    try {
      encoding = enValueOfFunc.apply(StringUtils.toUpperCase(en));
    } catch (IllegalArgumentException e) {
      throw new IllegalArgumentException(
          "Invalid/unsupported encoding option specified: " + en);
    }
    Preconditions.checkArgument(encoding != null,
        "Invalid/unsupported encoding option specified: " + en);
  }

  boolean r = StringUtils.popOption("-R", args);
  setRecursive(r);
  dump = StringUtils.popOption("-d", args);

  if (!dump && name == null) {
    throw new HadoopIllegalArgumentException(
        "Must specify '-n name' or '-d' option.");
  }

  if (args.isEmpty()) {
    throw new HadoopIllegalArgumentException("<path> is missing.");
  }
  if (args.size() > 1) {
    throw new HadoopIllegalArgumentException("Too many arguments.");
  }
}
 
Example 3
Source File: DFSAdmin.java    From big-c with Apache License 2.0 5 votes vote down vote up
public int triggerBlockReport(String[] argv) throws IOException {
  List<String> args = new LinkedList<String>();
  for (int j = 1; j < argv.length; j++) {
    args.add(argv[j]);
  }
  boolean incremental = StringUtils.popOption("-incremental", args);
  String hostPort = StringUtils.popFirstNonOption(args);
  if (hostPort == null) {
    System.err.println("You must specify a host:port pair.");
    return 1;
  }
  if (!args.isEmpty()) {
    System.err.print("Can't understand arguments: " +
      Joiner.on(" ").join(args) + "\n");
    return 1;
  }
  ClientDatanodeProtocol dnProxy = getDataNodeProxy(hostPort);
  try {
    dnProxy.triggerBlockReport(
        new BlockReportOptions.Factory().
            setIncremental(incremental).
            build());
  } catch (IOException e) {
    System.err.println("triggerBlockReport error: " + e);
    return 1;
  }
  System.out.println("Triggering " +
      (incremental ? "an incremental " : "a full ") +
      "block report on " + hostPort + ".");
  return 0;
}
 
Example 4
Source File: XAttrCommands.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Override
protected void processOptions(LinkedList<String> args) throws IOException {
  name = StringUtils.popOptionWithArgument("-n", args);
  String en = StringUtils.popOptionWithArgument("-e", args);
  if (en != null) {
    try {
      encoding = enValueOfFunc.apply(StringUtils.toUpperCase(en));
    } catch (IllegalArgumentException e) {
      throw new IllegalArgumentException(
          "Invalid/unsupported encoding option specified: " + en);
    }
    Preconditions.checkArgument(encoding != null,
        "Invalid/unsupported encoding option specified: " + en);
  }

  boolean r = StringUtils.popOption("-R", args);
  setRecursive(r);
  dump = StringUtils.popOption("-d", args);

  if (!dump && name == null) {
    throw new HadoopIllegalArgumentException(
        "Must specify '-n name' or '-d' option.");
  }

  if (args.isEmpty()) {
    throw new HadoopIllegalArgumentException("<path> is missing.");
  }
  if (args.size() > 1) {
    throw new HadoopIllegalArgumentException("Too many arguments.");
  }
}
 
Example 5
Source File: CacheAdmin.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public int run(Configuration conf, List<String> args) throws IOException {
  CacheDirectiveInfo.Builder builder =
      new CacheDirectiveInfo.Builder();
  String pathFilter = StringUtils.popOptionWithArgument("-path", args);
  if (pathFilter != null) {
    builder.setPath(new Path(pathFilter));
  }
  String poolFilter = StringUtils.popOptionWithArgument("-pool", args);
  if (poolFilter != null) {
    builder.setPool(poolFilter);
  }
  boolean printStats = StringUtils.popOption("-stats", args);
  String idFilter = StringUtils.popOptionWithArgument("-id", args);
  if (idFilter != null) {
    builder.setId(Long.parseLong(idFilter));
  }
  if (!args.isEmpty()) {
    System.err.println("Can't understand argument: " + args.get(0));
    return 1;
  }
  TableListing.Builder tableBuilder = new TableListing.Builder().
      addField("ID", Justification.RIGHT).
      addField("POOL", Justification.LEFT).
      addField("REPL", Justification.RIGHT).
      addField("EXPIRY", Justification.LEFT).
      addField("PATH", Justification.LEFT);
  if (printStats) {
    tableBuilder.addField("BYTES_NEEDED", Justification.RIGHT).
                addField("BYTES_CACHED", Justification.RIGHT).
                addField("FILES_NEEDED", Justification.RIGHT).
                addField("FILES_CACHED", Justification.RIGHT);
  }
  TableListing tableListing = tableBuilder.build();
  try {
    DistributedFileSystem dfs = AdminHelper.getDFS(conf);
    RemoteIterator<CacheDirectiveEntry> iter =
        dfs.listCacheDirectives(builder.build());
    int numEntries = 0;
    while (iter.hasNext()) {
      CacheDirectiveEntry entry = iter.next();
      CacheDirectiveInfo directive = entry.getInfo();
      CacheDirectiveStats stats = entry.getStats();
      List<String> row = new LinkedList<String>();
      row.add("" + directive.getId());
      row.add(directive.getPool());
      row.add("" + directive.getReplication());
      String expiry;
      // This is effectively never, round for nice printing
      if (directive.getExpiration().getMillis() >
          Expiration.MAX_RELATIVE_EXPIRY_MS / 2) {
        expiry = "never";
      } else {
        expiry = directive.getExpiration().toString();
      }
      row.add(expiry);
      row.add(directive.getPath().toUri().getPath());
      if (printStats) {
        row.add("" + stats.getBytesNeeded());
        row.add("" + stats.getBytesCached());
        row.add("" + stats.getFilesNeeded());
        row.add("" + stats.getFilesCached());
      }
      tableListing.addRow(row.toArray(new String[row.size()]));
      numEntries++;
    }
    System.out.print(String.format("Found %d entr%s%n",
        numEntries, numEntries == 1 ? "y" : "ies"));
    if (numEntries > 0) {
      System.out.print(tableListing);
    }
  } catch (IOException e) {
    System.err.println(AdminHelper.prettifyException(e));
    return 2;
  }
  return 0;
}
 
Example 6
Source File: CacheAdmin.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public int run(Configuration conf, List<String> args) throws IOException {
  String name = StringUtils.popFirstNonOption(args);
  final boolean printStats = StringUtils.popOption("-stats", args);
  if (!args.isEmpty()) {
    System.err.print("Can't understand arguments: " +
      Joiner.on(" ").join(args) + "\n");
    System.err.println("Usage is " + getShortUsage());
    return 1;
  }
  DistributedFileSystem dfs = AdminHelper.getDFS(conf);
  TableListing.Builder builder = new TableListing.Builder().
      addField("NAME", Justification.LEFT).
      addField("OWNER", Justification.LEFT).
      addField("GROUP", Justification.LEFT).
      addField("MODE", Justification.LEFT).
      addField("LIMIT", Justification.RIGHT).
      addField("MAXTTL", Justification.RIGHT);
  if (printStats) {
    builder.
        addField("BYTES_NEEDED", Justification.RIGHT).
        addField("BYTES_CACHED", Justification.RIGHT).
        addField("BYTES_OVERLIMIT", Justification.RIGHT).
        addField("FILES_NEEDED", Justification.RIGHT).
        addField("FILES_CACHED", Justification.RIGHT);
  }
  TableListing listing = builder.build();
  int numResults = 0;
  try {
    RemoteIterator<CachePoolEntry> iter = dfs.listCachePools();
    while (iter.hasNext()) {
      CachePoolEntry entry = iter.next();
      CachePoolInfo info = entry.getInfo();
      LinkedList<String> row = new LinkedList<String>();
      if (name == null || info.getPoolName().equals(name)) {
        row.add(info.getPoolName());
        row.add(info.getOwnerName());
        row.add(info.getGroupName());
        row.add(info.getMode() != null ? info.getMode().toString() : null);
        Long limit = info.getLimit();
        String limitString;
        if (limit != null && limit.equals(CachePoolInfo.LIMIT_UNLIMITED)) {
          limitString = "unlimited";
        } else {
          limitString = "" + limit;
        }
        row.add(limitString);
        Long maxTtl = info.getMaxRelativeExpiryMs();
        String maxTtlString = null;

        if (maxTtl != null) {
          if (maxTtl == CachePoolInfo.RELATIVE_EXPIRY_NEVER) {
            maxTtlString  = "never";
          } else {
            maxTtlString = DFSUtil.durationToString(maxTtl);
          }
        }
        row.add(maxTtlString);
        if (printStats) {
          CachePoolStats stats = entry.getStats();
          row.add(Long.toString(stats.getBytesNeeded()));
          row.add(Long.toString(stats.getBytesCached()));
          row.add(Long.toString(stats.getBytesOverlimit()));
          row.add(Long.toString(stats.getFilesNeeded()));
          row.add(Long.toString(stats.getFilesCached()));
        }
        listing.addRow(row.toArray(new String[row.size()]));
        ++numResults;
        if (name != null) {
          break;
        }
      }
    }
  } catch (IOException e) {
    System.err.println(AdminHelper.prettifyException(e));
    return 2;
  }
  System.out.print(String.format("Found %d result%s.%n", numResults,
      (numResults == 1 ? "" : "s")));
  if (numResults > 0) { 
    System.out.print(listing);
  }
  // If list pools succeed, we return 0 (success exit code)
  return 0;
}
 
Example 7
Source File: TraceAdmin.java    From hadoop with Apache License 2.0 4 votes vote down vote up
@Override
public int run(String argv[]) throws Exception {
  LinkedList<String> args = new LinkedList<String>();
  for (String arg : argv) {
    args.add(arg);
  }
  if (StringUtils.popOption("-h", args) ||
      StringUtils.popOption("-help", args)) {
    usage();
    return 0;
  } else if (args.size() == 0) {
    usage();
    return 0;
  }
  String hostPort = StringUtils.popOptionWithArgument("-host", args);
  if (hostPort == null) {
    System.err.println("You must specify a host with -host.");
    return 1;
  }
  if (args.size() < 0) {
    System.err.println("You must specify an operation.");
    return 1;
  }
  RPC.setProtocolEngine(getConf(), TraceAdminProtocolPB.class,
      ProtobufRpcEngine.class);
  InetSocketAddress address = NetUtils.createSocketAddr(hostPort);
  UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
  Class<?> xface = TraceAdminProtocolPB.class;
  proxy = (TraceAdminProtocolPB)RPC.getProxy(xface,
      RPC.getProtocolVersion(xface), address,
      ugi, getConf(), NetUtils.getDefaultSocketFactory(getConf()), 0);
  remote = new TraceAdminProtocolTranslatorPB(proxy);
  try {
    if (args.get(0).equals("-list")) {
      return listSpanReceivers(args.subList(1, args.size()));
    } else if (args.get(0).equals("-add")) {
      return addSpanReceiver(args.subList(1, args.size()));
    } else if (args.get(0).equals("-remove")) {
      return removeSpanReceiver(args.subList(1, args.size()));
    } else {
      System.err.println("Unrecognized tracing command: " + args.get(0));
      System.err.println("Use -help for help.");
      return 1;
    }
  } finally {
    remote.close();
  }
}
 
Example 8
Source File: CacheAdmin.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public int run(Configuration conf, List<String> args) throws IOException {
  CacheDirectiveInfo.Builder builder =
      new CacheDirectiveInfo.Builder();
  String pathFilter = StringUtils.popOptionWithArgument("-path", args);
  if (pathFilter != null) {
    builder.setPath(new Path(pathFilter));
  }
  String poolFilter = StringUtils.popOptionWithArgument("-pool", args);
  if (poolFilter != null) {
    builder.setPool(poolFilter);
  }
  boolean printStats = StringUtils.popOption("-stats", args);
  String idFilter = StringUtils.popOptionWithArgument("-id", args);
  if (idFilter != null) {
    builder.setId(Long.parseLong(idFilter));
  }
  if (!args.isEmpty()) {
    System.err.println("Can't understand argument: " + args.get(0));
    return 1;
  }
  TableListing.Builder tableBuilder = new TableListing.Builder().
      addField("ID", Justification.RIGHT).
      addField("POOL", Justification.LEFT).
      addField("REPL", Justification.RIGHT).
      addField("EXPIRY", Justification.LEFT).
      addField("PATH", Justification.LEFT);
  if (printStats) {
    tableBuilder.addField("BYTES_NEEDED", Justification.RIGHT).
                addField("BYTES_CACHED", Justification.RIGHT).
                addField("FILES_NEEDED", Justification.RIGHT).
                addField("FILES_CACHED", Justification.RIGHT);
  }
  TableListing tableListing = tableBuilder.build();
  try {
    DistributedFileSystem dfs = AdminHelper.getDFS(conf);
    RemoteIterator<CacheDirectiveEntry> iter =
        dfs.listCacheDirectives(builder.build());
    int numEntries = 0;
    while (iter.hasNext()) {
      CacheDirectiveEntry entry = iter.next();
      CacheDirectiveInfo directive = entry.getInfo();
      CacheDirectiveStats stats = entry.getStats();
      List<String> row = new LinkedList<String>();
      row.add("" + directive.getId());
      row.add(directive.getPool());
      row.add("" + directive.getReplication());
      String expiry;
      // This is effectively never, round for nice printing
      if (directive.getExpiration().getMillis() >
          Expiration.MAX_RELATIVE_EXPIRY_MS / 2) {
        expiry = "never";
      } else {
        expiry = directive.getExpiration().toString();
      }
      row.add(expiry);
      row.add(directive.getPath().toUri().getPath());
      if (printStats) {
        row.add("" + stats.getBytesNeeded());
        row.add("" + stats.getBytesCached());
        row.add("" + stats.getFilesNeeded());
        row.add("" + stats.getFilesCached());
      }
      tableListing.addRow(row.toArray(new String[row.size()]));
      numEntries++;
    }
    System.out.print(String.format("Found %d entr%s%n",
        numEntries, numEntries == 1 ? "y" : "ies"));
    if (numEntries > 0) {
      System.out.print(tableListing);
    }
  } catch (IOException e) {
    System.err.println(AdminHelper.prettifyException(e));
    return 2;
  }
  return 0;
}
 
Example 9
Source File: CacheAdmin.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public int run(Configuration conf, List<String> args) throws IOException {
  String name = StringUtils.popFirstNonOption(args);
  final boolean printStats = StringUtils.popOption("-stats", args);
  if (!args.isEmpty()) {
    System.err.print("Can't understand arguments: " +
      Joiner.on(" ").join(args) + "\n");
    System.err.println("Usage is " + getShortUsage());
    return 1;
  }
  DistributedFileSystem dfs = AdminHelper.getDFS(conf);
  TableListing.Builder builder = new TableListing.Builder().
      addField("NAME", Justification.LEFT).
      addField("OWNER", Justification.LEFT).
      addField("GROUP", Justification.LEFT).
      addField("MODE", Justification.LEFT).
      addField("LIMIT", Justification.RIGHT).
      addField("MAXTTL", Justification.RIGHT);
  if (printStats) {
    builder.
        addField("BYTES_NEEDED", Justification.RIGHT).
        addField("BYTES_CACHED", Justification.RIGHT).
        addField("BYTES_OVERLIMIT", Justification.RIGHT).
        addField("FILES_NEEDED", Justification.RIGHT).
        addField("FILES_CACHED", Justification.RIGHT);
  }
  TableListing listing = builder.build();
  int numResults = 0;
  try {
    RemoteIterator<CachePoolEntry> iter = dfs.listCachePools();
    while (iter.hasNext()) {
      CachePoolEntry entry = iter.next();
      CachePoolInfo info = entry.getInfo();
      LinkedList<String> row = new LinkedList<String>();
      if (name == null || info.getPoolName().equals(name)) {
        row.add(info.getPoolName());
        row.add(info.getOwnerName());
        row.add(info.getGroupName());
        row.add(info.getMode() != null ? info.getMode().toString() : null);
        Long limit = info.getLimit();
        String limitString;
        if (limit != null && limit.equals(CachePoolInfo.LIMIT_UNLIMITED)) {
          limitString = "unlimited";
        } else {
          limitString = "" + limit;
        }
        row.add(limitString);
        Long maxTtl = info.getMaxRelativeExpiryMs();
        String maxTtlString = null;

        if (maxTtl != null) {
          if (maxTtl == CachePoolInfo.RELATIVE_EXPIRY_NEVER) {
            maxTtlString  = "never";
          } else {
            maxTtlString = DFSUtil.durationToString(maxTtl);
          }
        }
        row.add(maxTtlString);
        if (printStats) {
          CachePoolStats stats = entry.getStats();
          row.add(Long.toString(stats.getBytesNeeded()));
          row.add(Long.toString(stats.getBytesCached()));
          row.add(Long.toString(stats.getBytesOverlimit()));
          row.add(Long.toString(stats.getFilesNeeded()));
          row.add(Long.toString(stats.getFilesCached()));
        }
        listing.addRow(row.toArray(new String[row.size()]));
        ++numResults;
        if (name != null) {
          break;
        }
      }
    }
  } catch (IOException e) {
    System.err.println(AdminHelper.prettifyException(e));
    return 2;
  }
  System.out.print(String.format("Found %d result%s.%n", numResults,
      (numResults == 1 ? "" : "s")));
  if (numResults > 0) { 
    System.out.print(listing);
  }
  // If list pools succeed, we return 0 (success exit code)
  return 0;
}
 
Example 10
Source File: TraceAdmin.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Override
public int run(String argv[]) throws Exception {
  LinkedList<String> args = new LinkedList<String>();
  for (String arg : argv) {
    args.add(arg);
  }
  if (StringUtils.popOption("-h", args) ||
      StringUtils.popOption("-help", args)) {
    usage();
    return 0;
  } else if (args.size() == 0) {
    usage();
    return 0;
  }
  String hostPort = StringUtils.popOptionWithArgument("-host", args);
  if (hostPort == null) {
    System.err.println("You must specify a host with -host.");
    return 1;
  }
  if (args.size() < 0) {
    System.err.println("You must specify an operation.");
    return 1;
  }
  RPC.setProtocolEngine(getConf(), TraceAdminProtocolPB.class,
      ProtobufRpcEngine.class);
  InetSocketAddress address = NetUtils.createSocketAddr(hostPort);
  UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
  Class<?> xface = TraceAdminProtocolPB.class;
  proxy = (TraceAdminProtocolPB)RPC.getProxy(xface,
      RPC.getProtocolVersion(xface), address,
      ugi, getConf(), NetUtils.getDefaultSocketFactory(getConf()), 0);
  remote = new TraceAdminProtocolTranslatorPB(proxy);
  try {
    if (args.get(0).equals("-list")) {
      return listSpanReceivers(args.subList(1, args.size()));
    } else if (args.get(0).equals("-add")) {
      return addSpanReceiver(args.subList(1, args.size()));
    } else if (args.get(0).equals("-remove")) {
      return removeSpanReceiver(args.subList(1, args.size()));
    } else {
      System.err.println("Unrecognized tracing command: " + args.get(0));
      System.err.println("Use -help for help.");
      return 1;
    }
  } finally {
    remote.close();
  }
}