Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

pgimport.js 3.1 KB

You have to be logged in to leave a comment. Sign In
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  1. "use strict";
  2. const util = require('util');
  3. const zlib = require('zlib');
  4. const fs = require('fs');
  5. const through = require('through2');
  6. const fws = require('flush-write-stream');
  7. const pg = require('pg');
  8. const async = require('async');
  9. const throughput = require('./lib/throughput');
  10. const io = require('./lib/io');
  11. const options = require('yargs').argv;
  12. const date = options.date || '2016-07-31';
  13. var ninserts = 0;
  14. var autp = throughput('authors');
  15. var wtp = throughput('works');
  16. var etp = throughput('editions');
  17. /**
  18. * Pipe that runs PostgreSQL queries.
  19. */
  20. function runQueries(client, finished) {
  21. var nqueries = 0;
  22. var started = false;
  23. function write(data, enc, next) {
  24. async.series([
  25. (cb) => {
  26. if (started) {
  27. cb();
  28. } else {
  29. client.query('BEGIN ISOLATION LEVEL READ UNCOMMITTED', (err) => {
  30. started = true;
  31. cb(err);
  32. });
  33. }
  34. },
  35. (cb) => client.query(data, cb),
  36. (cb) => {
  37. nqueries += 1;
  38. if (nqueries % 10000 === 0) {
  39. async.series([
  40. (cb) => client.query('COMMIT', cb),
  41. (cb) => client.query('BEGIN ISOLATION LEVEL READ UNCOMMITTED', cb)
  42. ], cb);
  43. } else {
  44. process.nextTick(cb);
  45. }
  46. }
  47. ], next);
  48. }
  49. function flush(cb) {
  50. client.query('COMMIT', (err) => {
  51. process.nextTick(finished, err, nqueries);
  52. });
  53. }
  54. return fws.obj(write, flush);
  55. }
  56. const imports = {
  57. authors: function (rec) {
  58. return {
  59. text: 'INSERT INTO authors (author_key, author_name, author_data) VALUES ($1, $2, $3)',
  60. name: 'insert-author',
  61. values: [rec.key, rec.name, JSON.stringify(rec)]
  62. };
  63. },
  64. works: function (rec) {
  65. return {
  66. text: 'INSERT INTO works (work_key, work_title, work_data) VALUES ($1, $2, $3)',
  67. name: 'insert-work',
  68. values: [rec.key, rec.title, JSON.stringify(rec)]
  69. };
  70. },
  71. editions: function(rec) {
  72. return {
  73. text: 'INSERT INTO editions (edition_key, edition_title, edition_data) VALUES ($1, $2, $3)',
  74. name: 'insert-edition',
  75. values: [rec.key, rec.title, JSON.stringify(rec)]
  76. };
  77. }
  78. };
  79. function doImport(name, callback) {
  80. const proc = imports[name];
  81. if (proc === undefined) {
  82. return callback(new Error("no such import " + name));
  83. }
  84. const client = new pg.Client(options['db-url']);
  85. async.waterfall([
  86. client.connect.bind(client),
  87. (_, cb) => io.openFile(util.format("data/ol_dump_%s_%s.txt.gz", name, date), cb),
  88. (stream, cb) => {
  89. stream.pipe(zlib.createUnzip())
  90. .pipe(io.decodeLines())
  91. .pipe(through.obj((rec, enc, cb) => {
  92. cb(null, proc(rec));
  93. }))
  94. .pipe(runQueries(client, cb));
  95. }
  96. ], (err) => {
  97. if (err) {
  98. console.error("error running %s: %s", name, err);
  99. } else {
  100. console.info("finished %s", name);
  101. }
  102. client.end((e2) => {
  103. if (err) {
  104. callback(err);
  105. } else if (e2) {
  106. callback(e2);
  107. } else {
  108. callback();
  109. }
  110. });
  111. });
  112. }
  113. for (let name of Object.keys(imports)) {
  114. module.exports[name] = doImport.bind(null, name);
  115. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...