我有一个包含 json 格式数据的文件。数据很奇怪,因为这些行具有一组属性的不同子集,这些属性并非都是事先知道的(随着时间的推移,它们会逐渐增加)。例如:
[{"firstName":"Joe","education":"highschool","sex":"male"},
{"lastName":"Edwards","address":"ohio","sex":"male"},
{"favoriteSport":"cycling","bicycle":"raleigh","ownsBoat":"yes","ownsDog":"yes","ownsHouse":"yes"}]
文件中已经存在大量数据,因此我想将其导入到 couchdb 中,而不是逐项输入数据。我按照此处帖子中的程序进行操作,但是在创建数据库时,它是空的。我用了:
curl -X PUT -d @../Data/data.json http://127.0.0.1:5984/test_import
更新:由于我正在使用 nodejs(新手),我想我会尝试使用“摇篮”。我的想法是将数据作为数组导入,并使用'cradle's dbsave() 批量加载。但使用以下内容:
var fs = require('fs');
var cradle = require('cradle');
var data = fs.readFile( '../Data/data.json', function (err, data) {
if (err) {
throw err;
}
.
.
.
makeDB(bigdata,'test_import'); // where 'bigdata' is an array of json objects/couchdb 'documents'
});
function makeDB (p,filename) {
var db = new(cradle.Connection)().database(filename);
console.log(db);
db.save(p, function(err,res) {
if (err) {
console.log(err);
} else {
console.log('Success!');
}
});
};
后者似乎工作!创建并填充了一个数据库,但它确实引发了以下错误:
k:\nodejs\node_modules\cradle\lib\cradle.js:198
return callback(body);
^
TypeError: undefined is not a function
OR
k:\nodejs\node_modules\cradle\lib\cradle.js:201
callback(null, self.options.raw ? body : new cradle.Response(body, res
^
TypeError: undefined is not a function
at Request.cradle.Connection.request [as _callback] (k:\nodejs\node_modules\cradle\lib\cradle.
js:201:9)
at Request.init.self.callback (k:\nodejs\node_modules\request\main.js:120:22)
at Request.EventEmitter.emit (events.js:91:17)
at Request.<anonymous> (k:\nodejs\node_modules\request\main.js:633:16)
at Request.EventEmitter.emit (events.js:88:17)
at IncomingMessage.Request.start.self.req.self.httpModule.request.buffer (k:\nodejs\node_modul
es\request\main.js:595:14)
at IncomingMessage.EventEmitter.emit (events.js:115:20)
at IncomingMessage._emitEnd (http.js:366:10)
at HTTPParser.parserOnMessageComplete [as onMessageComplete] (http.js:149:23)
at Socket.socketOnData [as ondata] (http.js:1366:20)