|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.arrow.tools; |
| 20 | + |
| 21 | +import java.io.File; |
| 22 | +import java.io.FileInputStream; |
| 23 | +import java.io.FileOutputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.PrintStream; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import org.apache.arrow.memory.BufferAllocator; |
| 29 | +import org.apache.arrow.memory.RootAllocator; |
| 30 | +import org.apache.arrow.vector.VectorLoader; |
| 31 | +import org.apache.arrow.vector.VectorSchemaRoot; |
| 32 | +import org.apache.arrow.vector.VectorUnloader; |
| 33 | +import org.apache.arrow.vector.file.ArrowBlock; |
| 34 | +import org.apache.arrow.vector.file.ArrowFooter; |
| 35 | +import org.apache.arrow.vector.file.ArrowReader; |
| 36 | +import org.apache.arrow.vector.file.ArrowWriter; |
| 37 | +import org.apache.arrow.vector.schema.ArrowRecordBatch; |
| 38 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 39 | +import org.apache.commons.cli.CommandLine; |
| 40 | +import org.apache.commons.cli.CommandLineParser; |
| 41 | +import org.apache.commons.cli.Options; |
| 42 | +import org.apache.commons.cli.ParseException; |
| 43 | +import org.apache.commons.cli.PosixParser; |
| 44 | +import org.slf4j.Logger; |
| 45 | +import org.slf4j.LoggerFactory; |
| 46 | + |
| 47 | +public class FileRoundtrip { |
| 48 | + private static final Logger LOGGER = LoggerFactory.getLogger(FileRoundtrip.class); |
| 49 | + |
| 50 | + public static void main(String[] args) { |
| 51 | + System.exit(new FileRoundtrip(System.out, System.err).run(args)); |
| 52 | + } |
| 53 | + |
| 54 | + private final Options options; |
| 55 | + private final PrintStream out; |
| 56 | + private final PrintStream err; |
| 57 | + |
| 58 | + FileRoundtrip(PrintStream out, PrintStream err) { |
| 59 | + this.out = out; |
| 60 | + this.err = err; |
| 61 | + this.options = new Options(); |
| 62 | + this.options.addOption("i", "in", true, "input file"); |
| 63 | + this.options.addOption("o", "out", true, "output file"); |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + private File validateFile(String type, String fileName) { |
| 68 | + if (fileName == null) { |
| 69 | + throw new IllegalArgumentException("missing " + type + " file parameter"); |
| 70 | + } |
| 71 | + File f = new File(fileName); |
| 72 | + if (!f.exists() || f.isDirectory()) { |
| 73 | + throw new IllegalArgumentException(type + " file not found: " + f.getAbsolutePath()); |
| 74 | + } |
| 75 | + return f; |
| 76 | + } |
| 77 | + |
| 78 | + int run(String[] args) { |
| 79 | + try { |
| 80 | + CommandLineParser parser = new PosixParser(); |
| 81 | + CommandLine cmd = parser.parse(options, args, false); |
| 82 | + |
| 83 | + String inFileName = cmd.getOptionValue("in"); |
| 84 | + String outFileName = cmd.getOptionValue("out"); |
| 85 | + |
| 86 | + File inFile = validateFile("input", inFileName); |
| 87 | + File outFile = validateFile("output", outFileName); |
| 88 | + BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); // TODO: close |
| 89 | + try( |
| 90 | + FileInputStream fileInputStream = new FileInputStream(inFile); |
| 91 | + ArrowReader arrowReader = new ArrowReader(fileInputStream.getChannel(), allocator);) { |
| 92 | + |
| 93 | + ArrowFooter footer = arrowReader.readFooter(); |
| 94 | + Schema schema = footer.getSchema(); |
| 95 | + LOGGER.debug("Input file size: " + inFile.length()); |
| 96 | + LOGGER.debug("Found schema: " + schema); |
| 97 | + |
| 98 | + try ( |
| 99 | + FileOutputStream fileOutputStream = new FileOutputStream(outFile); |
| 100 | + ArrowWriter arrowWriter = new ArrowWriter(fileOutputStream.getChannel(), schema); |
| 101 | + ) { |
| 102 | + |
| 103 | + // initialize vectors |
| 104 | + |
| 105 | + List<ArrowBlock> recordBatches = footer.getRecordBatches(); |
| 106 | + for (ArrowBlock rbBlock : recordBatches) { |
| 107 | + try (ArrowRecordBatch inRecordBatch = arrowReader.readRecordBatch(rbBlock); |
| 108 | + VectorSchemaRoot root = new VectorSchemaRoot(schema, allocator);) { |
| 109 | + |
| 110 | + VectorLoader vectorLoader = new VectorLoader(root); |
| 111 | + vectorLoader.load(inRecordBatch); |
| 112 | + |
| 113 | + VectorUnloader vectorUnloader = new VectorUnloader(root); |
| 114 | + ArrowRecordBatch recordBatch = vectorUnloader.getRecordBatch(); |
| 115 | + arrowWriter.writeRecordBatch(recordBatch); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + LOGGER.debug("Output file size: " + outFile.length()); |
| 120 | + } |
| 121 | + } catch (ParseException e) { |
| 122 | + return fatalError("Invalid parameters", e); |
| 123 | + } catch (IOException e) { |
| 124 | + return fatalError("Error accessing files", e); |
| 125 | + } |
| 126 | + return 0; |
| 127 | + } |
| 128 | + |
| 129 | + private int fatalError(String message, Throwable e) { |
| 130 | + err.println(message); |
| 131 | + LOGGER.error(message, e); |
| 132 | + return 1; |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments