Last active
August 26, 2022 22:17
-
-
Save hubgit/7ee9693989b74c4414be0eb1a10836b1 to your computer and use it in GitHub Desktop.
Read/write a gzipped JSON lines file with Deno
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createInputReader = async (path: string) => { | |
const file = await Deno.open(path, { | |
read: true, | |
}) | |
return file.readable | |
.pipeThrough(new DecompressionStream('gzip')) | |
.pipeThrough(new TextDecoderStream()) | |
.pipeThrough(new TextLineStream()) | |
.pipeThrough( | |
new TransformStream({ | |
transform(chunk, controller) { | |
controller.enqueue(JSON.parse(chunk)) | |
}, | |
}) | |
) | |
} | |
// const input = await createInputReader('example.jsonl.gz') | |
// for await (const item of input) { | |
// // do something | |
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createOutputWriter = async (path: string) => { | |
const file = await Deno.open(path, { | |
create: true, | |
write: true, | |
truncate: true, | |
}) | |
const stream = new TransformStream() | |
stream.readable | |
.pipeThrough( | |
new TransformStream({ | |
transform(chunk, controller) { | |
controller.enqueue(JSON.stringify(chunk)) | |
controller.enqueue('\n') | |
}, | |
}) | |
) | |
.pipeThrough(new TextEncoderStream()) | |
.pipeThrough(new CompressionStream('gzip')) | |
.pipeTo(file.writable) | |
return stream.writable.getWriter() | |
} | |
// const writer = await createOutputWriter('example.jsonl.gz') | |
// await writer.write({ foo: 'bar' }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment