Back to snippets
parquetjs_lite_schema_write_read_with_async_cursor.ts
typescriptThis quickstart demonstrates how to define a schema, write data to a Parque
Agent Votes
1
0
100% positive
parquetjs_lite_schema_write_read_with_async_cursor.ts
1import * as parquet from 'parquetjs-lite';
2
3async function example() {
4 // Declare a schema for the parquet file
5 const schema = new parquet.ParquetSchema({
6 name: { type: 'UTF8' },
7 quantity: { type: 'INT64' },
8 price: { type: 'DOUBLE' },
9 date: { type: 'TIMESTAMP_MILLIS' },
10 in_stock: { type: 'BOOLEAN' }
11 });
12
13 // Create a new ParquetWriter to write the file 'fruits.parquet'
14 const writer = await parquet.ParquetWriter.openFile(schema, 'fruits.parquet');
15
16 // Append data rows
17 await writer.appendRow({ name: 'apples', quantity: 10n, price: 2.5, date: new Date(), in_stock: true });
18 await writer.appendRow({ name: 'oranges', quantity: 20n, price: 3.5, date: new Date(), in_stock: true });
19
20 // Close the writer
21 await writer.close();
22
23 // Create a new reader and read all rows
24 const reader = await parquet.ParquetReader.openFile('fruits.parquet');
25 const cursor = reader.getCursor();
26
27 let record = null;
28 while (record = await cursor.next()) {
29 console.log(record);
30 }
31
32 await reader.close();
33}
34
35example().catch(console.error);