You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by ns...@apache.org on 2011/10/11 19:43:02 UTC

svn commit: r1181937 - in /hbase/branches/0.89/src/main/java/org/apache/hadoop/hbase: io/hfile/HFilePrettyPrinter.java regionserver/wal/HLogPrettyPrinter.java

Author: nspiegelberg
Date: Tue Oct 11 17:43:02 2011
New Revision: 1181937

URL: http://svn.apache.org/viewvc?rev=1181937&view=rev
Log:
Replace jettison JSON encoder use with jackson for consistency with open source.

Summary: The open source trunk is using the jackson JSON encoder
(http://jackson.codehaus.org/) internally. This appears to be better maintained
and faster than jettison. Both for this reason and for consistency with open
source and my slow query log and show processlist patches, I replaced our few
uses of jettison with jackson.
Test Plan: Compiled and ran both the HLogPrettyPrinter and the
HFilePrettyPrinter in JSON mode, validating the JSON.
Reviewed By: nspiegelberg
Reviewers: nspiegelberg
CC: hbase@lists, nspiegelberg
Differential Revision: 306195

Modified:
    hbase/branches/0.89/src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePrettyPrinter.java
    hbase/branches/0.89/src/main/java/org/apache/hadoop/hbase/regionserver/wal/HLogPrettyPrinter.java

Modified: hbase/branches/0.89/src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePrettyPrinter.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.89/src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePrettyPrinter.java?rev=1181937&r1=1181936&r2=1181937&view=diff
==============================================================================
--- hbase/branches/0.89/src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePrettyPrinter.java (original)
+++ hbase/branches/0.89/src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePrettyPrinter.java Tue Oct 11 17:43:02 2011
@@ -22,6 +22,7 @@ package org.apache.hadoop.hbase.io.hfile
 import java.io.DataInput;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -48,9 +49,7 @@ import org.apache.hadoop.hbase.util.Byte
 import org.apache.hadoop.hbase.util.Bytes;
 import org.apache.hadoop.hbase.util.FSUtils;
 import org.apache.hadoop.hbase.util.Writables;
-import org.codehaus.jettison.json.JSONArray;
-import org.codehaus.jettison.json.JSONException;
-import org.codehaus.jettison.json.JSONObject;
+import org.codehaus.jackson.map.ObjectMapper;
 
 /**
  * Implements pretty-printing functionality for {@link HFile}s.
@@ -75,6 +74,9 @@ public class HFilePrettyPrinter {
   private List<Path> files = new ArrayList<Path>();
   private int count;
 
+  // JSON encoder
+  private ObjectMapper mapper = new ObjectMapper();
+
   private static final String FOUR_SPACES = "    ";
 
   public HFilePrettyPrinter() {
@@ -256,24 +258,19 @@ public class HFilePrettyPrinter {
       // dump key value
       if (printKey) {
         if (outputJSON) {
-          JSONObject jsonKv = new JSONObject();
+          Map<String, Object> jsonKv = new HashMap<String, Object>();
           // dump key value
-          try {
-            if (printKey) {
-              jsonKv.put("key", kv);
-              if (printValue) {
-                jsonKv.put("value", Bytes.toStringBinary(kv.getValue()));
-              }
-            }
-          } catch (JSONException e) {
-            e.printStackTrace();
+          jsonKv.put("key", kv.toStringMap());
+          if (printValue) {
+            jsonKv.put("value", Bytes.toStringBinary(kv.getValue()));
           }
           if (first) {
             first = false;
           } else {
             System.out.print(",");
           }
-          System.out.print(jsonKv);
+          // encode and print JSON
+          System.out.print(mapper.writeValueAsString(jsonKv));
         } else {
           // normal, "pretty string" output
           System.out.print("K: " + kv);

Modified: hbase/branches/0.89/src/main/java/org/apache/hadoop/hbase/regionserver/wal/HLogPrettyPrinter.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.89/src/main/java/org/apache/hadoop/hbase/regionserver/wal/HLogPrettyPrinter.java?rev=1181937&r1=1181936&r2=1181937&view=diff
==============================================================================
--- hbase/branches/0.89/src/main/java/org/apache/hadoop/hbase/regionserver/wal/HLogPrettyPrinter.java (original)
+++ hbase/branches/0.89/src/main/java/org/apache/hadoop/hbase/regionserver/wal/HLogPrettyPrinter.java Tue Oct 11 17:43:02 2011
@@ -1,10 +1,30 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.apache.hadoop.hbase.regionserver.wal;
 
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.PrintStream;
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.CommandLineParser;
@@ -19,9 +39,7 @@ import org.apache.hadoop.hbase.HBaseConf
 import org.apache.hadoop.hbase.KeyValue;
 import org.apache.hadoop.hbase.regionserver.wal.HLog.Reader;
 import org.apache.hadoop.hbase.util.Bytes;
-import org.codehaus.jettison.json.JSONArray;
-import org.codehaus.jettison.json.JSONException;
-import org.codehaus.jettison.json.JSONObject;
+import org.codehaus.jackson.map.ObjectMapper;
 
 /**
  * HLogPrettyPrinter prints the contents of a given HLog with a variety of
@@ -48,6 +66,8 @@ public class HLogPrettyPrinter {
   private boolean firstTxn;
   // useful for programatic capture of JSON output
   private PrintStream out;
+  // for JSON encoding
+  private ObjectMapper mapper;
 
   /**
    * Basic constructor that simply initializes values to reasonable defaults.
@@ -61,6 +81,7 @@ public class HLogPrettyPrinter {
     persistentOutput = false;
     firstTxn = true;
     out = System.out;
+    mapper = new ObjectMapper();
   }
 
   /**
@@ -222,23 +243,25 @@ public class HLogPrettyPrinter {
         HLogKey key = entry.getKey();
         WALEdit edit = entry.getEdit();
         // begin building a transaction structure
-        JSONObject txn = new JSONObject(key.toStringMap());
+        Map<String, Object> txn = key.toStringMap();
         // check output filters
         if (sequence >= 0 && ((Long) txn.get("sequence")) != sequence)
           continue;
         if (region != null && !((String) txn.get("region")).equals(region))
           continue;
         // initialize list into which we will store atomic actions
-        JSONArray actions = new JSONArray();
+        List<Map> actions = new ArrayList<Map>();
         for (KeyValue kv : edit.getKeyValues()) {
           // add atomic operation to txn
-          JSONObject op = new JSONObject(kv.toStringMap());
+          Map<String, Object> op =
+            new HashMap<String, Object>(kv.toStringMap());
           if (outputValues)
             op.put("value", Bytes.toStringBinary(kv.getValue()));
+          // check row output filter
           if (row == null || ((String) op.get("row")).equals(row))
-            actions.put(op);
+            actions.add(op);
         }
-        if (actions.length() == 0)
+        if (actions.size() == 0)
           continue;
         txn.put("actions", actions);
         if (outputJSON) {
@@ -247,27 +270,26 @@ public class HLogPrettyPrinter {
             firstTxn = false;
           else
             out.print(",");
-          out.print(txn);
+          // encode and print JSON
+          out.print(mapper.writeValueAsString(txn));
         } else {
           // Pretty output, complete with indentation by atomic action
-          out.println("Sequence " + txn.getLong("sequence") + " "
-              + "from region " + txn.getString("region") + " " + "in table "
-              + txn.getString("table"));
-          for (int i = 0; i < actions.length(); i++) {
-            JSONObject op = actions.getJSONObject(i);
-            out.println("  Put action:");
-            out.println("    row: " + op.getString("row"));
-            out.println("    column: " + op.getString("family") + ":"
-                + op.getString("qualifier"));
+          out.println("Sequence " + txn.get("sequence") + " "
+              + "from region " + txn.get("region") + " " + "in table "
+              + txn.get("table"));
+          for (int i = 0; i < actions.size(); i++) {
+            Map op = actions.get(i);
+            out.println("  Action:");
+            out.println("    row: " + op.get("row"));
+            out.println("    column: " + op.get("family") + ":"
+                + op.get("qualifier"));
             out.println("    at time: "
-                + (new Date(op.getLong("timestamp"))));
+                + (new Date((Long) op.get("timestamp"))));
             if (outputValues)
               out.println("    value: " + op.get("value"));
           }
         }
       }
-    } catch (JSONException e) {
-      e.printStackTrace();
     } finally {
       log.close();
     }