/** * Dump a Log File (Map or Sequence) to stdout. Will read from HDFS or local file system. * * @param args - first argument is the file to print */ public static void main(String[] args) throws IOException { Opts opts = new Opts(); opts.parseArgs(LogReader.class.getName(), args); VolumeManager fs = VolumeManagerImpl.get(); Matcher rowMatcher = null; KeyExtent ke = null; Text row = null; if (opts.files.isEmpty()) { new JCommander(opts).usage(); return; } if (opts.row != null) row = new Text(opts.row); if (opts.extent != null) { String sa[] = opts.extent.split(";"); ke = new KeyExtent(new Text(sa[0]), new Text(sa[1]), new Text(sa[2])); } if (opts.regexp != null) { Pattern pattern = Pattern.compile(opts.regexp); rowMatcher = pattern.matcher(""); } Set<Integer> tabletIds = new HashSet<Integer>(); for (String file : opts.files) { Path path = new Path(file); LogFileKey key = new LogFileKey(); LogFileValue value = new LogFileValue(); if (fs.isFile(path)) { // read log entries from a simple hdfs file DFSLoggerInputStreams streams = DfsLogger.readHeaderAndReturnStream(fs, path, SiteConfiguration.getInstance()); DataInputStream input = streams.getDecryptingInputStream(); try { while (true) { try { key.readFields(input); value.readFields(input); } catch (EOFException ex) { break; } printLogEvent(key, value, row, rowMatcher, ke, tabletIds, opts.maxMutations); } } finally { input.close(); } } else { // read the log entries sorted in a map file MultiReader input = new MultiReader(fs, path); while (input.next(key, value)) { printLogEvent(key, value, row, rowMatcher, ke, tabletIds, opts.maxMutations); } } } }
public static void printLogEvent( LogFileKey key, LogFileValue value, Text row, Matcher rowMatcher, KeyExtent ke, Set<Integer> tabletIds, int maxMutations) { if (ke != null) { if (key.event == LogEvents.DEFINE_TABLET) { if (key.tablet.equals(ke)) { tabletIds.add(key.tid); } else { return; } } else if (!tabletIds.contains(key.tid)) { return; } } if (row != null || rowMatcher != null) { if (key.event == LogEvents.MUTATION || key.event == LogEvents.MANY_MUTATIONS) { boolean found = false; for (Mutation m : value.mutations) { if (row != null && new Text(m.getRow()).equals(row)) { found = true; break; } if (rowMatcher != null) { rowMatcher.reset(new String(m.getRow(), StandardCharsets.UTF_8)); if (rowMatcher.matches()) { found = true; break; } } } if (!found) return; } else { return; } } System.out.println(key); System.out.println(LogFileValue.format(value, maxMutations)); }