-
Notifications
You must be signed in to change notification settings - Fork 5
/
transform_for_mysql_bulk_load.js
73 lines (66 loc) · 1.43 KB
/
transform_for_mysql_bulk_load.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//
// Usage: node transform_for_mysql_bulk_load.js -h
//
var opts = require('commander'),
fs = require('fs'),
path = require('path'),
json2csv = require('json2csv');
opts
.option('-s --source-dir [path]', 'Path to directory containing Enron emails JSON files', './dataset')
.parse(process.argv)
var sourceDir = opts.sourceDir;
// Helper functions
var emailArrayToList = function(emailArray) {
if (emailArray) {
return emailArray.map(function(email) {
return email.address;
}).join(',');
} else {
return '';
}
}
fs.readdir(sourceDir, function(err, files) {
if (err) { console.error(err); process.exit(2); }
var filePath, data, json, numFilesProcessed = 0, totalNumFiles = files.length;
for (var i = 0; i < files.length; ++i) {
file = files[i];
filePath = path.join(sourceDir, file);
data = fs.readFileSync(filePath);
json = JSON.parse(data);
json2csv({
data: json,
fields: [
{
value: function(row) {
return emailArrayToList(row.from);
}
},
{
value: function(row) {
return emailArrayToList(row.to);
}
},
{
value: function(row) {
return emailArrayToList(row.cc);
}
},
{
value: function(row) {
return emailArrayToList(row.bcc);
}
},
{
value: 'subject'
},
{
value: 'text'
}
],
del: "\t",
hasCSVColumnTitle: false
}, function(err, csv) {
console.log(csv);
});
} // END for
}); // fs.readdir