将使用Node.js和StreamingAPI获取到的Tweet流式传输到MongoDB中
我将之前已经执行的推特获取流入MongoDB数据库中。
我将使用Mongoose模块。
猫鼬
这是在使用Node.js时用于使用MongoDB的模块。比MongoDB官方模块更受欢迎。
使用Mongoose的Node.js和MongoDB连接示例。
我先试着写一个尽量简洁的版本。
$ npm install mongoose
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/n0bisuke');
var Project = mongoose.model('Project', { name: String });
var project = new Project({ name: 'ligblog' });
project.save(function (err) {
if (err) //失敗時
console.log('保存成功');
});
参考 QuickStart
http://mongoosejs.com/docs/guide.html
var DB_NAME = 'myapp';
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/'+DB_NAME);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log('connection success');
});
var Schema = mongoose.Schema;
var TwitterSchema = new Schema({
body: {
text: String,
url: String,
image: String,
location: {
lat: String,
lon: String
}
},
user: {
twitter_id: String,
name: String,
screen_name: String,
description: String,
images: String,
location: {
lat: String,
lon: String
}
},
create_at: { type: Date, default: Date.now }
});
mongoose.model('TwModel', TwitterSchema);
var TwModel = mongoose.model('TwModel');
var twmodel = new TwModel();
twmodel.user.name = 'n0bisuke';
twmodel.save(function (err, data) {
if (err) return console.error(err);
console.log(data);
});
执行
$ node mongoose.js
connection success
{ __v: 0,
_id: 547c5e89993ef9e1cd5f7c39,
create_at: Mon Dec 01 2014 21:26:49 GMT+0900 (JST),
user: { name: 'n0bisuke', location: {} },
body: { location: {} } }
通过TwitterStreamingAPI传输数据
正如往常一样,我只是在尝试样本,所以没有进行文件分类等操作。呵呵。
var DB_NAME = 'myapp';
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/'+DB_NAME);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
console.log('connection success');
});
var Schema = mongoose.Schema;
var TwitterSchema = new Schema({
content: {
text: String,
url: String,
image: String,
location: {
lat: String,
lon: String
}
},
user: {
twitter_id: String,
name: String,
screen_name: String,
description: String,
images: String,
location: {
lat: String,
lon: String
}
},
create_at: { type: Date, default: Date.now }
});
mongoose.model('TwModel', TwitterSchema);
/*twitter設定*/
var twitter = require('twitter');
var twit = new twitter({
consumer_key: 'xx',
consumer_secret: 'xx',
access_token_key: 'xx',
access_token_secret: 'xx'
});
/**/
var TwModel = mongoose.model('TwModel');
var keyword = process.argv[2]; //第一引数
var option = {'track': keyword};
console.log(keyword+'を含むツイートを取得します。');
twit.stream('statuses/filter', option, function(stream) {
stream.on('data', function (data){
if(data.lang === 'ja'){
var twmodel = new TwModel();
twmodel.content = {
text: data.text
};
twmodel.user = {
screen_name: data.user.screen_name
};
twmodel.save(function (err, data) {
if (err) return console.error(err);
console.log(data);
});
}
});
});
我会以同样的方式执行,每次获取推特数据时都会将数据流入MongoDB中。