OrientDB is an Open Source NoSQL DBMS with both the features of Document and Graph DBMSs. It’s written in Java and it’s amazing fast: can store up to 150,000 records per second on common hardware.
とりあえず Eclipse にファイルやライブラリを配置して・・・。(./file/Tweets は OrientDB によってつくられた DB の物理ファイルです)
まずは CSV2OrientDB のローダをかきました。 TweetsLoader.groovy
import au.com.bytecode.opencsv.CSVReader
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx
import com.orientechnologies.orient.core.record.impl.ODocument
// define CSV header (exclude expanded_urls)
def header = [
"tweet_id"
,"in_reply_to_status_id"
,"in_reply_to_user_id"
,"retweeted_status_id"
,"retweeted_status_user_id"
,"timestamp"
,"source"
,"text"]
// load CSV
CSVReader reader = new CSVReader(new FileReader("./file/tweets.csv"))
List myEntries = reader.readAll()
reader.close()
// create OrientDB
ODatabaseDocumentTx db =
new ODatabaseDocumentTx("local:./file/Tweets").create()
// import CSV
myEntries.each {
ODocument doc = new ODocument("Tweet");
def index = 0
header.each { name ->
doc.field(name, it[index])
index++
}
doc.save()
}
// close OrientDB
db.close();
これを実行すると .csv が “local:./file/Tweets” DB に読み込まれます。スキーマレスなので適当にロードできます。お手軽。 🙂
でもって、できたデータベースにクエリーを発行します。 Groovy が入っているつぶやきを単純に like にて。 TweetsQuery.groogy
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx
import com.orientechnologies.orient.core.record.impl.ODocument
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery
// open OrientDB
ODatabaseDocumentTx db =
new ODatabaseDocumentTx(
"local:./file/Tweets").open("admin", "admin");
// query
List<ODocument> result = db.query(
new OSQLSynchQuery<ODocument>(
"select * from Tweet where text like '%Groovy%'"));
// output
result.each {
println it.field("text")
}
// close OrientDB
db.close();