|
| 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 org.apache.arrow.memory.BufferAllocator; |
| 22 | +import org.apache.arrow.memory.RootAllocator; |
| 23 | +import org.apache.arrow.vector.VectorLoader; |
| 24 | +import org.apache.arrow.vector.VectorUnloader; |
| 25 | +import org.apache.arrow.vector.complex.NullableMapVector; |
| 26 | +import org.apache.arrow.vector.file.ArrowBlock; |
| 27 | +import org.apache.arrow.vector.file.ArrowFooter; |
| 28 | +import org.apache.arrow.vector.file.ArrowReader; |
| 29 | +import org.apache.arrow.vector.file.ArrowWriter; |
| 30 | +import org.apache.arrow.vector.schema.ArrowRecordBatch; |
| 31 | +import org.apache.arrow.vector.types.Types; |
| 32 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 33 | +import org.apache.commons.cli.CommandLine; |
| 34 | +import org.apache.commons.cli.CommandLineParser; |
| 35 | +import org.apache.commons.cli.Option; |
| 36 | +import org.apache.commons.cli.OptionBuilder; |
| 37 | +import org.apache.commons.cli.Options; |
| 38 | +import org.apache.commons.cli.PosixParser; |
| 39 | +import org.slf4j.Logger; |
| 40 | +import org.slf4j.LoggerFactory; |
| 41 | + |
| 42 | +import java.io.File; |
| 43 | +import java.io.FileInputStream; |
| 44 | +import java.io.FileOutputStream; |
| 45 | +import java.util.Arrays; |
| 46 | +import java.util.List; |
| 47 | + |
| 48 | +public class FileRoundtrip { |
| 49 | + private static final Logger LOGGER = LoggerFactory.getLogger(FileRoundtrip.class); |
| 50 | + public static final Options OPTIONS; |
| 51 | + |
| 52 | + static { |
| 53 | + OPTIONS = new Options(); |
| 54 | + } |
| 55 | + |
| 56 | + public static void main(String[] args) { |
| 57 | + try { |
| 58 | + String[] cargs = Arrays.copyOfRange(args, 1, args.length); |
| 59 | + |
| 60 | + CommandLineParser parser = new PosixParser(); |
| 61 | + CommandLine cmd = parser.parse(OPTIONS, cargs, false); |
| 62 | + |
| 63 | + String[] parsed_args = cmd.getArgs(); |
| 64 | + |
| 65 | + String inFileName = parsed_args[0]; |
| 66 | + String outFileName = parsed_args[1]; |
| 67 | + |
| 68 | + BufferAllocator allocator = new RootAllocator(Integer.MAX_VALUE); |
| 69 | + |
| 70 | + File inFile = new File(inFileName); |
| 71 | + FileInputStream fileInputStream = new FileInputStream(inFile); |
| 72 | + ArrowReader arrowReader = new ArrowReader(fileInputStream.getChannel(), allocator); |
| 73 | + |
| 74 | + ArrowFooter footer = arrowReader.readFooter(); |
| 75 | + Schema schema = footer.getSchema(); |
| 76 | + LOGGER.debug("Found schema: " + schema); |
| 77 | + |
| 78 | + File outFile = new File(outFileName); |
| 79 | + FileOutputStream fileOutputStream = new FileOutputStream(outFile); |
| 80 | + ArrowWriter arrowWriter = new ArrowWriter(fileOutputStream.getChannel(), schema); |
| 81 | + |
| 82 | + // initialize vectors |
| 83 | + |
| 84 | + List<ArrowBlock> recordBatches = footer.getRecordBatches(); |
| 85 | + for (ArrowBlock rbBlock : recordBatches) { |
| 86 | + ArrowRecordBatch inRecordBatch = arrowReader.readRecordBatch(rbBlock); |
| 87 | + |
| 88 | + NullableMapVector inParent = new NullableMapVector("parent", allocator, null); |
| 89 | + NullableMapVector root = inParent.addOrGet("root", Types.MinorType.MAP, NullableMapVector.class); |
| 90 | + VectorLoader vectorLoader = new VectorLoader(schema, root); |
| 91 | + vectorLoader.load(inRecordBatch); |
| 92 | + |
| 93 | + NullableMapVector outParent = new NullableMapVector("parent", allocator, null); |
| 94 | + VectorUnloader vectorUnloader = new VectorUnloader(outParent); |
| 95 | + ArrowRecordBatch recordBatch = vectorUnloader.getRecordBatch(); |
| 96 | + arrowWriter.writeRecordBatch(recordBatch); |
| 97 | + } |
| 98 | + } catch (Throwable th) { |
| 99 | + System.err.println(th.getMessage()); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments