hyparquet-writer/src/write.js

57 lines
1.3 KiB
JavaScript
Raw Normal View History

2025-04-08 10:22:30 +00:00
import { ByteWriter } from './bytewriter.js'
import { fileWriter } from './filewriter.js'
2025-04-07 08:02:21 +00:00
import { ParquetWriter } from './parquet-writer.js'
2025-04-08 06:14:48 +00:00
import { schemaFromColumnData } from './schema.js'
2025-03-26 04:06:43 +00:00
/**
2025-04-08 10:22:30 +00:00
* Write data as parquet to a file or stream.
2025-03-26 04:06:43 +00:00
*
2025-04-08 10:22:30 +00:00
* @import {ParquetWriteOptions} from '../src/types.js'
* @param {ParquetWriteOptions} options
2025-03-26 04:06:43 +00:00
*/
2025-04-15 06:22:55 +00:00
export function parquetWrite({
writer,
columnData,
compressed = true,
statistics = true,
rowGroupSize = 100000,
kvMetadata,
}) {
2025-04-07 08:02:21 +00:00
const schema = schemaFromColumnData(columnData)
2025-04-08 06:14:48 +00:00
const pq = new ParquetWriter({
writer,
2025-04-07 08:02:21 +00:00
schema,
compressed,
statistics,
kvMetadata,
})
2025-04-08 06:14:48 +00:00
pq.write({
2025-04-07 08:02:21 +00:00
columnData,
rowGroupSize,
})
2025-04-08 06:14:48 +00:00
pq.finish()
2025-04-08 10:22:30 +00:00
}
/**
* Write data as parquet to an ArrayBuffer.
*
* @param {Omit<ParquetWriteOptions, 'writer'>} options
* @returns {ArrayBuffer}
*/
export function parquetWriteBuffer(options) {
const writer = new ByteWriter()
parquetWrite({ ...options, writer })
2025-04-08 06:14:48 +00:00
return writer.getBuffer()
2025-03-26 04:06:43 +00:00
}
2025-04-08 10:22:30 +00:00
/**
* Write data as parquet to an ArrayBuffer.
*
* @param {Omit<ParquetWriteOptions, 'writer'> & {filename: string}} options
*/
export function parquetWriteFile(options) {
const { filename, ...rest } = options
const writer = fileWriter(filename)
parquetWrite({ ...rest, writer })
}