-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_buffer.js
75 lines (64 loc) · 1.7 KB
/
array_buffer.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
74
75
console.time("exchange");
var mysql = require('mysql');
var arango = require('arango');
var docs = [];
function processRow(row, connection, quit) {
if (row instanceof Object) {
// Duplicate primary key (and remove original pair later),
// casting to string is required (_key is a string in ArangoDB!)
row._key = "" + row.findex;
docs.push(row);
}
if (docs.length >= 1000 || row === undefined) {
connection.pause();
db.import.importJSONData(
"fa_stammdaten_array_buffer",
JSON.stringify(docs, function(key, value) {
// ignore "findex" key and keys with value null, undefined or whitespace only
if (value == null || key === "findex" || (typeof value === "string" && !value.trim())) {
return undefined;
} else {
return value;
}
}),
{
createCollection: true,
waitForSync: false
},
function(err, ret) {
docs = [];
connection.resume();
if (typeof quit == "function") quit();
}
);
}
}
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: ''
});
var db = arango.Connection("http://localhost:8529/amtub");
console.log("Connecting to MySQL");
connection.connect();
var query = connection.query('SELECT * FROM _import_mysql.fa_stammdaten');
var i = 0;
query
.on('error', function(err) {
console.log(err);
})
.on('fields', function(fields) {
//console.log(fields);
console.log("Fields:", Object.keys(fields).length);
})
.on('result', function(row) {
if (++i % 1000 == 0) console.log(i);
processRow(row, connection);
})
.on('end', function() {
processRow(undefined, connection, function(){query.emit('finish')});
})
.on('finish', function() {
console.timeEnd("exchange");
process.exit();
});