-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_large_data.ts
More file actions
38 lines (33 loc) · 1.1 KB
/
read_large_data.ts
File metadata and controls
38 lines (33 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Reading a CSV file with a large/massive amount of data
* and convert it to objects.
* Data will be read from `POSTS_FILE` to the console.
*
* Run this program inside the processing_csv folder using the command:
* ```
* deno run -A ./read_large_data.ts
* ```
*/
import { POSTS_FILE, POST_COLS, Post } from "./constants.ts";
import { parse } from "https://deno.land/std@0.154.0/encoding/csv.ts";
import { TextProtoReader } from "https://deno.land/std@0.154.0/textproto/mod.ts";
import { BufReader } from "https://deno.land/std@0.154.0/io/buffer.ts";
async function readLargeCsv() {
const file = await Deno.open(POSTS_FILE, {read: true});
const reader = new TextProtoReader(BufReader.create(file));
let lines = ""
while (true) {
const line = await reader.readLine();
if (line === null) break;
lines = lines + line + "\n"
}
// Convert CSV lines into Post objects
const record: Post[] = await parse(lines,
{skipFirstRow: true, columns: POST_COLS}) as Post[]
//write object array to console
console.log(record)
}
async function main() {
await readLargeCsv();
}
await main();