Mongodb 和 Scala 的入门指南

fmms 13年前
     <p>MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。他支持的数据结构非常松散,是类似 json的bjson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是他支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几 乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。</p>    <p>在本文中,我们将介绍使用 Scala 语言如何与 Mongodb 进行连接与数据处理。</p>    <p>首先需要安装 Mongodb,很多地方都有介绍,这里不再累赘。一旦 Mongodb 安装完毕便可以启动:</p>    <pre class="brush:shell; toolbar: true; auto-links: false;">vikas@vikas-laptop:~/w/software/archive/mongodb-linux-i686-2.0.2/bin$ mongod mongod --help for help and startup options Sat Feb  4 23:19:33  Sat Feb  4 23:19:33 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability. Sat Feb  4 23:19:33 </pre>    <p>上面是启动 Mongodb 服务器,为了测试安装是否成功,我们可以打开另外一个终端并执行下面命令:</p>    <pre class="brush:shell; toolbar: true; auto-links: false;">vikas@vikas-laptop:~/w/software/archive/mongodb-linux-i686-2.0.2/bin$ mongo MongoDB shell version: 2.0.2 connecting to: test > show dbs; local 0.0625GB test 0.0625GB > </pre>    <p></p>    <p>这样我们就可以写程序连接到 Mongodb 了,接下来我们编写一个 Scala 程序。在开始之前需要了解,Scala 需要一个 Mongodb 的驱动包 —— <a href="/misc/goto?guid=4959518065122802202" rel="nofollow" target="_blank">Casbah</a></p>    <p>如果你正在使用 sbt 0.11x,那么你需要执行下面步骤以便将该驱动包含到 build.scala 中。</p>    <pre class="brush:scala; toolbar: true; auto-links: false;">object ApplicationBuild extends Build {      val appName         = "MyCoolApp"     val appVersion      = "1.0"      val appDependencies = Seq(       "com.mongodb.casbah" %% "casbah" % "2.1.5-1",       // Add your project dependencies here,     )      val main = PlayProject(appName, appVersion, appDependencies).settings(defaultScalaSettings:_*).settings(       // Add your own project settings here           ) }</pre>    <p>一旦引入该驱动,就可以写一个简单的应用来访问 Mongodb 数据库:</p>    <pre class="brush:scala; toolbar: true; auto-links: false;">import com.mongodb.casbah.Imports._  object MongoTest extends App {    connectToMongo    def connectToMongo {     val mongoConn = MongoConnection()     val mongoColl = mongoConn("test")("vikas")     val bread1 = MongoDBObject("name" -> "parrys",       "price" -> "10 INR")     val bread2 = MongoDBObject("name" -> "breadAndMore")     mongoColl += bread1     mongoColl += bread2     mongoColl.find()      for { x <- mongoColl } yield println("value "+ x)   } }</pre>    <p></p>    <p>上面这个小程序主要执行以下几个步骤:</p>    <p>1. 连接到名为 test 的数据库的 vikas 集合(相当于SQL数据库中的表)</p>    <p>2. 创建一个 MongoCollection 并添加 MongoDBObjects 到集合中:mongoColl += bread1. 这一步相当于往表中插入文档,接下来查找刚插入的文档。</p>    <p>执行结果:</p>    <pre class="brush:shell; toolbar: true; auto-links: false;">23:34:41.072 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Registering Scala Conversions. 23:34:41.076 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Deserializers for Scala Conversions registering 23:34:41.077 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Serializers for Scala Conversions registering 23:34:41.078 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Setting up OptionSerializer 23:34:41.114 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Setting up ScalaJCollectionSerializer 23:34:41.158 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Setting up ScalaRegexSerializers 23:34:41.159 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Hooking up scala.util.matching.Regex serializer 23:34:41.160 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Reached base registration method on MongoConversionHelper. 23:34:41.626 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Registering Scala Conversions. 23:34:41.626 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Deserializers for Scala Conversions registering 23:34:41.626 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Serializers for Scala Conversions registering 23:34:41.626 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Setting up OptionSerializer 23:34:41.626 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Setting up ScalaJCollectionSerializer 23:34:41.626 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Setting up ScalaRegexSerializers 23:34:41.626 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Hooking up scala.util.matching.Regex serializer 23:34:41.626 [main] DEBUG c.m.c.c.c.s.RegisterConversionHelpers$ - Reached base registration method on MongoConversionHelper. value { "_id" : { "$oid" : "4f2d733944ae57332d00eb55"} , "name" : "parrys" , "price" : "10 INR"} value { "_id" : { "$oid" : "4f2d733944ae57332d00eb56"} , "name" : "breadAndMore"}</pre>    <p>如果我们返回终端可以通过下面命令来查询刚插入的文档:</p>    <pre class="brush:shell; toolbar: true; auto-links: false;">vikas@vikas-laptop:~/w/software/archive/mongodb-linux-i686-2.0.2/bin$ mongo MongoDB shell version: 2.0.2 connecting to: test > use test switched to db test > db["vikas"].find() { "_id" : ObjectId("4f2d733944ae57332d00eb55"), "name" : "parrys", "price" : "10 INR" } { "_id" : ObjectId("4f2d733944ae57332d00eb56"), "name" : "breadAndMore" } > </pre>    <p>另外有一个 MongoDB 的 ORM 框架 for Scala ,详情请看 <a href="/misc/goto?guid=4959518065202806301" rel="nofollow" target="_blank">salat</a> </p>    <p>文章译自:<a href="/misc/goto?guid=4959518065394118056" rel="nofollow" target="_blank">knoldus.wordpress.com</a></p>    <p></p>