PageRenderTime 34ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/scala/demos/src/main/scala/demoMongoDb.scala

http://findataweb.googlecode.com/
Scala | 166 lines | 67 code | 74 blank | 25 comment | 1 complexity | c85e57ed1149ebe0660909c1b11ce5a6 MD5 | raw file
  1. /* Play with the MongoDb from scala
  2. * http://novus.github.com/docs/casbah/sphinx/html/intro/getting_started.html
  3. *
  4. * Start mongo with: >mongod --port 8645
  5. * Stop it with Ctrl+C
  6. */
  7. // use casbah
  8. :cp Documents/findataweb/scala/demos/lib/mongo-2.5.1.jar
  9. :cp Documents/findataweb/scala/demos/lib/casbah-core_2.8.1-2.1.0.jar
  10. :cp Documents/findataweb/scala/demos/lib/casbah-commons_2.8.1-2.1.0.jar
  11. :cp Documents/findataweb/scala/demos/lib/casbah-query_2.8.1-2.1.0.jar
  12. :cp Documents/findataweb/scala/demos/lib/joda-time-1.6.jar
  13. :cp Documents/findataweb/scala/demos/lib/scalaj-collection_2.8.0-1.0.jar
  14. :cp Documents/findataweb/scala/demos/lib/slf4j-api-1.6.1.jar
  15. :cp Documents/findataweb/scala/demos/lib/slf4j-jdk14-1.6.1.jar
  16. import com.mongodb.casbah.Imports._
  17. // connect to "localhost" host, port 8645
  18. val mongoConn = MongoConnection()
  19. val newObj = MongoDBObject("foo" -> "bar",
  20. "x" -> "y",
  21. "pie" -> 3.14,
  22. "spam" -> "eggs")
  23. newObj += "OMG!" -> "Ponies!"
  24. // works but now what?!
  25. //###################################################################
  26. //###################################################################
  27. :cp Documents/findataweb/scala/demos/lib/mongo-2.3.jar
  28. :cp Documents/findataweb/scala/demos/lib/mongo-scala-driver_2.8.0-0.8.6.jar
  29. import com.osinka.mongodb._
  30. import com.osinka.mongodb.shape._
  31. val db = mongo.getDB("mydb")
  32. val dbCollection = db.getCollection("my_objects")
  33. val myObjects = dboCollection of MyObjectShape
  34. // use straight java
  35. :cp Documents/findataweb/scala/demos/lib/mongo-2.3.jar
  36. val mongoConn = new com.mongodb.Mongo("127.0.0.1", 8645) // works
  37. // use casbah
  38. :cp Documents/findataweb/scala/demos/lib/mongo-2.3.jar
  39. :cp Documents/findataweb/scala/demos/lib/casbah_2.8.0-1.0.8.5.jar
  40. import com.novus.casbah.mongodb.Imports._
  41. // connect to "localhost" host, port 8645
  42. val mongoConn = MongoConnection("127.0.0.1", 8645) // closes up my scala!
  43. //val dir = "./Documents"
  44. val dir = "H:/user/R/Adrian"
  45. val filename = dir + "/findataweb/scala/demos/src/main/scala/airquality.csv"
  46. val src = Source.fromFile(filename)
  47. val iter = src.getLines().map(_.split(","))
  48. var row = iter.next
  49. //row.asInstanceOf[Array[Double]] does not work!
  50. val aux = row.map(_.toDouble)
  51. case class Row(Ozone: Double, SolarR: Double, Wind: Double,
  52. Temp: Double, Month: Double, Day: Double)
  53. val r1 = Row(aux(0), aux(1), aux(2), aux(3), aux(4), aux(5))
  54. /* Read a csv file with scala using opencsv
  55. * :cp ../../../lib/opencsv-2.2.jar
  56. */
  57. //import scala.io.Source
  58. import au.com.bytecode.opencsv.CSVReader // from opencsv.sf.net
  59. import java.io.FileReader // for CSVReader
  60. //import java.io.BufferedOutputStream
  61. //import java.text.DecimalFormat
  62. def ReadCsv(filename, header=true, skip=0): List[T] = {
  63. //val output = new BufferedOutputStream(System.out)
  64. //val formatter = new DecimalFormat("0.00")
  65. val dir = "./Documents/findataweb/scala/demos/src/test/scala/"
  66. val filename = dir + "airquality.csv"
  67. val filename = "airquality.csv"
  68. val reader = new CSVReader(new FileReader(filename))
  69. // reads everything as a java List of Array[String]
  70. val contents = reader.readAll().toArray
  71. scala.Double.NaN
  72. if (header){
  73. val header = reader.readNext()
  74. }
  75. val row = reader.readNext()
  76. def readRows(reader: CSVReader) {
  77. reader.readNext() match {
  78. case null =>
  79. output.flush
  80. return
  81. case columns =>
  82. output.write(
  83. (
  84. columns(0) + " is " + formatter.format(
  85. columns(1).toDouble * columns(2).toDouble
  86. ) + "\n"
  87. ).getBytes
  88. )
  89. readRows(reader)
  90. }
  91. }
  92. }