Sfoglia il codice sorgente

HADOOP-13235. Use Date and Time API in KafkaSink.

Akira Ajisaka 8 anni fa
parent
commit
8d202f1258

+ 15 - 8
hadoop-tools/hadoop-kafka/src/main/java/org/apache/hadoop/metrics2/sink/KafkaSink.java

@@ -38,8 +38,10 @@ import java.io.Closeable;
 import java.io.IOException;
 import java.io.IOException;
 import java.net.InetAddress;
 import java.net.InetAddress;
 import java.nio.charset.Charset;
 import java.nio.charset.Charset;
-import java.text.SimpleDateFormat;
-import java.util.Date;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
 import java.util.Properties;
 import java.util.Properties;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.Future;
@@ -62,6 +64,12 @@ public class KafkaSink implements MetricsSink, Closeable {
   private String topic = null;
   private String topic = null;
   private Producer<Integer, byte[]> producer = null;
   private Producer<Integer, byte[]> producer = null;
 
 
+  private final DateTimeFormatter dateFormat =
+      DateTimeFormatter.ofPattern("yyyy-MM-dd");
+  private final DateTimeFormatter timeFormat =
+      DateTimeFormatter.ofPattern("HH:mm:ss");
+  private final ZoneId zoneId = ZoneId.systemDefault();
+
   public void setProducer(Producer<Integer, byte[]> p) {
   public void setProducer(Producer<Integer, byte[]> p) {
     this.producer = p;
     this.producer = p;
   }
   }
@@ -121,12 +129,11 @@ public class KafkaSink implements MetricsSink, Closeable {
     // Create the json object.
     // Create the json object.
     StringBuilder jsonLines = new StringBuilder();
     StringBuilder jsonLines = new StringBuilder();
 
 
-    Long timestamp = record.timestamp();
-    Date currDate = new Date(timestamp);
-    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
-    SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
-    String date = dateFormat.format(currDate);
-    String time = timeFormat.format(currDate);
+    long timestamp = record.timestamp();
+    Instant instant = Instant.ofEpochMilli(timestamp);
+    LocalDateTime ldt = LocalDateTime.ofInstant(instant, zoneId);
+    String date = ldt.format(dateFormat);
+    String time = ldt.format(timeFormat);
 
 
     // Collect datapoints and populate the json object.
     // Collect datapoints and populate the json object.
     jsonLines.append("{\"hostname\": \"" + hostname);
     jsonLines.append("{\"hostname\": \"" + hostname);