2017-06-27 19 views
1

我有一個使用NodeJS和PostgreSQL的奇怪錯誤,我希望你能幫助我。nodeJS將數據插入到PostgreSQL中的錯誤

我有大量的數據集,大約2百萬條我想插入到我的數據庫中。

一個數據由4列:

id: string, 
points: float[][] 
mid: float[] 
occurences: json[] 

我插入數據像這樣:

let pgp = require('pg-promise')(options); 
let connectionString = 'postgres://archiv:[email protected]:5432/fotoarchivDB'; 
let db = pgp(connectionString); 

cityNet.forEach((arr) => { 
    db 
    .none(
     "INSERT INTO currentcitynet(id,points,mid,occurences) VALUES $1", 
     Inserts("${id},${points}::double precision[],${mid}::double precision[],${occurences}::json[]",arr)) 
    .then(data => { 
     //success 
    }) 
    .catch(error => { 
     console.log(error); 
     //error 
    }); 
}) 

function Inserts(template, data) { 
    if (!(this instanceof Inserts)) { 
     return new Inserts(template, data); 
    } 
    this._rawDBType = true; 
    this.formatDBType = function() { 
    return data.map(d => "(" + pgp.as.format(template, d) + ")").join(","); 
}; 

這工作了整整第一個309248個的數據塊,然後突然它只是出現了錯誤(看起來好像)下面的數據試圖插入:

{ error: syntax error at end of input 
at Connection.parseE (/home/christian/Masterarbeit_reworked/projekt/server/node_modules/pg-promise/node_modules/pg/lib/connection.js:539:11) 
at Connection.parseMessage (/home/christian/Masterarbeit_reworked/projekt/server/node_modules/pg-promise/node_modules/pg/lib/connection.js:366:17) 
at Socket.<anonymous> (/home/christian/Masterarbeit_reworked/projekt/server/node_modules/pg-promise/node_modules/pg/lib/connection.js:105:22) 
at emitOne (events.js:96:13) 
at Socket.emit (events.js:188:7) 
at readableAddChunk (_stream_readable.js:176:18) 
at Socket.Readable.push (_stream_readable.js:134:10) 
at TCP.onread (net.js:548:20) 
name: 'error', 
length: 88, 
severity: 'ERROR', 
code: '42601', 
detail: undefined, 
hint: undefined, 
position: '326824', 
internalPosition: undefined, 
internalQuery: undefined, 
where: undefined, 
schema: undefined, 
table: undefined, 
column: undefined, 
dataType: undefined, 
constraint: undefined, 
file: 'scan.l', 
line: '1074', 
routine: 'scanner_yyerror' } 

每個迭代錯誤消息的'位置'條目都會更改。

我可以重做,它會在309248條目後總是出錯。 當我嘗試插入較少,如1000條目時,不會發生錯誤。

這真讓我困惑。我認爲PostgreSQL沒有任何最大數量的行。此外,錯誤信息根本無法幫助我。

解決 被發現的錯誤。在我的數據中有「空」條目滑入它。過濾掉空數據。 由於目前的方法有效,我會嘗試插入數據的其他建議,但性能非常糟糕。

+0

您是否正在嘗試一次插入兩億條記錄? Node.js將無法處理該問題。插頁必須正確分頁。另外,不要使用這種舊的方法來生成插入,請使用如下所示的插入:https:// stackoverflow。com/questions/37300997/multi-row-insert-with-pg-promise –

回答

1

我不知道,但它看起來像你的最後一個元素(309249)得到了錯誤的數據結構和PostgreSQL無法解析某些屬性

+0

這似乎是正確的。不知何故最後一個元素成爲「空」 我會找出爲什麼空被添加,刪除它,然後再試一次 – Christian

+0

你是對的。它修復了錯誤。謝謝。 – Christian

+0

太好了。歡迎 –

0

我的pg-promise作者。您的整個方法應改爲下面的方法。

有道通過pg-promise做大量的插入:

const pgp = require('pg-promise')({ 
    capSQL: true 
}); 

const db = pgp(/*connection details*/); 

var cs = new pgp.helpers.ColumnSet([ 
    'id', 
    {name: 'points', cast: 'double precision[]'}, 
    {name: 'mid', cast: 'double precision[]'}, 
    {name: 'occurences', cast: 'json[]'} 
], {table: 'currentcitynet'}); 

function getNextInsertBatch(index) { 
    // retrieves the next data batch, according to the index, and returns it 
    // as an array of objects. A normal batch size: 1000 - 10,000 objects, 
    // depending on the size of the objects. 
    // 
    // returns null when there is no more data left. 
} 

db.tx('massive-insert', t => { 
    return t.sequence(index => { 
     const data = getNextInsertBatch(index); 
     if (data) { 
      const inserts = pgp.helpers.insert(data, cs); 
      return t.none(inserts); 
     } 
    }); 
}) 
    .then(data => { 
     console.log('Total batches:', data.total, ', Duration:', data.duration); 
    }) 
    .catch(error => { 
     console.log(error); 
    }); 

UPDATE

如果getNextInsertBatch只能拿到數據不同步,然後從它返回相應承諾,並更新sequence->source回調:

return t.sequence(index => { 
    return getNextInsertBatch(index) 
     .then(data => { 
      if (data) { 
       const inserts = pgp.helpers.insert(data, cs); 
       return t.none(inserts); 
      } 
     }); 
}); 

相關鏈接:

+0

哇,我會試試看,並給我的反饋。 – Christian

+0

@Christian這裏描述也將是比較容易診斷和修正是正確的做法,因爲類型'ColumnSet'會報警,當你遇到一個'null'。另外,性能明顯更好。 –

+0

@Christian是怎麼回事? ;) –

相關問題