2024-02-04 20:31:57 +00:00
|
|
|
import { createReadStream, createWriteStream, promises as fs } from 'fs'
|
2024-07-02 23:33:42 +00:00
|
|
|
import { compressors } from 'hyparquet-compressors'
|
2024-02-04 20:31:57 +00:00
|
|
|
import { parquetRead } from './src/hyparquet.js'
|
|
|
|
|
|
|
|
|
|
const url = 'https://huggingface.co/datasets/wikimedia/wikipedia/resolve/main/20231101.en/train-00000-of-00041.parquet'
|
2024-04-18 00:45:15 +00:00
|
|
|
const filename = 'example.parquet'
|
2024-02-04 20:31:57 +00:00
|
|
|
|
|
|
|
|
// download test parquet file if needed
|
2024-04-18 00:45:15 +00:00
|
|
|
let stat = await fs.stat(filename).catch(() => undefined)
|
2024-02-04 20:31:57 +00:00
|
|
|
if (!stat) {
|
|
|
|
|
console.log('downloading ' + url)
|
|
|
|
|
const res = await fetch(url)
|
|
|
|
|
if (!res.ok) throw new Error(res.statusText)
|
|
|
|
|
// write to file async
|
2024-04-18 00:45:15 +00:00
|
|
|
const writeStream = createWriteStream(filename)
|
2024-02-04 20:31:57 +00:00
|
|
|
for await (const chunk of res.body) {
|
|
|
|
|
writeStream.write(chunk)
|
|
|
|
|
}
|
2024-02-19 00:42:58 +00:00
|
|
|
writeStream.end()
|
|
|
|
|
console.log('downloaded example.parquet')
|
2024-04-18 00:45:15 +00:00
|
|
|
stat = await fs.stat(filename).catch(() => undefined)
|
2024-02-04 20:31:57 +00:00
|
|
|
}
|
2024-02-21 22:16:51 +00:00
|
|
|
|
2024-02-04 20:31:57 +00:00
|
|
|
// asyncBuffer
|
|
|
|
|
const file = {
|
|
|
|
|
byteLength: stat.size,
|
|
|
|
|
async slice(start, end) {
|
|
|
|
|
// read file slice
|
2024-04-18 00:45:15 +00:00
|
|
|
const readStream = createReadStream(filename, { start, end })
|
2024-07-02 23:35:42 +00:00
|
|
|
return await readStreamToArrayBuffer(readStream)
|
2024-02-04 20:31:57 +00:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
const startTime = performance.now()
|
2024-04-30 07:09:41 +00:00
|
|
|
console.log('parsing example.parquet data...')
|
2024-02-21 22:16:51 +00:00
|
|
|
|
2024-02-04 20:31:57 +00:00
|
|
|
// read parquet file
|
2024-02-21 22:16:51 +00:00
|
|
|
await parquetRead({
|
|
|
|
|
file,
|
2024-07-02 23:33:42 +00:00
|
|
|
compressors,
|
2024-02-21 22:16:51 +00:00
|
|
|
})
|
2024-02-04 20:31:57 +00:00
|
|
|
const ms = performance.now() - startTime
|
|
|
|
|
console.log(`parsed ${stat.size.toLocaleString()} bytes in ${ms.toFixed(0)} ms`)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert a web ReadableStream to ArrayBuffer.
|
|
|
|
|
*
|
|
|
|
|
* @param {ReadStream} input
|
|
|
|
|
* @returns {Promise<ArrayBuffer>}
|
|
|
|
|
*/
|
|
|
|
|
function readStreamToArrayBuffer(input) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
const chunks = []
|
|
|
|
|
input.on('data', chunk => chunks.push(chunk))
|
2024-07-02 23:35:42 +00:00
|
|
|
input.on('end', () => {
|
|
|
|
|
const buffer = Buffer.concat(chunks)
|
|
|
|
|
resolve(buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength))
|
|
|
|
|
})
|
2024-02-04 20:31:57 +00:00
|
|
|
input.on('error', reject)
|
|
|
|
|
})
|
|
|
|
|
}
|