目的:使的在IDEA中编辑代码,令代码实现mongodb运算,且转换较为便捷

  • 由实验2可知,运算环境的搭建亦需要对数据进行存储和计算,故需要实现类型转换,所以在实验2的基础上搭建环境。
  • 菜鸟教程可得到mongodb命令的具体格式等,如:新建集合

==》可以新建:

  1. case class CreateCollection(name:String,options:Options[Any]=None) //其他的mongodb命令也是照着这样建立的
  • 由org.mongodb.scala.model.Filters可知Filters内的相关方法的返回类型都是Bson。例子如下:

  1. /**
  2. * Creates a filter that matches all documents where the value of the given field is greater than the specified value.
  3. *
  4. * @param fieldName the field name
  5. * @param value the value
  6. * @tparam TItem the value type
  7. * @return the filter
  8. * @see [[http://docs.mongodb.org/manual/reference/operator/query/gt \$gt]]
  9. */
  10. def gt[TItem](fieldName: String, value: TItem): Bson = JFilters.gt(fieldName, value)
  11.  
  12. /**
  13. * Creates a filter that matches all documents where the value of the given field is less than the specified value.
  14. *
  15. * @param fieldName the field name
  16. * @param value the value
  17. * @tparam TItem the value type
  18. * @return the filter
  19. * @see [[http://docs.mongodb.org/manual/reference/operator/query/lt \$lt]]
  20. */
  21. def lt[TItem](fieldName: String, value: TItem): Bson = JFilters.lt(fieldName, value)

  

  • 根据相关语法格式,可新建

  1. case class Count(filter: Option[Bson], options: Option[Any])
  • 执行相关命令需要引入数据库和集合,新建MGOContext.scala
  1. case class MGOContext(
  2. dbName:String,
  3. collName:String,
  4. action:MGOCommands=null
  5. ){ctx=>
  6. def setDbName(name:String):MGOContext=ctx.copy(dbName=name)
  7.  
  8. def setCollName(name:String):MGOContext=ctx.copy(collName=name)
  9.  
  10. def setCommand(cmd:MGOCommands):MGOContext=ctx.copy(action=cmd)
  11. }
  12.  
  13. object MGOContext {
  14. def apply(db:String,coll:String) = new MGOContext(db,coll)
  15.  
  16. def apply(
  17. db: String,
  18. coll: String,
  19. action: MGOCommands = null
  20. ): MGOContext = new MGOContext(db, coll, action)
  21.  
  22. }

 

  • 分别新建相关的数据库操作,新建MGOCommands.scala和MGOAdmins.scala
  1. //MGOAdmins.scala
  2. import org.bson.conversions.Bson
  3.  
  4. object MGOAdmins {
  5. case class DropCollection(collName: String) extends MGOCommands
  6.  
  7. case class CreateCollection(collName: String, options: Option[Any] = None) extends MGOCommands
  8.  
  9. case class ListCollection(dbName: String) extends MGOCommands
  10.  
  11. case class CreateView(viewName: String, viewOn: String, pipeline: Seq[Bson], options: Option[Any] = None) extends MGOCommands
  12.  
  13. case class CreateIndex(key: Bson, options: Option[Any] = None) extends MGOCommands
  14.  
  15. case class DropIndexByName(indexName: String, options: Option[Any] = None) extends MGOCommands
  16.  
  17. case class DropIndexByKey(key: Bson, options: Option[Any] = None) extends MGOCommands
  18.  
  19. case class DropAllIndexes(options: Option[Any] = None) extends MGOCommands
  20.  
  21. }
  22.  
  23. //MGOCommands.scala
  24. import org.mongodb.scala.{Document, FindObservable}
  25. import org.mongodb.scala.bson.conversions.Bson
  26. import org.mongodb.scala.model.WriteModel
  27.  
  28. trait MGOCommands
  29.  
  30. object MGOCommands {
  31. case class Count(filter: Option[Bson], options: Option[Any]) extends MGOCommands
  32.  
  33. case class Distict(fieldName: String, filter: Option[Bson]) extends MGOCommands
  34.  
  35. case class Find[M](filter: Option[Bson] = None,
  36. andThen: Option[FindObservable[Document] => FindObservable[Document]]= None,
  37. converter: Option[Document => M] = None,
  38. firstOnly: Boolean = false) extends MGOCommands
  39.  
  40. case class Aggregate(pipeLine: Seq[Bson]) extends MGOCommands
  41.  
  42. case class MapReduce(mapFunction: String, reduceFunction: String) extends MGOCommands
  43.  
  44. case class Insert(newdocs: Seq[Document], options: Option[Any] = None) extends MGOCommands
  45.  
  46. case class Delete(filter: Bson, options: Option[Any] = None, onlyOne: Boolean = false) extends MGOCommands
  47.  
  48. case class Replace(filter: Bson, replacement: Document, options: Option[Any] = None) extends MGOCommands
  49.  
  50. case class Update(filter: Bson, update: Bson, options: Option[Any] = None, onlyOne: Boolean = false) extends MGOCommands
  51.  
  52. case class BulkWrite(commands: List[WriteModel[Document]], options: Option[Any] = None) extends MGOCommands
  53.  
  54. }
  • 综上所述,具体实现相关的操作
  1. import org.mongodb.scala.{Document, MongoClient}
  2. import org.mongodb.scala.model.{BulkWriteOptions, CountOptions, CreateCollectionOptions, CreateViewOptions, DeleteOptions, DropIndexOptions, IndexOptions, InsertManyOptions, InsertOneOptions, UpdateOptions}
  3.  
  4. import scala.concurrent.Future
  5.  
  6. object MGOEngine{
  7.  
  8. import MGOAdmins._
  9. import MGOCommands._
  10.  
  11. def DAO[T](ctx: MGOContext)(implicit client: MongoClient): Future[T] = {
  12. val db = client.getDatabase(ctx.dbName)
  13. val coll = db.getCollection(ctx.collName)
  14. ctx.action match {
  15. case Count(Some(filter), Some(opt)) =>
  16. coll.count(filter, opt.asInstanceOf[CountOptions])
  17. .toFuture().asInstanceOf[Future[T]]
  18. case Count(Some(filter), None) =>
  19. coll.count(filter).toFuture()
  20. .asInstanceOf[Future[T]]
  21. case Count(None, None) =>
  22. coll.count().toFuture()
  23. .asInstanceOf[Future[T]]
  24. /* distinct */
  25. case Distict(field, Some(filter)) =>
  26. coll.distinct(field, filter).toFuture()
  27. .asInstanceOf[Future[T]]
  28. case Distict(field, None) =>
  29. coll.distinct((field)).toFuture()
  30. .asInstanceOf[Future[T]]
  31. /* find */
  32. case Find(None, None, optConv, false) =>
  33. if (optConv == None) coll.find().toFuture().asInstanceOf[Future[T]]
  34. else coll.find().map(optConv.get).toFuture().asInstanceOf[Future[T]]
  35. case Find(None, None, optConv, true) =>
  36. if (optConv == None) coll.find().first().head().asInstanceOf[Future[T]]
  37. else coll.find().first().map(optConv.get).head().asInstanceOf[Future[T]]
  38. case Find(Some(filter), None, optConv, false) =>
  39. if (optConv == None) coll.find(filter).toFuture().asInstanceOf[Future[T]]
  40. else coll.find(filter).map(optConv.get).toFuture().asInstanceOf[Future[T]]
  41. case Find(Some(filter), None, optConv, true) =>
  42. if (optConv == None) coll.find(filter).first().head().asInstanceOf[Future[T]]
  43. else coll.find(filter).first().map(optConv.get).head().asInstanceOf[Future[T]]
  44. case Find(None, Some(next), optConv, _) =>
  45. if (optConv == None) next(coll.find[Document]()).toFuture().asInstanceOf[Future[T]]
  46. else next(coll.find[Document]()).map(optConv.get).toFuture().asInstanceOf[Future[T]]
  47. case Find(Some(filter), Some(next), optConv, _) =>
  48. if (optConv == None) next(coll.find[Document](filter)).toFuture().asInstanceOf[Future[T]]
  49. else next(coll.find[Document](filter)).map(optConv.get).toFuture().asInstanceOf[Future[T]]
  50. /* aggregate */
  51. case Aggregate(pline) => coll.aggregate(pline).toFuture().asInstanceOf[Future[T]]
  52. /* mapReduce */
  53. case MapReduce(mf, rf) => coll.mapReduce(mf, rf).toFuture().asInstanceOf[Future[T]]
  54. /* insert */
  55. case Insert(docs, Some(opt)) =>
  56. if (docs.size > 1) coll.insertMany(docs, opt.asInstanceOf[InsertManyOptions]).toFuture()
  57. .asInstanceOf[Future[T]]
  58. else coll.insertOne(docs.head, opt.asInstanceOf[InsertOneOptions]).toFuture()
  59. .asInstanceOf[Future[T]]
  60. case Insert(docs, None) =>
  61. if (docs.size > 1) coll.insertMany(docs).toFuture().asInstanceOf[Future[T]]
  62. else coll.insertOne(docs.head).toFuture().asInstanceOf[Future[T]]
  63. /* delete */
  64. case Delete(filter, None, onlyOne) =>
  65. if (onlyOne) coll.deleteOne(filter).toFuture().asInstanceOf[Future[T]]
  66. else coll.deleteMany(filter).toFuture().asInstanceOf[Future[T]]
  67. case Delete(filter, Some(opt), onlyOne) =>
  68. if (onlyOne) coll.deleteOne(filter, opt.asInstanceOf[DeleteOptions]).toFuture().asInstanceOf[Future[T]]
  69. else coll.deleteMany(filter, opt.asInstanceOf[DeleteOptions]).toFuture().asInstanceOf[Future[T]]
  70. /* replace */
  71. case Replace(filter, replacement, None) =>
  72. coll.replaceOne(filter, replacement).toFuture().asInstanceOf[Future[T]]
  73. case Replace(filter, replacement, Some(opt)) =>
  74. coll.replaceOne(filter, replacement, opt.asInstanceOf[UpdateOptions]).toFuture().asInstanceOf[Future[T]]
  75. /* update */
  76. case Update(filter, update, None, onlyOne) =>
  77. if (onlyOne) coll.updateOne(filter, update).toFuture().asInstanceOf[Future[T]]
  78. else coll.updateMany(filter, update).toFuture().asInstanceOf[Future[T]]
  79. case Update(filter, update, Some(opt), onlyOne) =>
  80. if (onlyOne) coll.updateOne(filter, update, opt.asInstanceOf[UpdateOptions]).toFuture().asInstanceOf[Future[T]]
  81. else coll.updateMany(filter, update, opt.asInstanceOf[UpdateOptions]).toFuture().asInstanceOf[Future[T]]
  82. /* bulkWrite */
  83. case BulkWrite(commands, None) =>
  84. coll.bulkWrite(commands).toFuture().asInstanceOf[Future[T]]
  85. case BulkWrite(commands, Some(opt)) =>
  86. coll.bulkWrite(commands, opt.asInstanceOf[BulkWriteOptions]).toFuture().asInstanceOf[Future[T]]
  87.  
  88. /* drop collection */
  89. case DropCollection(collName) =>
  90. val coll = db.getCollection(collName)
  91. coll.drop().toFuture().asInstanceOf[Future[T]]
  92. /* create collection */
  93. case CreateCollection(collName, None) =>
  94. db.createCollection(collName).toFuture().asInstanceOf[Future[T]]
  95. case CreateCollection(collName, Some(opt)) =>
  96. db.createCollection(collName, opt.asInstanceOf[CreateCollectionOptions]).toFuture().asInstanceOf[Future[T]]
  97. /* list collection */
  98. case ListCollection(dbName) =>
  99. client.getDatabase(dbName).listCollections().toFuture().asInstanceOf[Future[T]]
  100. /* create view */
  101. case CreateView(viewName, viewOn, pline, None) =>
  102. db.createView(viewName, viewOn, pline).toFuture().asInstanceOf[Future[T]]
  103. case CreateView(viewName, viewOn, pline, Some(opt)) =>
  104. db.createView(viewName, viewOn, pline, opt.asInstanceOf[CreateViewOptions]).toFuture().asInstanceOf[Future[T]]
  105. /* create index */
  106. case CreateIndex(key, None) =>
  107. coll.createIndex(key).toFuture().asInstanceOf[Future[T]]
  108. case CreateIndex(key, Some(opt)) =>
  109. coll.createIndex(key, opt.asInstanceOf[IndexOptions]).toFuture().asInstanceOf[Future[T]]
  110. /* drop index */
  111. case DropIndexByName(indexName, None) =>
  112. coll.dropIndex(indexName).toFuture().asInstanceOf[Future[T]]
  113. case DropIndexByName(indexName, Some(opt)) =>
  114. coll.dropIndex(indexName, opt.asInstanceOf[DropIndexOptions]).toFuture().asInstanceOf[Future[T]]
  115. case DropIndexByKey(key, None) =>
  116. coll.dropIndex(key).toFuture().asInstanceOf[Future[T]]
  117. case DropIndexByKey(key, Some(opt)) =>
  118. coll.dropIndex(key, opt.asInstanceOf[DropIndexOptions]).toFuture().asInstanceOf[Future[T]]
  119. case DropAllIndexes(None) =>
  120. coll.dropIndexes().toFuture().asInstanceOf[Future[T]]
  121. case DropAllIndexes(Some(opt)) =>
  122. coll.dropIndexes(opt.asInstanceOf[DropIndexOptions]).toFuture().asInstanceOf[Future[T]]
  123. }
  124.  
  125. }}

  

Scala与Mongodb实践3-----运算环境的搭建的更多相关文章

  1. Scala与Mongodb实践4-----数据库操具体应用

    目的:在实践3中搭建了运算环境,这里学会如何使用该环境进行具体的运算和相关的排序组合等. 由数据库mongodb操作如find,aggregate等可知它们的返回类型是FindObservable.A ...

  2. Scala与Mongodb实践2-----图片、日期的存储读取

    目的:在IDEA中实现图片.日期等相关的类型在mongodb存储读取 主要是Scala和mongodb里面的类型的转换.Scala里面的数据编码类型和mongodb里面的存储的数据类型各个不同.存在类 ...

  3. 转】[1.0.2] 详解基于maven管理-scala开发的spark项目开发环境的搭建与测试

    场景 好的,假设项目数据调研与需求分析已接近尾声,马上进入Coding阶段了,辣么在Coding之前需要干马呢?是的,“统一开发工具.开发环境的搭建与本地测试.测试环境的搭建与测试” - 本文详细记录 ...

  4. Scala与Mongodb实践1-----mongodbCRUD

    目的:如何使用MongoDB之前提供有关Scala驱动程序及其异步API. 1.现有条件 IDEA中的:Scala+sbt+SDK mongodb-scala-driver的网址:http://mon ...

  5. 使用Scala操作Mongodb

    介绍 Scala是一种功能性面向对象语言.它融汇了很多前所未有的特性.而同一时候又执行于JVM之上.随着开发人员对Scala的兴趣日增,以及越来越多的工具支持,无疑Scala语言将成为你手上一件不可缺 ...

  6. SDP(6):分布式数据库运算环境- Cassandra-Engine

    现代信息系统应该是避不开大数据处理的.作为一个通用的系统集成工具也必须具备大数据存储和读取能力.cassandra是一种分布式的数据库,具备了分布式数据库高可用性(high-availability) ...

  7. Scala对MongoDB的增删改查操作

    =========================================== 原文链接: Scala对MongoDB的增删改查操作 转载请注明出处! ==================== ...

  8. Windows 7上安装配置TensorFlow-GPU运算环境

    Windows 7上安装配置TensorFlow-GPU运算环境 1. 概述 在深度学习实践中,对于简单的模型和相对较小的数据集,我们可以使用CPU完成建模过程.例如在MNIST数据集上进行手写数字识 ...

  9. 实验 2 Scala 编程初级实践

    实验 2 Scala 编程初级实践 一.实验目的 1.掌握 Scala 语言的基本语法.数据结构和控制结构: 2.掌握面向对象编程的基础知识,能够编写自定义类和特质: 3.掌握函数式编程的基础知识,能 ...

随机推荐

  1. 彻底解决tensorflow:ImportError: Could not find 'cudart64_90.dll' tensorflow安装

    今天装tensorflow-gpu出现了很多问题 1.pip install tensorflow-gpu下载过慢 解决办法可查看 Python机器学习常用模块 2.安装完tensorflow以后,运 ...

  2. CodeForces 1243"Character Swap (Hard Version)"(multimap)

    传送门 •前置知识-multimap的用法 $multimap$ 与 $map$ 的区别在于一个 $key$ 可以对应几个值: 对于 $map$ 而言,一个 $key$ 只能对应一个值,并且按照 $k ...

  3. 【u224】传送机

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 刷完牙洗完脸,黄黄同学就要上课去了.可是黄黄同学每次去上课时总喜欢把校园里面的每条路都走一遍,当然,黄 ...

  4. linux 安装一个共享的处理者

    共享中断通过 request_irq 来安装就像不共享的一样, 但是有 2 个不同: SA_SHIRQ 位必须在 flags 参数中指定, 当请求中断时. dev_id 参数必须是独特的. 任何模块地 ...

  5. python写的有声小说爬虫

    querybook.py from bs4 import BeautifulSoup from lxml import html import xml import requests import s ...

  6. 【62.89%】【BZOJ 1072】[SCOI2007]排列perm

    Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 1862  Solved: 1171 [Submit][Status][Discuss] Descri ...

  7. C# 强转空会不会出现异常

    有小伙伴问我强转 null 会不会出现异常,我告诉他,如果是引用类型那么不会,如果是值类型,那么会出现空异常 如果是引用类型,只要是空类型,是支持随意转换,如下面代码,这是可以运行 class Pro ...

  8. FineReport报表和水晶报表的比较

    FineReport报表和水晶报表的比较 FineReport报表软件针对复杂格式的报表数据及Web报表的展现,通过多源分片.不规则分组.双向扩展来轻松拖拽做复杂格式的报表,制作报表从此摆脱了复杂的S ...

  9. JDK源码系列(一) ------ 深入理解SPI机制

    什么是SPI机制 最近我建了另一个文章分类,用于扩展JDK中一些重要但不常用的功能. SPI,全名Service Provider Interface,是一种服务发现机制.它可以看成是一种针对接口实现 ...

  10. cocos2dx Vec2

    //SE是坐标重叠部分 // returns true if segment A-B intersects with segment C-D. S->E is the overlap part ...