diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/util/CombinedHostsFileReader.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/util/CombinedHostsFileReader.java index 33acb91f837..9b23ad0e7f3 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/util/CombinedHostsFileReader.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/util/CombinedHostsFileReader.java @@ -22,7 +22,6 @@ import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.IOException; import java.io.Reader; - import java.util.Iterator; import java.util.Set; import java.util.HashSet; @@ -31,7 +30,7 @@ import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.map.ObjectMapper; - +import org.codehaus.jackson.map.ObjectReader; import org.apache.hadoop.hdfs.protocol.DatanodeAdminProperties; /** @@ -48,6 +47,10 @@ import org.apache.hadoop.hdfs.protocol.DatanodeAdminProperties; @InterfaceAudience.LimitedPrivate({"HDFS"}) @InterfaceStability.Unstable public final class CombinedHostsFileReader { + private static final ObjectReader READER = + new ObjectMapper().reader(DatanodeAdminProperties.class); + private static final JsonFactory JSON_FACTORY = new JsonFactory(); + private CombinedHostsFileReader() { } @@ -60,12 +63,10 @@ public final class CombinedHostsFileReader { public static Set readFile(final String hostsFile) throws IOException { HashSet allDNs = new HashSet<>(); - ObjectMapper mapper = new ObjectMapper(); try (Reader input = new InputStreamReader(new FileInputStream(hostsFile), "UTF-8")) { Iterator iterator = - mapper.readValues(new JsonFactory().createJsonParser(input), - DatanodeAdminProperties.class); + READER.readValues(JSON_FACTORY.createJsonParser(input)); while (iterator.hasNext()) { DatanodeAdminProperties properties = iterator.next(); allDNs.add(properties); diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/util/CombinedHostsFileWriter.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/util/CombinedHostsFileWriter.java index ea70be2eb70..7a50d63dfab 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/util/CombinedHostsFileWriter.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/util/CombinedHostsFileWriter.java @@ -45,6 +45,7 @@ import org.apache.hadoop.hdfs.protocol.DatanodeAdminProperties; @InterfaceAudience.LimitedPrivate({"HDFS"}) @InterfaceStability.Unstable public final class CombinedHostsFileWriter { + private static final ObjectMapper MAPPER = new ObjectMapper(); private CombinedHostsFileWriter() { } @@ -60,8 +61,7 @@ public final class CombinedHostsFileWriter { try (Writer output = new OutputStreamWriter(new FileOutputStream(hostsFile), "UTF-8")) { for (DatanodeAdminProperties datanodeAdminProperties: allDNs) { - ObjectMapper mapper = new ObjectMapper(); - configs.append(mapper.writeValueAsString(datanodeAdminProperties)); + configs.append(MAPPER.writeValueAsString(datanodeAdminProperties)); } output.write(configs.toString()); }