|
| 1 | +#! /usr/bin/env node |
| 2 | + |
| 3 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 4 | +// or more contributor license agreements. See the NOTICE file |
| 5 | +// distributed with this work for additional information |
| 6 | +// regarding copyright ownership. The ASF licenses this file |
| 7 | +// to you under the Apache License, Version 2.0 (the |
| 8 | +// "License"); you may not use this file except in compliance |
| 9 | +// with the License. You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, |
| 14 | +// software distributed under the License is distributed on an |
| 15 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +// KIND, either express or implied. See the License for the |
| 17 | +// specific language governing permissions and limitations |
| 18 | +// under the License. |
| 19 | + |
| 20 | +const fs = require('fs'); |
| 21 | +const glob = require('glob'); |
| 22 | +const path = require('path'); |
| 23 | +const { Table } = require('../index'); |
| 24 | +const { promisify } = require('util'); |
| 25 | +const { parse } = require('json-bignum'); |
| 26 | +const argv = require(`command-line-args`)(cliOpts(), { partial: true }); |
| 27 | + |
| 28 | +const encoding = 'binary'; |
| 29 | +const stream = argv.format === 'stream'; |
| 30 | +const jsonPaths = [...(argv.json || [])]; |
| 31 | +const arrowPaths = [...(argv.arrow || [])]; |
| 32 | + |
| 33 | +if (!jsonPaths.length || !arrowPaths.length || (jsonPaths.length !== arrowPaths.length)) { |
| 34 | + return print_usage(); |
| 35 | +} |
| 36 | + |
| 37 | +const readFile = callResolved(promisify(fs.readFile)); |
| 38 | +const writeFile = callResolved(promisify(fs.writeFile)); |
| 39 | + |
| 40 | +(async () => await Promise.all(jsonPaths.map(async (jPath, i) => { |
| 41 | + const aPath = arrowPaths[i]; |
| 42 | + const arrowTable = Table.from(parse('' + (await readFile(jPath)))); |
| 43 | + await writeFile(aPath, arrowTable.serialize(encoding, stream), encoding); |
| 44 | +})))().catch((e) => { console.error(e); process.exit(1); }); |
| 45 | + |
| 46 | +function callResolved(fn) { |
| 47 | + return async (path_, ...xs) => await fn(path.resolve(path_), ...xs); |
| 48 | +} |
| 49 | + |
| 50 | +function cliOpts() { |
| 51 | + return [ |
| 52 | + { |
| 53 | + type: String, |
| 54 | + name: 'format', alias: 'f', |
| 55 | + multiple: false, defaultValue: 'file', |
| 56 | + description: 'The Arrow format to write, either "file" or "stream"' |
| 57 | + }, |
| 58 | + { |
| 59 | + type: String, |
| 60 | + name: 'arrow', alias: 'a', |
| 61 | + multiple: true, defaultValue: [], |
| 62 | + description: 'The Arrow file[s] to write' |
| 63 | + }, |
| 64 | + { |
| 65 | + type: String, |
| 66 | + name: 'json', alias: 'j', |
| 67 | + multiple: true, defaultValue: [], |
| 68 | + description: 'The JSON file[s] to read' |
| 69 | + } |
| 70 | + ]; |
| 71 | +} |
| 72 | + |
| 73 | +function print_usage() { |
| 74 | + console.log(require('command-line-usage')([ |
| 75 | + { |
| 76 | + header: 'json-to-arrow', |
| 77 | + content: 'Script for converting an JSON Arrow file to a binary Arrow file' |
| 78 | + }, |
| 79 | + { |
| 80 | + header: 'Synopsis', |
| 81 | + content: [ |
| 82 | + '$ json-to-arrow.js -j in.json -a out.arrow -f stream' |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + header: 'Options', |
| 87 | + optionList: [ |
| 88 | + ...cliOpts(), |
| 89 | + { |
| 90 | + name: 'help', |
| 91 | + description: 'Print this usage guide.' |
| 92 | + } |
| 93 | + ] |
| 94 | + }, |
| 95 | + ])); |
| 96 | + process.exit(1); |
| 97 | +} |
0 commit comments