2025-03-26 03:15:14 +00:00
# Hyparquet Writer
2025-04-07 08:27:06 +00:00

2025-03-27 06:37:05 +00:00
[](https://www.npmjs.com/package/hyparquet-writer)
2025-03-27 07:27:22 +00:00
[](https://www.npmjs.com/package/hyparquet-writer)
2025-03-26 07:45:22 +00:00
[](https://github.com/hyparam/hyparquet-writer/actions)
2025-03-26 03:15:14 +00:00
[](https://opensource.org/licenses/MIT)
2025-10-26 21:30:49 +00:00

2025-04-01 06:32:14 +00:00
[](https://www.npmjs.com/package/hyparquet-writer?activeTab=dependencies)
2025-03-26 05:36:06 +00:00
2025-04-03 07:42:54 +00:00
Hyparquet Writer is a JavaScript library for writing [Apache Parquet ](https://parquet.apache.org ) files. It is designed to be lightweight, fast and store data very efficiently. It is a companion to the [hyparquet ](https://github.com/hyparam/hyparquet ) library, which is a JavaScript library for reading parquet files.
2025-04-08 10:22:30 +00:00
## Quick Start
2025-03-26 05:36:06 +00:00
2025-04-08 10:22:30 +00:00
To write a parquet file to an `ArrayBuffer` use `parquetWriteBuffer` with argument `columnData` . Each column in `columnData` should contain:
2025-04-08 06:14:48 +00:00
- `name` : the column name
- `data` : an array of same-type values
2025-04-08 10:22:30 +00:00
- `type` : the parquet schema type (optional)
2025-04-08 06:14:48 +00:00
2025-04-08 10:22:30 +00:00
```javascript
import { parquetWriteBuffer } from 'hyparquet-writer'
const arrayBuffer = parquetWriteBuffer({
columnData: [
2025-05-09 21:47:22 +00:00
{ name: 'name', data: ['Alice', 'Bob', 'Charlie'], type: 'STRING' },
2025-04-08 10:22:30 +00:00
{ name: 'age', data: [25, 30, 35], type: 'INT32' },
],
})
```
2025-10-26 21:30:49 +00:00
Note: if `type` is not provided, the type will be guessed from the data. The supported `BasicType` are a superset of the parquet primitive types:
2025-04-08 10:22:30 +00:00
2025-10-26 21:30:49 +00:00
| Basic Type | Equivalent Schema Element |
|------|----------------|
2025-05-09 21:47:22 +00:00
| `BOOLEAN` | `{ type: 'BOOLEAN' }` |
| `INT32` | `{ type: 'INT32' }` |
| `INT64` | `{ type: 'INT64' }` |
| `FLOAT` | `{ type: 'FLOAT' }` |
| `DOUBLE` | `{ type: 'DOUBLE' }` |
| `BYTE_ARRAY` | `{ type: 'BYTE_ARRAY' }` |
| `STRING` | `{ type: 'BYTE_ARRAY', converted_type: 'UTF8' }` |
| `JSON` | `{ type: 'BYTE_ARRAY', converted_type: 'JSON' }` |
| `TIMESTAMP` | `{ type: 'INT64', converted_type: 'TIMESTAMP_MILLIS' }` |
| `UUID` | `{ type: 'FIXED_LEN_BYTE_ARRAY', type_length: 16, logical_type: { type: 'UUID' } }` |
| `FLOAT16` | `{ type: 'FIXED_LEN_BYTE_ARRAY', type_length: 2, logical_type: { type: 'FLOAT16' } }` |
2025-10-26 21:30:49 +00:00
| `GEOMETRY` | `{ type: 'BYTE_ARRAY', logical_type: { type: 'GEOMETRY' } }` |
| `GEOGRAPHY` | `{ type: 'BYTE_ARRAY', logical_type: { type: 'GEOGRAPHY' } }` |
2025-04-15 06:22:55 +00:00
2025-05-09 21:47:22 +00:00
More types are supported but require defining the `schema` explicitly. See the [advanced usage ](#advanced-usage ) section for more details.
2025-04-08 10:22:30 +00:00
2025-10-26 21:30:49 +00:00
### Write to Local Parquet File (nodejs)
2025-04-08 10:22:30 +00:00
To write a local parquet file in node.js use `parquetWriteFile` with arguments `filename` and `columnData` :
2025-03-26 07:45:22 +00:00
2025-03-26 05:36:06 +00:00
```javascript
2025-04-08 10:22:30 +00:00
const { parquetWriteFile } = await import('hyparquet-writer')
2025-03-26 05:36:06 +00:00
2025-04-08 10:22:30 +00:00
parquetWriteFile({
filename: 'example.parquet',
2025-03-27 07:27:22 +00:00
columnData: [
2025-05-09 21:47:22 +00:00
{ name: 'name', data: ['Alice', 'Bob', 'Charlie'], type: 'STRING' },
2025-03-28 23:13:27 +00:00
{ name: 'age', data: [25, 30, 35], type: 'INT32' },
2025-03-27 07:27:22 +00:00
],
})
2025-03-26 05:36:06 +00:00
```
2025-04-08 10:22:30 +00:00
Note: hyparquet-writer is published as an ES module, so dynamic `import()` may be required on the command line.
## Advanced Usage
2025-04-04 03:19:37 +00:00
2025-04-08 10:22:30 +00:00
Options can be passed to `parquetWrite` to adjust parquet file writing behavior:
2025-04-08 06:14:48 +00:00
2025-04-08 10:22:30 +00:00
- `writer` : a generic writer object
2025-05-09 21:47:22 +00:00
- `schema` : parquet schema object (optional)
2025-04-15 06:22:55 +00:00
- `compressed` : use snappy compression (default true)
2025-04-03 20:21:57 +00:00
- `statistics` : write column statistics (default true)
- `rowGroupSize` : number of rows in each row group (default 100000)
2025-04-08 10:22:30 +00:00
- `kvMetadata` : extra key-value metadata to be stored in the parquet footer
2025-04-04 03:19:37 +00:00
2025-04-08 06:14:48 +00:00
```javascript
2025-04-08 10:22:30 +00:00
import { ByteWriter, parquetWrite } from 'hyparquet-writer'
2025-04-08 06:14:48 +00:00
2025-04-08 10:22:30 +00:00
const writer = new ByteWriter()
2025-05-09 21:47:22 +00:00
parquetWrite({
2025-04-08 10:22:30 +00:00
writer,
2025-04-08 06:14:48 +00:00
columnData: [
2025-05-09 21:47:22 +00:00
{ name: 'name', data: ['Alice', 'Bob', 'Charlie'] },
{ name: 'age', data: [25, 30, 35] },
{ name: 'dob', data: [new Date(1000000), new Date(2000000), new Date(3000000)] },
],
// explicit schema:
schema: [
{ name: 'root', num_children: 3 },
{ name: 'name', type: 'BYTE_ARRAY', converted_type: 'UTF8' },
{ name: 'age', type: 'FIXED_LEN_BYTE_ARRAY', type_length: 4, converted_type: 'DECIMAL', scale: 2, precision: 4 },
{ name: 'dob', type: 'INT32', converted_type: 'DATE' },
2025-04-08 06:14:48 +00:00
],
2025-04-15 06:22:55 +00:00
compressed: false,
2025-04-08 06:14:48 +00:00
statistics: false,
rowGroupSize: 1000,
2025-04-15 06:22:55 +00:00
kvMetadata: [
{ key: 'key1', value: 'value1' },
{ key: 'key2', value: 'value2' },
],
})
2025-05-09 21:47:22 +00:00
const arrayBuffer = writer.getBuffer()
2025-04-15 06:22:55 +00:00
```
2025-05-09 21:47:22 +00:00
### Types
Parquet requires an explicit schema to be defined. You can provide schema information in three ways:
1. **Type** : You can provide a `type` in the `columnData` elements, the type will be used as the schema type.
2. **Schema** : You can provide a `schema` parameter that explicitly defines the parquet schema. The schema should be an array of `SchemaElement` objects (see [parquet-format ](https://github.com/apache/parquet-format )), each containing the following properties:
- `name` : column name
- `type` : parquet type
- `num_children` : number children in parquet nested schema (optional)
- `converted_type` : parquet converted type (optional)
- `logical_type` : parquet logical type (optional)
- `repetition_type` : parquet repetition type (optional)
- `type_length` : length for `FIXED_LENGTH_BYTE_ARRAY` type (optional)
- `scale` : the scale factor for `DECIMAL` converted types (optional)
- `precision` : the precision for `DECIMAL` converted types (optional)
- `field_id` : the field id for the column (optional)
3. **Auto-detect** : If you provide no type or schema, the type will be auto-detected from the data. However, it is recommended that you provide type information when possible. (zero rows would throw an exception, floats might be typed as int, etc)
Most converted types will be auto-detected if you just provide data with no types. However, it is still recommended that you provide type information when possible. (zero rows would throw an exception, floats might be typed as int, etc)
2025-04-15 06:22:55 +00:00
2025-05-09 21:47:22 +00:00
#### Schema Overrides
You can use mostly automatic schema detection, but override the schema for specific columns. This is useful if most of the column types can be automatically determined, but you want to use a specific schema element for one particular element.
2025-04-15 06:22:55 +00:00
```javascript
2025-10-23 19:15:14 +00:00
const { ByteWriter, parquetWrite, schemaFromColumnData } = await import("hyparquet-writer")
2025-05-09 21:47:22 +00:00
const columnData = [
{ name: 'unsigned_int', data: [1000000, 2000000] },
{ name: 'signed_int', data: [1000000, 2000000] },
]
2025-10-23 19:15:14 +00:00
const writer = new ByteWriter()
2025-04-15 06:22:55 +00:00
parquetWrite({
2025-10-23 19:15:14 +00:00
writer,
2025-05-09 21:47:22 +00:00
columnData,
// override schema for uint column
schema: schemaFromColumnData({
columnData,
schemaOverrides: {
unsigned_int: {
2025-10-23 19:15:14 +00:00
name: 'unsigned_int',
2025-05-09 21:47:22 +00:00
type: 'INT32',
converted_type: 'UINT_32',
2025-10-23 19:15:14 +00:00
repetition_type: 'REQUIRED',
2025-05-09 21:47:22 +00:00
},
2025-04-15 06:22:55 +00:00
},
2025-05-09 21:47:22 +00:00
}),
2025-04-08 06:14:48 +00:00
})
```
2025-03-26 05:36:06 +00:00
## References
- https://github.com/hyparam/hyparquet
2025-03-31 21:51:11 +00:00
- https://github.com/hyparam/hyparquet-compressors
2025-03-26 05:36:06 +00:00
- https://github.com/apache/parquet-format
- https://github.com/apache/parquet-testing