Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions javascript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lib/*_generated.js
dist
node_modules
typings
50 changes: 50 additions & 0 deletions javascript/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!---
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. See accompanying LICENSE file.
-->

### Installation

From this directory, run:

``` bash
$ npm install # pull dependencies
$ tsc # build typescript
$ webpack # bundle for the browser
```

### Usage
The library is designed to be used with node.js or in the browser, this repository contains examples of both.

#### Node
Import the arrow module:

``` js
var arrow = require("arrow.js");
```

See [bin/arrow_schema.js](bin/arrow_schema.js) and [bin/arrow2csv.js](bin/arrow2csv.js) for usage examples.

#### Browser
Include `dist/arrow-bundle.js` in a `<script />` tag:
``` html
<script src="arrow-bundle.js"/>
```
See [examples/read_file.html](examples/read_file.html) for a usage example - or try it out now at [theneuralbit.github.io/arrow](http://theneuralbit.github.io/arrow)

### API
##### `arrow.loadSchema(buffer)`
Returns a JSON representation of the file's Arrow schema.

##### `arrow.loadVectors(buffer)`
Returns a dictionary of `Vector` objects, one for each column, indexed by the column's name.
Vector objects have, at minimum, a `get(i)` method and a `length` attribute.
47 changes: 47 additions & 0 deletions javascript/bin/arrow2csv.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#! /usr/bin/env node

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

var fs = require('fs')
var process = require('process');
var loadVectors = require('../dist/arrow.js').loadVectors;
var program = require('commander');

function list (val) {
return val.split(',');
}

program
.version('0.1.0')
.usage('[options] <file>')
.option('-s --schema <list>', 'A comma-separated list of column names', list)
.parse(process.argv);

if (!program.schema) {
program.outputHelp();
process.exit(1);
}

var buf = fs.readFileSync(process.argv[process.argv.length - 1]);
var vectors = loadVectors(buf);

for (var i = 0; i < vectors[program.schema[0]].length; i += 1|0) {
console.log(program.schema.map(function (field) {
return '' + vectors[field].get(i);
}).join(','));
}
25 changes: 25 additions & 0 deletions javascript/bin/arrow_schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#! /usr/bin/env node

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

var fs = require('fs');
var process = require('process');
var loadSchema = require('../dist/arrow.js').loadSchema;

var buf = fs.readFileSync(process.argv[process.argv.length - 1]);
console.log(JSON.stringify(loadSchema(buf), null, '\t'));
79 changes: 79 additions & 0 deletions javascript/examples/read_file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!DOCTYPE html>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<html>
<head>
<title>arrow.js browser test</title>
<meta charset="utf-8">
<style>
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
</style>
<script type="text/javascript">
var reader = new FileReader();
function addCell (tr, type, name) {
var td = document.createElement(type)
td.textContent = name;
tr.appendChild(td);
}
reader.onload = function (evt) {
var buf = new Uint8Array(evt.target.result);
var schema = arrow.loadSchema(buf);
var vectors = arrow.loadVectors(buf);
var length = vectors[schema[0].name].length;

var thead = document.getElementById("thead");
var tbody = document.getElementById("tbody");
var header_row = document.createElement("tr");

schema.forEach(function (d) {
addCell(header_row, "th", d.name);
});

thead.appendChild(header_row);

for (var i = 0; i < length; i += 1|0) {
var tr = document.createElement("tr");
schema.forEach(function (d) { addCell(tr, "td", vectors[d.name].get(i)); });
tbody.appendChild(tr);
}
}

function handleFiles(files) {
reader.readAsArrayBuffer(files[0]);
}
</script>
</head>
<body>
<input id="arrow-in" type="file" onchange="handleFiles(this.files)" />
<table>
<thead id="thead">
</thead>
<tbody id="tbody">
</tbody>
</table>
<script type="text/javascript" src="../dist/arrow-bundle.js"></script>
</body>
</html>
5 changes: 5 additions & 0 deletions javascript/lib/Arrow_generated.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export var org: {
apache: {
arrow: any
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a WIP PR for adding TypeScript support to flatbuffers (#4065), which should make this definition unnecessary.

Loading