/scala/demos/src/main/scala/demoMongoDb.scala
Scala | 166 lines | 67 code | 74 blank | 25 comment | 1 complexity | c85e57ed1149ebe0660909c1b11ce5a6 MD5 | raw file
- /* Play with the MongoDb from scala
- * http://novus.github.com/docs/casbah/sphinx/html/intro/getting_started.html
- *
- * Start mongo with: >mongod --port 8645
- * Stop it with Ctrl+C
- */
- // use casbah
- :cp Documents/findataweb/scala/demos/lib/mongo-2.5.1.jar
- :cp Documents/findataweb/scala/demos/lib/casbah-core_2.8.1-2.1.0.jar
- :cp Documents/findataweb/scala/demos/lib/casbah-commons_2.8.1-2.1.0.jar
- :cp Documents/findataweb/scala/demos/lib/casbah-query_2.8.1-2.1.0.jar
- :cp Documents/findataweb/scala/demos/lib/joda-time-1.6.jar
- :cp Documents/findataweb/scala/demos/lib/scalaj-collection_2.8.0-1.0.jar
- :cp Documents/findataweb/scala/demos/lib/slf4j-api-1.6.1.jar
- :cp Documents/findataweb/scala/demos/lib/slf4j-jdk14-1.6.1.jar
- import com.mongodb.casbah.Imports._
- // connect to "localhost" host, port 8645
- val mongoConn = MongoConnection()
- val newObj = MongoDBObject("foo" -> "bar",
- "x" -> "y",
- "pie" -> 3.14,
- "spam" -> "eggs")
- newObj += "OMG!" -> "Ponies!"
- // works but now what?!
- //###################################################################
- //###################################################################
- :cp Documents/findataweb/scala/demos/lib/mongo-2.3.jar
- :cp Documents/findataweb/scala/demos/lib/mongo-scala-driver_2.8.0-0.8.6.jar
- import com.osinka.mongodb._
- import com.osinka.mongodb.shape._
- val db = mongo.getDB("mydb")
- val dbCollection = db.getCollection("my_objects")
- val myObjects = dboCollection of MyObjectShape
- // use straight java
- :cp Documents/findataweb/scala/demos/lib/mongo-2.3.jar
- val mongoConn = new com.mongodb.Mongo("127.0.0.1", 8645) // works
- // use casbah
- :cp Documents/findataweb/scala/demos/lib/mongo-2.3.jar
- :cp Documents/findataweb/scala/demos/lib/casbah_2.8.0-1.0.8.5.jar
- import com.novus.casbah.mongodb.Imports._
- // connect to "localhost" host, port 8645
- val mongoConn = MongoConnection("127.0.0.1", 8645) // closes up my scala!
- //val dir = "./Documents"
- val dir = "H:/user/R/Adrian"
- val filename = dir + "/findataweb/scala/demos/src/main/scala/airquality.csv"
- val src = Source.fromFile(filename)
- val iter = src.getLines().map(_.split(","))
- var row = iter.next
- //row.asInstanceOf[Array[Double]] does not work!
- val aux = row.map(_.toDouble)
- case class Row(Ozone: Double, SolarR: Double, Wind: Double,
- Temp: Double, Month: Double, Day: Double)
- val r1 = Row(aux(0), aux(1), aux(2), aux(3), aux(4), aux(5))
- /* Read a csv file with scala using opencsv
- * :cp ../../../lib/opencsv-2.2.jar
- */
- //import scala.io.Source
- import au.com.bytecode.opencsv.CSVReader // from opencsv.sf.net
- import java.io.FileReader // for CSVReader
- //import java.io.BufferedOutputStream
- //import java.text.DecimalFormat
- def ReadCsv(filename, header=true, skip=0): List[T] = {
- //val output = new BufferedOutputStream(System.out)
- //val formatter = new DecimalFormat("0.00")
- val dir = "./Documents/findataweb/scala/demos/src/test/scala/"
- val filename = dir + "airquality.csv"
-
- val filename = "airquality.csv"
- val reader = new CSVReader(new FileReader(filename))
- // reads everything as a java List of Array[String]
- val contents = reader.readAll().toArray
-
- scala.Double.NaN
- if (header){
- val header = reader.readNext()
- }
-
- val row = reader.readNext()
- def readRows(reader: CSVReader) {
- reader.readNext() match {
- case null =>
- output.flush
- return
- case columns =>
- output.write(
- (
- columns(0) + " is " + formatter.format(
- columns(1).toDouble * columns(2).toDouble
- ) + "\n"
- ).getBytes
- )
- readRows(reader)
- }
- }
- }