【发布时间】:2019-01-19 14:58:37
【问题描述】:
我已下载 Geonames 数据库转储,并尝试将所有内容放入 postgresql 表中,但无论我尝试什么,都会遇到多个错误。
我进行的最后修改得到以下内容:
Error: Connection terminated by user
at Client.end (/media/DarkHawk/srv/Databases/PremadeDB/Geonames/node_modules/pg/lib/client.js:402:36)
at Pool._remove (/media/DarkHawk/srv/Databases/PremadeDB/Geonames/node_modules/pg-pool/index.js:135:12)
at Timeout.setTimeout (/media/DarkHawk/srv/Databases/PremadeDB/Geonames/node_modules/pg-pool/index.js:38:12)
at ontimeout (timers.js:498:11)
at tryOnTimeout (timers.js:323:5)
at Timer.listOnTimeout (timers.js:290:5)
Line added 6052 0.05135667935111022%
(node:31819) UnhandledPromiseRejectionWarning: Error: This socket is closed
at Socket._writeGeneric (net.js:729:18)
at Socket._write (net.js:783:8)
at doWrite (_stream_writable.js:397:12)
at writeOrBuffer (_stream_writable.js:383:5)
at Socket.Writable.write (_stream_writable.js:290:11)
at Socket.write (net.js:707:40)
at Connection.end (/media/DarkHawk/srv/Databases/PremadeDB/Geonames/node_modules/pg/lib/connection.js:318:22)
at global.Promise (/media/DarkHawk/srv/Databases/PremadeDB/Geonames/node_modules/pg/lib/client.js:410:23)
at new Promise (<anonymous>)
at Client.end (/media/DarkHawk/srv/Databases/PremadeDB/Geonames/node_modules/pg/lib/client.js:409:12)
(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)
(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.
(node:31819) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 end listeners added. Use emitter.setMaxListeners() to increase limit
我的代码是:
var pg = require("pg");
var fs = require('fs');
const pool = new pg.Pool({
user: 'smurf',
host: 'localhost',
database: 'mydb',
password: 'smurf',
port: 5432,
})
var filename = 'allCountries.txt';
var fs = require('fs'),
es = require('event-stream');
var lineNr = 0;
var max = 11784251; // Number of line, dirty, to get % of lines inserted
// Connect to Postgresql
pool.connect((err, client, done) => {
if (err) throw err
// Stream file line by line
var s = fs.createReadStream(filename)
.pipe(es.split())
.pipe(es.mapSync(function(e) {
// pause the readstream
s.pause();
lineNr += 1;
// Each line need to be properly formated
e = e.split("\t"); //TAB split
// The following fields need formating
e[0] = parseInt(e[0]);
e[4] = parseFloat(e[4]);
e[5] = parseFloat(e[5]);
e[14] = parseInt(e[14]);
e[15] = e[15] == '' ? 0 : e[15];
e[16] = parseInt(e[16]);
// Insert into db
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) {
if (err) {
console.log(err);
}
done(); // Release this connection to the pool
console.log("Line added ", lineNr, (lineNr / max * 100) + "%") // Monitor progress
s.resume(); // Go to next line
});
})
.on('error', function(err) {
console.log('Error while reading file.', err);
})
.on('end', function() {
console.log('Read entire file.')
})
);
}) // END pool.connect
我尝试了 ReadFile、ReadFileSync、readline 扩展。移动或省略 done() 函数或只是移动它。
我通常使用php插入大量文件,所以我不知道我在这里做错了什么。
MaxListenersExceededWarning 错误对我来说毫无意义,因为我似乎关闭了所有打开的内容。我在这里做错了什么?
谢谢!
【问题讨论】:
-
您是否尝试过更改默认的最大侦听器?我认为有一个限制,但我从来没有遇到过这个问题。 Ref
-
我不明白为什么在我关闭所有内容时会达到最大听众数。我糊涂了。使用
require('events').EventEmitter.defaultMaxListeners = 40;更改最大侦听器(来自:stackoverflow.com/questions/8313628/…)不会让我在这个过程中走得更远(~6500 行) -
这是因为您的 pg 数据库中充斥着使用
mapSync的条目。它的作用是创建插入,但不等待那些完成。尝试改用es.map并在插入数据后使用回调。您还可以使用我的scramjet并将 pg 与 es6 async/await 代码一起使用 - 它应该更容易。 This sample 显示了类似的情况。 -
@MichałKapracki 我忘了告诉你你是对的,很明显:) 非常感谢你的帮助。
-
嗯...我可以回答...
标签: node.js postgresql geonames