diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/common/helpers/ContainerInfo.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/common/helpers/ContainerInfo.java index 0bd4c26d42..10fd96c175 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/common/helpers/ContainerInfo.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/container/common/helpers/ContainerInfo.java @@ -18,6 +18,10 @@ package org.apache.hadoop.hdds.scm.container.common.helpers; +import com.fasterxml.jackson.annotation.JsonAutoDetect; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectWriter; import com.google.common.base.Preconditions; import org.apache.commons.lang3.builder.EqualsBuilder; import org.apache.commons.lang3.builder.HashCodeBuilder; @@ -25,6 +29,7 @@ import org.apache.hadoop.hdds.protocol.proto.HddsProtos; import org.apache.hadoop.util.Time; +import java.io.IOException; import java.util.Comparator; /** @@ -32,6 +37,17 @@ */ public class ContainerInfo implements Comparator, Comparable { + + private static final ObjectWriter WRITER; + + static { + ObjectMapper mapper = new ObjectMapper(); + mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); + mapper + .setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.NONE); + WRITER = mapper.writer(); + } + private HddsProtos.LifeCycleState state; private Pipeline pipeline; // Bytes allocated by SCM for clients. @@ -254,6 +270,16 @@ public int compareTo(ContainerInfo o) { return this.compare(this, o); } + /** + * Returns a JSON string of this object. + * + * @return String - json string + * @throws IOException + */ + public String toJsonString() throws IOException { + return WRITER.writeValueAsString(this); + } + /** * Builder class for ContainerInfo. */ diff --git a/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/CreateContainerHandler.java b/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/CreateContainerHandler.java index c0ff1f7a6d..278ee30842 100644 --- a/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/CreateContainerHandler.java +++ b/hadoop-hdds/tools/src/main/java/org/apache/hadoop/hdds/scm/cli/container/CreateContainerHandler.java @@ -44,6 +44,10 @@ public CreateContainerHandler(ScmClient scmClient) { @Override public void execute(CommandLine cmd) throws IOException { + if (cmd.hasOption(HELP_OP)) { + displayHelp(); + } + if (!cmd.hasOption(CONTAINER_CREATE)) { throw new IOException("Expecting container create"); } @@ -57,7 +61,7 @@ public void execute(CommandLine cmd) throws IOException { public void displayHelp() { Options options = new Options(); HelpFormatter helpFormatter = new HelpFormatter(); - helpFormatter.printHelp(CMD_WIDTH, "hdfs scm -container -create