Reads an avro data file into a plain text file.
| 34 | |
| 35 | /** Reads an avro data file into a plain text file. */ |
| 36 | public class ToTextTool implements Tool { |
| 37 | private static final String TEXT_FILE_SCHEMA = "\"bytes\""; |
| 38 | private static final byte[] LINE_SEPARATOR = System.getProperty("line.separator").getBytes(StandardCharsets.UTF_8); |
| 39 | |
| 40 | @Override |
| 41 | public String getName() { |
| 42 | return "totext"; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public String getShortDescription() { |
| 47 | return "Converts an Avro data file to a text file."; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public int run(InputStream stdin, PrintStream out, PrintStream err, List<String> args) throws Exception { |
| 52 | |
| 53 | OptionParser p = new OptionParser(); |
| 54 | OptionSet opts = p.parse(args.toArray(new String[0])); |
| 55 | if (opts.nonOptionArguments().size() != 2) { |
| 56 | err.println("Expected 2 args: from_file to_file (filenames or '-' for stdin/stdout"); |
| 57 | p.printHelpOn(err); |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | BufferedInputStream inStream = Util.fileOrStdin(args.get(0), stdin); |
| 62 | BufferedOutputStream outStream = Util.fileOrStdout(args.get(1), out); |
| 63 | |
| 64 | GenericDatumReader<Object> reader = new GenericDatumReader<>(); |
| 65 | DataFileStream<Object> fileReader = new DataFileStream<>(inStream, reader); |
| 66 | |
| 67 | if (!fileReader.getSchema().equals(new Schema.Parser().parse(TEXT_FILE_SCHEMA))) { |
| 68 | err.println("Avro file is not generic text schema"); |
| 69 | p.printHelpOn(err); |
| 70 | fileReader.close(); |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | while (fileReader.hasNext()) { |
| 75 | ByteBuffer outBuff = (ByteBuffer) fileReader.next(); |
| 76 | outStream.write(outBuff.array()); |
| 77 | outStream.write(LINE_SEPARATOR); |
| 78 | } |
| 79 | fileReader.close(); |
| 80 | Util.close(inStream); |
| 81 | Util.close(outStream); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…