Guest User

Untitled

a guest
Oct 25th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.67 KB | None | 0 0
  1. Error: Connection terminated by user
  2. at Client.end (/media/DarkHawk/srv/Databases/PremadeDB/Geonames/node_modules/pg/lib/client.js:402:36)
  3. at Pool._remove (/media/DarkHawk/srv/Databases/PremadeDB/Geonames/node_modules/pg-pool/index.js:135:12)
  4. at Timeout.setTimeout (/media/DarkHawk/srv/Databases/PremadeDB/Geonames/node_modules/pg-pool/index.js:38:12)
  5. at ontimeout (timers.js:498:11)
  6. at tryOnTimeout (timers.js:323:5)
  7. at Timer.listOnTimeout (timers.js:290:5)
  8. Line added 6052 0.05135667935111022%
  9. (node:31819) UnhandledPromiseRejectionWarning: Error: This socket is closed
  10. at Socket._writeGeneric (net.js:729:18)
  11. at Socket._write (net.js:783:8)
  12. at doWrite (_stream_writable.js:397:12)
  13. at writeOrBuffer (_stream_writable.js:383:5)
  14. at Socket.Writable.write (_stream_writable.js:290:11)
  15. at Socket.write (net.js:707:40)
  16. at Connection.end (/media/DarkHawk/srv/Databases/PremadeDB/Geonames/node_modules/pg/lib/connection.js:318:22)
  17. at global.Promise (/media/DarkHawk/srv/Databases/PremadeDB/Geonames/node_modules/pg/lib/client.js:410:23)
  18. at new Promise (<anonymous>)
  19. at Client.end (/media/DarkHawk/srv/Databases/PremadeDB/Geonames/node_modules/pg/lib/client.js:409:12)
  20. (node:31819) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
  21. (node:31819) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  22. (node:31819) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 end listeners added. Use emitter.setMaxListeners() to increase limit
  23.  
  24. var pg = require("pg");
  25. var fs = require('fs');
  26.  
  27. const pool = new pg.Pool({
  28. user: 'smurf',
  29. host: 'localhost',
  30. database: 'mydb',
  31. password: 'smurf',
  32. port: 5432,
  33. })
  34.  
  35. var filename = 'allCountries.txt';
  36.  
  37. var fs = require('fs'),
  38. es = require('event-stream');
  39.  
  40. var lineNr = 0;
  41. var max = 11784251; // Number of line, dirty, to get % of lines inserted
  42.  
  43. // Connect to Postgresql
  44. pool.connect((err, client, done) => {
  45. if (err) throw err
  46.  
  47. // Stream file line by line
  48. var s = fs.createReadStream(filename)
  49. .pipe(es.split())
  50. .pipe(es.mapSync(function(e) {
  51.  
  52. // pause the readstream
  53. s.pause();
  54.  
  55. lineNr += 1;
  56.  
  57. // Each line need to be properly formated
  58. e = e.split("t"); //TAB split
  59.  
  60. // The following fields need formating
  61. e[0] = parseInt(e[0]);
  62. e[4] = parseFloat(e[4]);
  63. e[5] = parseFloat(e[5]);
  64. e[14] = parseInt(e[14]);
  65.  
  66. e[15] = e[15] == '' ? 0 : e[15];
  67.  
  68. e[16] = parseInt(e[16]);
  69.  
  70. // Insert into db
  71. pool.query('INSERT INTO geonames.rawdata (geonameid, name, asciiname, alternatenames, latitude, longitude, fclass, fcode, country, cc2, admin1, admin2, admin3, admin4, population, elevation, gtopo30, timezone, moddate) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19);', e, function(err, result) {
  72. if (err) {
  73. console.log(err);
  74.  
  75. }
  76. done(); // Release this connection to the pool
  77. console.log("Line added ", lineNr, (lineNr / max * 100) + "%") // Monitor progress
  78. s.resume(); // Go to next line
  79.  
  80. });
  81.  
  82. })
  83. .on('error', function(err) {
  84. console.log('Error while reading file.', err);
  85. })
  86. .on('end', function() {
  87. console.log('Read entire file.')
  88. })
  89. );
  90. }) // END pool.connect
  91.  
  92. var fs = require('fs'),
  93. es = require('event-stream');
  94.  
  95. var lineNr = 0;
  96. var max = 11784251; // Number of line, dirty, to get % of lines inserted
  97.  
  98. // Connect to Postgresql
  99. pool.connect((err, client, done) => {
  100. if (err) throw err
  101.  
  102. // Stream file line by line
  103. var s = fs.createReadStream(filename)
  104. .pipe(es.split())
  105. .pipe(es.map(function(e, cb) {
  106.  
  107. lineNr += 1;
  108.  
  109. // Each line need to be properly formated
  110. e = e.split("t"); //TAB split
  111.  
  112. // The following fields need formating
  113. e[0] = parseInt(e[0]);
  114. e[4] = parseFloat(e[4]);
  115. e[5] = parseFloat(e[5]);
  116. e[14] = parseInt(e[14]);
  117.  
  118. e[15] = e[15] == '' ? 0 : e[15];
  119.  
  120. e[16] = parseInt(e[16]);
  121.  
  122. // Insert into db
  123. pool.query('INSERT INTO geonames.rawdata (geonameid, name, asciiname, alternatenames, latitude, longitude, fclass, fcode, country, cc2, admin1, admin2, admin3, admin4, population, elevation, gtopo30, timezone, moddate) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19);', e, function(err, result) {
  124. cb(err, result); // call the callback
  125. console.log("Line added ", lineNr, (lineNr / max * 100) + "%") // Monitor progress
  126. });
  127.  
  128. })
  129. .resume()
  130. .on('error', function(err) {
  131. done();
  132. console.log('Error while reading file.', err);
  133. })
  134. .on('end', function() {
  135. done();
  136. console.log('Read entire file.')
  137. })
  138. );
  139. }) // END pool.connect
  140.  
  141. const {Pool} = require("pg");
  142. const {StringStream} = require("scramjet");
  143. const fs = require("fs");
  144.  
  145. const pool = new Pool(options);
  146. const max = 11784251;
  147. const INSERT_ENTRY = 'INSERT INTO geonames.rawdata (geonameid, name, asciiname, alternatenames, latitude, longitude, fclass, fcode, country, cc2, admin1, admin2, admin3, admin4, population, elevation, gtopo30, timezone, moddate) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19);';
  148.  
  149. StringStream
  150. .from(fs.createReadStream(filename))
  151. .lines()
  152. .parse(line => {
  153. // Each line need to be properly formated
  154. const entry = line.split("t"); //TAB split
  155.  
  156. // The following fields need formating
  157. entry[0] = parseInt(entry[0]);
  158. entry[4] = parseFloat(entry[4]);
  159. entry[5] = parseFloat(entry[5]);
  160. entry[14] = parseInt(entry[14]);
  161. entry[15] = entry[15] == '' ? 0 : entry[15];
  162. entry[16] = parseInt(entry[16]);
  163.  
  164. return entry;
  165. })
  166. .setOptions({maxParallel: 32})
  167. .each(entry => {
  168. const client = await pool.connect();
  169. try {
  170. await client.query(INSERT_ENTRY, entry)
  171. } catch(err) {
  172. console.log('Error while adding line...', err);
  173. // some more logic could be here?
  174. } finally {
  175. client.release();
  176. }
  177. })
  178. .each(() => !(lineNr++ % 1000) && console.log("Line added ", lineNr, (lineNr / max * 100) + "%"))
  179. .run()
  180. .then(
  181. () => console.log('Read entire file.'),
  182. e => console.log('Error while handling file.', err)
  183. );
Add Comment
Please, Sign In to add comment