1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.wal;
19
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.PrintStream;
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.commons.cli.CommandLine;
31 import org.apache.commons.cli.CommandLineParser;
32 import org.apache.commons.cli.HelpFormatter;
33 import org.apache.commons.cli.Options;
34 import org.apache.commons.cli.ParseException;
35 import org.apache.commons.cli.PosixParser;
36 import org.apache.hadoop.hbase.classification.InterfaceAudience;
37 import org.apache.hadoop.hbase.classification.InterfaceStability;
38 import org.apache.hadoop.hbase.util.GsonUtil;
39 import org.apache.hadoop.conf.Configuration;
40 import org.apache.hadoop.fs.FileSystem;
41 import org.apache.hadoop.fs.Path;
42 import org.apache.hadoop.hbase.Cell;
43 import org.apache.hadoop.hbase.CellUtil;
44 import org.apache.hadoop.hbase.HBaseConfiguration;
45 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
46 import org.apache.hadoop.hbase.Tag;
47 import org.apache.hadoop.hbase.util.Bytes;
48 import org.apache.hadoop.hbase.util.FSUtils;
49
50 import org.apache.hadoop.hbase.regionserver.wal.ProtobufLogReader;
51
52 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
53 import org.apache.hbase.thirdparty.com.google.gson.Gson;
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
70 @InterfaceStability.Evolving
71 public class WALPrettyPrinter {
72 private boolean outputValues;
73 private boolean outputJSON;
74
75 private long sequence;
76 private String region;
77 private String row;
78
79 private boolean persistentOutput;
80 private boolean firstTxn;
81
82 private PrintStream out;
83
84 private static final Gson GSON = GsonUtil.createGson().create();
85
86 private long position;
87
88
89
90
91 public WALPrettyPrinter() {
92 outputValues = false;
93 outputJSON = false;
94 sequence = -1;
95 region = null;
96 row = null;
97 persistentOutput = false;
98 firstTxn = true;
99 out = System.out;
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127 public WALPrettyPrinter(boolean outputValues, boolean outputJSON,
128 long sequence, String region, String row, boolean
129 persistentOutput,
130 PrintStream out) {
131 this.outputValues = outputValues;
132 this.outputJSON = outputJSON;
133 this.sequence = sequence;
134 this.region = region;
135 this.row = row;
136 this.persistentOutput = persistentOutput;
137 if (persistentOutput) {
138 beginPersistentOutput();
139 }
140 this.out = out;
141 this.firstTxn = true;
142 }
143
144
145
146
147 public void enableValues() {
148 outputValues = true;
149 }
150
151
152
153
154 public void disableValues() {
155 outputValues = false;
156 }
157
158
159
160
161 public void enableJSON() {
162 outputJSON = true;
163 }
164
165
166
167
168 public void disableJSON() {
169 outputJSON = false;
170 }
171
172
173
174
175
176
177
178
179 public void setSequenceFilter(long sequence) {
180 this.sequence = sequence;
181 }
182
183
184
185
186
187
188
189
190 public void setRegionFilter(String region) {
191 this.region = region;
192 }
193
194
195
196
197
198
199
200
201 public void setRowFilter(String row) {
202 this.row = row;
203 }
204
205
206
207
208
209 public void beginPersistentOutput() {
210 if (persistentOutput)
211 return;
212 persistentOutput = true;
213 firstTxn = true;
214 if (outputJSON)
215 out.print("[");
216 }
217
218
219
220
221
222 public void endPersistentOutput() {
223 if (!persistentOutput)
224 return;
225 persistentOutput = false;
226 if (outputJSON)
227 out.print("]");
228 }
229
230
231
232
233
234
235
236
237
238
239
240
241
242 public void processFile(final Configuration conf, final Path p)
243 throws IOException {
244 FileSystem fs = p.getFileSystem(conf);
245 if (!fs.exists(p)) {
246 throw new FileNotFoundException(p.toString());
247 }
248 if (!fs.isFile(p)) {
249 throw new IOException(p + " is not a file");
250 }
251
252 WAL.Reader log = WALFactory.createReader(fs, p, conf);
253
254 if (log instanceof ProtobufLogReader) {
255 List<String> writerClsNames = ((ProtobufLogReader) log).getWriterClsNames();
256 if (writerClsNames != null && writerClsNames.size() > 0) {
257 out.print("Writer Classes: ");
258 for (int i = 0; i < writerClsNames.size(); i++) {
259 out.print(writerClsNames.get(i));
260 if (i != writerClsNames.size() - 1) {
261 out.print(" ");
262 }
263 }
264 out.println();
265 }
266
267 String cellCodecClsName = ((ProtobufLogReader) log).getCodecClsName();
268 if (cellCodecClsName != null) {
269 out.println("Cell Codec Class: " + cellCodecClsName);
270 }
271 }
272
273 if (outputJSON && !persistentOutput) {
274 out.print("[");
275 firstTxn = true;
276 }
277
278 if (position > 0) {
279 log.seek(position);
280 }
281
282 try {
283 WAL.Entry entry;
284 while ((entry = log.next()) != null) {
285 WALKey key = entry.getKey();
286 WALEdit edit = entry.getEdit();
287
288 Map<String, Object> txn = key.toStringMap();
289 long writeTime = key.getWriteTime();
290
291 if (sequence >= 0 && ((Long) txn.get("sequence")) != sequence)
292 continue;
293 if (region != null && !((String) txn.get("region")).equals(region))
294 continue;
295
296 List<Map> actions = new ArrayList<Map>();
297 for (Cell cell : edit.getCells()) {
298
299 Map<String, Object> op = new HashMap<String, Object>(toStringMap(cell));
300 if (outputValues) op.put("value", Bytes.toStringBinary(cell.getValue()));
301
302 if (row == null || ((String) op.get("row")).equals(row)) {
303 actions.add(op);
304 }
305 op.put("total_size_sum", CellUtil.estimatedHeapSizeOf(cell));
306
307 }
308 if (actions.size() == 0)
309 continue;
310 txn.put("actions", actions);
311 if (outputJSON) {
312
313 if (firstTxn)
314 firstTxn = false;
315 else
316 out.print(",");
317
318 out.print(GSON.toJson(txn));
319 } else {
320
321 out.println("Sequence=" + txn.get("sequence") + " "
322 + ", region=" + txn.get("region") + " at write timestamp=" + new Date(writeTime));
323 for (int i = 0; i < actions.size(); i++) {
324 Map op = actions.get(i);
325 out.println("row=" + op.get("row") +
326 ", column=" + op.get("family") + ":" + op.get("qualifier"));
327 if (op.get("tag") != null) {
328 out.println(" tag: " + op.get("tag"));
329 }
330 if (outputValues) out.println(" value: " + op.get("value"));
331 out.println("cell total size sum: " + op.get("total_size_sum"));
332 }
333 }
334 out.println("edit heap size: " + entry.getEdit().heapSize());
335 out.println("position: " + log.getPosition());
336 }
337 } finally {
338 log.close();
339 }
340 if (outputJSON && !persistentOutput) {
341 out.print("]");
342 }
343 }
344
345 private static Map<String, Object> toStringMap(Cell cell) {
346 Map<String, Object> stringMap = new HashMap<String, Object>();
347 stringMap.put("row",
348 Bytes.toStringBinary(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
349 stringMap.put("family", Bytes.toStringBinary(cell.getFamilyArray(), cell.getFamilyOffset(),
350 cell.getFamilyLength()));
351 stringMap.put("qualifier",
352 Bytes.toStringBinary(cell.getQualifierArray(), cell.getQualifierOffset(),
353 cell.getQualifierLength()));
354 stringMap.put("timestamp", cell.getTimestamp());
355 stringMap.put("vlen", cell.getValueLength());
356 if (cell.getTagsLength() > 0) {
357 List<String> tagsString = new ArrayList<String>();
358 Iterator<Tag> tagsIterator = CellUtil.tagsIterator(cell.getTagsArray(), cell.getTagsOffset(),
359 cell.getTagsLength());
360 while (tagsIterator.hasNext()) {
361 Tag tag = tagsIterator.next();
362 tagsString.add((tag.getType()) + ":"
363 + Bytes.toStringBinary(tag.getBuffer(), tag.getTagOffset(), tag.getTagLength()));
364 }
365 stringMap.put("tag", tagsString);
366 }
367 return stringMap;
368 }
369
370 public static void main(String[] args) throws IOException {
371 run(args);
372 }
373
374
375
376
377
378
379
380
381
382
383 public static void run(String[] args) throws IOException {
384
385 Options options = new Options();
386 options.addOption("h", "help", false, "Output help message");
387 options.addOption("j", "json", false, "Output JSON");
388 options.addOption("p", "printvals", false, "Print values");
389 options.addOption("r", "region", true,
390 "Region to filter by. Pass encoded region name; e.g. '9192caead6a5a20acb4454ffbc79fa14'");
391 options.addOption("s", "sequence", true,
392 "Sequence to filter by. Pass sequence number.");
393 options.addOption("w", "row", true, "Row to filter by. Pass row name.");
394 options.addOption("g", "goto", true, "Position to seek to in the file");
395
396 WALPrettyPrinter printer = new WALPrettyPrinter();
397 CommandLineParser parser = new PosixParser();
398 List<?> files = null;
399 try {
400 CommandLine cmd = parser.parse(options, args);
401 files = cmd.getArgList();
402 if (files.size() == 0 || cmd.hasOption("h")) {
403 HelpFormatter formatter = new HelpFormatter();
404 formatter.printHelp("WAL <filename...>", options, true);
405 System.exit(-1);
406 }
407
408 if (cmd.hasOption("p"))
409 printer.enableValues();
410 if (cmd.hasOption("j"))
411 printer.enableJSON();
412 if (cmd.hasOption("r"))
413 printer.setRegionFilter(cmd.getOptionValue("r"));
414 if (cmd.hasOption("s"))
415 printer.setSequenceFilter(Long.parseLong(cmd.getOptionValue("s")));
416 if (cmd.hasOption("w"))
417 printer.setRowFilter(cmd.getOptionValue("w"));
418 if (cmd.hasOption("g")) {
419 printer.setPosition(Long.parseLong(cmd.getOptionValue("g")));
420 }
421 } catch (ParseException e) {
422 e.printStackTrace();
423 HelpFormatter formatter = new HelpFormatter();
424 formatter.printHelp("HFile filename(s) ", options, true);
425 System.exit(-1);
426 }
427
428 Configuration conf = HBaseConfiguration.create();
429 FSUtils.setFsDefault(conf, FSUtils.getRootDir(conf));
430
431
432 printer.beginPersistentOutput();
433 for (Object f : files) {
434 Path file = new Path((String) f);
435 FileSystem fs = file.getFileSystem(conf);
436 if (!fs.exists(file)) {
437 System.err.println("ERROR, file doesnt exist: " + file);
438 return;
439 }
440 printer.processFile(conf, file);
441 }
442 printer.endPersistentOutput();
443 }
444
445 public void setPosition(long position) {
446 this.position = position;
447 }
448 }