]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/js/bin/json-to-arrow.js
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / js / bin / json-to-arrow.js
CommitLineData
1d09f67e
TL
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// @ts-check
21
22const fs = require('fs');
23const Path = require('path');
24const { parse } = require('json-bignum');
25const eos = require('util').promisify(require('stream').finished);
26const extension = process.env.ARROW_JS_DEBUG === 'src' ? '.ts' : '';
27const argv = require(`command-line-args`)(cliOpts(), { partial: true });
28const { RecordBatchReader, RecordBatchFileWriter, RecordBatchStreamWriter } = require(`../index${extension}`);
29
30const jsonPaths = [...(argv.json || [])];
31const arrowPaths = [...(argv.arrow || [])];
32
33(async () => {
34
35 if (!jsonPaths.length || !arrowPaths.length || (jsonPaths.length !== arrowPaths.length)) {
36 return print_usage();
37 }
38
39 await Promise.all(jsonPaths.map(async (path, i) => {
40
41 const RecordBatchWriter = argv.format !== 'stream'
42 ? RecordBatchFileWriter
43 : RecordBatchStreamWriter;
44
45 const reader = RecordBatchReader.from(parse(
46 await fs.promises.readFile(Path.resolve(path), 'utf8')));
47
48 const jsonToArrow = reader
49 .pipe(RecordBatchWriter.throughNode())
50 .pipe(fs.createWriteStream(arrowPaths[i]));
51
52 await eos(jsonToArrow);
53
54 }));
55})()
56.then((x) => +x || 0, (e) => {
57 e && process.stderr.write(`${e}`);
58 return process.exitCode || 1;
59}).then((code = 0) => process.exit(code));
60
61function cliOpts() {
62 return [
63 {
64 type: String,
65 name: 'format', alias: 'f',
66 multiple: false, defaultValue: 'file',
67 description: 'The Arrow format to write, either "file" or "stream"'
68 },
69 {
70 type: String,
71 name: 'arrow', alias: 'a',
72 multiple: true, defaultValue: [],
73 description: 'The Arrow file[s] to write'
74 },
75 {
76 type: String,
77 name: 'json', alias: 'j',
78 multiple: true, defaultValue: [],
79 description: 'The JSON file[s] to read'
80 }
81 ];
82}
83
84function print_usage() {
85 console.log(require('command-line-usage')([
86 {
87 header: 'json-to-arrow',
88 content: 'Script for converting a JSON Arrow file to a binary Arrow file'
89 },
90 {
91 header: 'Synopsis',
92 content: [
93 '$ json-to-arrow.js -j in.json -a out.arrow -f stream'
94 ]
95 },
96 {
97 header: 'Options',
98 optionList: [
99 ...cliOpts(),
100 {
101 name: 'help',
102 description: 'Print this usage guide.'
103 }
104 ]
105 },
106 ]));
107 return 1;
108}