Tool to concatenate avro files with the same schema and non-reserved metatdata.
| 42 | * metatdata. |
| 43 | */ |
| 44 | public class ConcatTool implements Tool { |
| 45 | /** |
| 46 | * @return 0 for success, 1 if the schemas of the input files differ, 2 if the |
| 47 | * non-reserved input metadata differs, 3 if the input files are encoded |
| 48 | * with more than one codec. |
| 49 | */ |
| 50 | @Override |
| 51 | public int run(InputStream in, PrintStream out, PrintStream err, List<String> args) throws Exception { |
| 52 | |
| 53 | if (args.isEmpty()) { |
| 54 | printHelp(out); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | OutputStream output = out; |
| 59 | if (args.size() > 1) { |
| 60 | output = Util.fileOrStdout(args.get(args.size() - 1), out); |
| 61 | args = args.subList(0, args.size() - 1); |
| 62 | } |
| 63 | |
| 64 | DataFileWriter<GenericRecord> writer = new DataFileWriter<>(new GenericDatumWriter<>()); |
| 65 | Schema schema = null; |
| 66 | Map<String, byte[]> metadata = new TreeMap<>(); |
| 67 | String inputCodec = null; |
| 68 | |
| 69 | for (String inFile : expandsInputFiles(args)) { |
| 70 | InputStream input = Util.fileOrStdin(inFile, in); |
| 71 | DataFileStream<GenericRecord> reader = new DataFileStream<>(input, new GenericDatumReader<>()); |
| 72 | |
| 73 | if (schema == null) { |
| 74 | // this is the first file - set up the writer, and store the |
| 75 | // Schema & metadata we'll use. |
| 76 | schema = reader.getSchema(); |
| 77 | for (String key : reader.getMetaKeys()) { |
| 78 | if (!DataFileWriter.isReservedMeta(key)) { |
| 79 | byte[] metadatum = reader.getMeta(key); |
| 80 | metadata.put(key, metadatum); |
| 81 | writer.setMeta(key, metadatum); |
| 82 | } |
| 83 | } |
| 84 | inputCodec = reader.getMetaString(DataFileConstants.CODEC); |
| 85 | if (inputCodec == null) { |
| 86 | inputCodec = DataFileConstants.NULL_CODEC; |
| 87 | } |
| 88 | writer.setCodec(CodecFactory.fromString(inputCodec)); |
| 89 | writer.create(schema, output); |
| 90 | } else { |
| 91 | // check that we're appending to the same schema & metadata. |
| 92 | if (!schema.equals(reader.getSchema())) { |
| 93 | err.println("input files have different schemas"); |
| 94 | reader.close(); |
| 95 | return 1; |
| 96 | } |
| 97 | for (String key : reader.getMetaKeys()) { |
| 98 | if (!DataFileWriter.isReservedMeta(key)) { |
| 99 | byte[] metadatum = reader.getMeta(key); |
| 100 | byte[] writersMetadatum = metadata.get(key); |
| 101 | if (!Arrays.equals(metadatum, writersMetadatum)) { |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…