-
Notifications
You must be signed in to change notification settings - Fork 1
/
movies-graph-populate-vertices-batch.js
74 lines (65 loc) · 2.17 KB
/
movies-graph-populate-vertices-batch.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
/**
* Created by: edgardo on 2/9/17.
* Source: .js
* Author: edgardo
* Description:
*
*/
const neo4j = require('neo4j-driver').v1;
const vertices = require('./movie/vertices.json');
/* Instantiate connection */
var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neoj4"));
/* Register a callback to know if driver creation failed. */
/* This could happen due to wrong credentials or database unavailability */
driver.onError = function(error) {
console.log('Driver instantiation failed', error);
};
/* Create a session to run Cypher statements in. */
var session = driver.session();
const batchSize = 300;
let eQueue = Object.keys(vertices);
let total = eQueue.length, current = 0;
console.log(total);
/* Register a callback to know if driver creation was successful */
driver.onCompleted = function() {
/* proceed with using the driver, it was successfully instantiated */
console.log('Driver instantiation success');
console.time("main");
/* Insertion function */
function insertEdge(arr) {
var query = [];
/* Persist all vertices */
arr.forEach((vkey)=> {
let vertex = vertices[vkey];
var name = (vertex.name && vertex.name.replace)?vertex.name.replace(/'/g,''):'';
query.push("CREATE (a" +vkey+":Vertex {id:'"+vertex.id +"',label:'"+vertex.label+"',genreId:'"+ vertex.genreId+ "',name:'"+ name+ "'})");
});
//console.time("time");
session.run(query.join('\n'))
.then(function(result){
//console.timeEnd("time");
/* Continue inserting */
if (eQueue.length) {
insertEdge(eQueue.splice(0, batchSize));
} else {
console.timeEnd("main");
session.close();
process.exit();
}
})
.catch(function(error) {
console.timeEnd("time");
console.log("Error: Edge batch creation failed.", error, current / total);
/* Continue inserting */
if (eQueue.length) {
insertEdge(eQueue.splice(0, batchSize));
} else {
console.timeEnd("main");
session.close();
process.exit();
}
});
}
/* Initiating vertex insertion */
insertEdge(eQueue.splice(0, batchSize));
};