Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial import

  • Loading branch information...
commit d81f65d75d313dada0e0acf62e7b5edd50da8fbd 0 parents
@tc authored
8 .gitignore
@@ -0,0 +1,8 @@
+.DS_Store
+*~
+src/r-mongo-scala/target
+*.iws
+*.ipr
+*.iml
+*.class
+.idea
12 DESCRIPTION
@@ -0,0 +1,12 @@
+Package: RMongo
+Type: Package
+Title: MongoDB Client for R
+Version: 0.0.5
+Date: 2010-09-22
+Author: Tommy Chheng
+Maintainer: Tommy Chheng <tommy@quid.com>
+Description: MongoDB Database interface for R. The interface is provided via Java calls to the mongo-java-driver.
+License: GPL-3
+LazyLoad: yes
+Depends: R(>= 2.8.0), rJava, rjson
+URL: http://github.com/quid/RMongo
46 R/rmongo.R
@@ -0,0 +1,46 @@
+library("rJava")
+library("rjson")
+.jinit()
+.jaddClassPath("../lib/r-mongo-scala-1.0-SNAPSHOT.jar")
+
+setClass("RMongo", representation(javaMongo = "jobjRef"))
+
+mongoDbConnect <- function(dbName, host="localhost", port=27017){
+ rmongo <- new("RMongo", javaMongo = .jnew("com/quid/RMongo", dbName))
+ rmongo
+}
+
+setGeneric("dbInsertDocument", function(this, collection, doc) standardGeneric("dbInsertDocument"))
+setMethod("dbInsertDocument", signature(this="RMongo", collection="character", doc="character"),
+ function(this, collection, doc){
+ results <- .jcall(this@javaMongo, "S", "dbInsertDocument", collection, doc)
+ results
+ }
+)
+
+#
+# format can be json or data.frame
+# json will return an rjson object
+# data.frame will attempt to convert to flat data frame table.
+setGeneric("dbGetQuery", function(this, collection, query) standardGeneric("dbGetQuery"))
+setMethod("dbGetQuery", signature(this="RMongo", collection="character", query="character"),
+ function(this, collection, query){
+ format <- "json"
+ results <- .jcall(this@javaMongo, "S", "dbGetQuery", collection, query, format)
+ if(format == "json"){
+ jsonResults <- fromJSON(results)
+ jsonResults
+ }else if(format == "data.frame"){
+ #convert to data.frame table
+ results
+ }
+ }
+)
+
+setGeneric("dbDisconnect", function(this) standardGeneric("dbDisconnect"))
+setMethod("dbDisconnect", signature(this="RMongo"),
+ function(this){
+ .jcall(this@javaMongo, "V", "close")
+ }
+)
+
38 man/RMongo-package.Rd
@@ -0,0 +1,38 @@
+\name{RMongo-package}
+\alias{RMongo-package}
+\alias{RMongo}
+\docType{package}
+\title{
+MongoDB client for R.
+}
+\description{
+The functions in this package allow you make queries with a MongoDB database from R.
+}
+\details{
+\tabular{ll}{
+Package: \tab RMongo\cr
+Type: \tab Package\cr
+Version: \tab 0.05\cr
+Date: \tab 2010-09-26\cr
+License: \tab GPL-3\cr
+LazyLoad: \tab yes\cr
+}
+mongoDbConnect
+dbGetQuery
+dbDisconnect
+}
+\author{
+Tommy Chheng <tommy@quid.com>
+}
+\references{
+rJava
+rjson
+}
+\keyword{ mongodb }
+\keyword{ mongo }
+\seealso{
+~~ \code{\link[rJava:rJava-package]{rJava}} ~~
+}
+\examples{
+see examples in runit.rmongo.R
+}
11 src/r-mongo-scala/.gitignore
@@ -0,0 +1,11 @@
+.DS_Store
+src/test/output
+*.iml
+*.ipr
+*.iws
+.*~
+dist
+build
+target
+*.class
+
22 src/r-mongo-scala/README.txt
@@ -0,0 +1,22 @@
+r-mongo-scala
+----
+For use with the RMongo r package.
+
+
+# ---------------------------------------------------------------------------
+# Copyright 2010, Quid, Inc.
+#
+# r-mongo-scala is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# r-mongo-scala is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with r-mongo-scala. If not, see <http://www.gnu.org/licenses/&gt;.
+#
+# ---------------------------------------------------------------------------
142 src/r-mongo-scala/pom.xml
@@ -0,0 +1,142 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.quid</groupId>
+ <artifactId>r-mongo-scala</artifactId>
+ <packaging>jar</packaging>
+ <version>1.0-SNAPSHOT</version>
+ <name>r-mongo-scala</name>
+
+ <properties>
+ <scala.version>2.8.0</scala.version>
+ <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+ </properties>
+
+ <repositories>
+ <repository>
+ <id>maven.org</id>
+ <url>http://repo2.maven.org/maven2/</url>
+ </repository>
+
+ <repository>
+ <id>scala-tools.org</id>
+ <name>Scala-Tools Maven2 Repository</name>
+ <url>http://scala-tools.org/repo-releases/</url>
+ </repository>
+
+ <repository>
+ <id>jboss</id>
+ <url>http://repository.jboss.com/maven2</url>
+ <releases>
+ <enabled>true</enabled>
+ </releases>
+ </repository>
+ </repositories>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.scala-lang</groupId>
+ <artifactId>scala-library</artifactId>
+ <version>${scala.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.7</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.mongodb</groupId>
+ <artifactId>mongo-java-driver</artifactId>
+ <version>2.1</version>
+ </dependency>
+
+ </dependencies>
+
+ <build>
+ <sourceDirectory>src/main/scala</sourceDirectory>
+ <testSourceDirectory>src/test/scala</testSourceDirectory>
+ <plugins>
+ <plugin>
+ <groupId>org.scala-tools</groupId>
+ <artifactId>maven-scala-plugin</artifactId>
+ <version>2.14</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>compile</goal>
+ <goal>testCompile</goal>
+ </goals>
+ </execution>
+ </executions>
+ <configuration>
+ <scalaVersion>${scala.version}</scalaVersion>
+ <args>
+ <arg>-target:jvm-1.5</arg>
+ </args>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.1</version>
+
+ <configuration>
+ <encoding>UTF-8</encoding>
+ <source>1.6</source>
+ <target>1.6</target>
+ <optimize>true</optimize>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <artifactId>maven-assembly-plugin</artifactId>
+ <version>2.2-beta-5</version>
+ <configuration>
+ <descriptorRefs>
+ <descriptorRef>jar-with-dependencies</descriptorRef>
+ </descriptorRefs>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-shade-plugin</artifactId>
+ <version>1.4</version>
+ <executions>
+ <execution>
+ <phase>package</phase>
+ <goals>
+ <goal>shade</goal>
+ </goals>
+ <configuration>
+ <minimizeJar>true</minimizeJar>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+
+ <plugin>
+ <artifactId>maven-antrun-plugin</artifactId>
+ <version>1.5</version>
+ <executions>
+ <execution>
+ <phase>install</phase>
+ <goals>
+ <goal>run</goal>
+ </goals>
+ <configuration>
+ <target>
+ <copy file="target/${project.name}-${project.version}.jar"
+ tofile="../../lib/${project.name}-${project.version}.jar"/>
+ </target>
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+
+ </plugins>
+ </build>
+</project>
68 src/r-mongo-scala/src/main/scala/com/quid/RMongo.scala
@@ -0,0 +1,68 @@
+package com.quid
+
+import com.mongodb.util.JSON
+import com.mongodb.{Mongo, DBObject, DB, DBCursor}
+
+/**
+ *
+ * User: @tommychheng
+ * Date: Sep 23, 2010
+ * Time: 9:36:10 PM
+ *
+ *
+ */
+
+class RMongo(dbName: String, host: String, port: Int) {
+ val m = new Mongo(host, port)
+ val db = m.getDB(dbName)
+
+ def this(dbName: String) = this (dbName, "localhost", 27017)
+
+
+ def dbInsertDocument(collectionName: String, jsonDoc: String): String = {
+ val dbCollection = db.getCollection(collectionName)
+
+ val docObject = JSON.parse(jsonDoc).asInstanceOf[DBObject]
+ val results = dbCollection.insert(docObject)
+
+ if(results.getError == null) "ok" else results.getError
+ }
+
+ def dbGetQuery(collectionName: String, query: String, format: String = "json"): String = {
+ val dbCollection = db.getCollection(collectionName)
+
+ val queryObject = JSON.parse(query).asInstanceOf[DBObject]
+ val cursor = dbCollection.find(queryObject).iterator
+
+ val results = format match {
+ case "json" => toJsonOutput(cursor)
+ case "data.frame" => toDataFrameOutput(cursor)
+ }
+
+ results
+ }
+
+ def toJsonOutput(cursor: java.util.Iterator[DBObject]): String = {
+ val results = new StringBuffer("[")
+ while (cursor.hasNext) {
+ val item = cursor.next
+ results.append(item.toString)
+ results.append(",")
+ }
+ results.deleteCharAt(results.length - 1) //remove last comma
+ results.append("]")
+ results.toString
+ }
+
+ def toDataFrameOutput(cursor: java.util.Iterator[DBObject]): String = {
+ "NOT SUPPORTED YET"
+ }
+
+ def close() {
+ m.close()
+ }
+
+ def main() {
+
+ }
+}
67 src/r-mongo-scala/src/test/scala/com/quid/MongoTest.scala
@@ -0,0 +1,67 @@
+package com.quid
+
+import com.mongodb.util.JSON
+import org.junit.{Before, Assert, Test}
+import com.mongodb.{BasicDBList, Mongo, DB, DBObject}
+
+/**
+ *
+ * User: @tommychheng
+ * Date: Sep 24, 2010
+ * Time: 9:36:41 AM
+ *
+ *
+ */
+class MongoTest{
+ def clearTestDB{
+ val m = new Mongo()
+ val db = m.getDB("test")
+ val collection = db.getCollection("test_data")
+ collection.drop() //make sure the collection is empty
+ }
+
+ @Before
+ def insertDocs{
+ clearTestDB
+
+ val m = new Mongo()
+ val db = m.getDB("test")
+ val collection = db.getCollection("test_data")
+ val doc = """ {"foo": "bar", "size": 5} """
+ val docObject = JSON.parse(doc).asInstanceOf[DBObject]
+ collection.insert(docObject)
+
+ val doc1 = """ {"foo": "n1", "size": 10} """
+ val docObject1 = JSON.parse(doc1).asInstanceOf[DBObject]
+ collection.insert(docObject1)
+ }
+
+ @Test
+ def testDbInsertDocument{
+ clearTestDB
+
+ val rMongo = new RMongo("test")
+ val doc = """ {"_id": "foo", "foo": "bar", "size": 5} """
+
+ val response = rMongo.dbInsertDocument("test_data", doc)
+
+ Assert.assertEquals("ok", response)
+
+ val duplicateResponse = rMongo.dbInsertDocument("test_data", doc)
+
+ Assert.assertEquals("E11000 duplicate key error index: test.test_data.$_id_ dup key: { : \"foo\" }", duplicateResponse)
+ }
+
+ @Test
+ def testDbGetQuery{
+ val rMongo = new RMongo("test")
+ val results = rMongo.dbGetQuery("test_data", """ {} """)
+
+ val jsonParsed = scala.util.parsing.json.JSON.parseFull(results)
+ val records = jsonParsed.getOrElse(List()).asInstanceOf[List[Any]]
+ val record = records.head.asInstanceOf[Map[String,Any]]
+
+ Assert.assertEquals(record.getOrElse("foo", ""), "bar")
+ }
+
+}
19 tests/runit.rmongo.R
@@ -0,0 +1,19 @@
+library('RUnit')
+source('../R/rmongo.R', chdir=TRUE)
+
+test.dbInsertDocument <- function(){
+ mongo <- mongoDbConnect("test")
+ output <- dbInsertDocument(mongo, "test_data", "{\"foo\": \"bar\"}")
+ dbDisconnect(mongo)
+
+ checkEquals("ok", output)
+}
+
+test.dbGetQuery <- function(){
+ #insert the records using r-mongo-scala project
+ mongo <- mongoDbConnect("test")
+ output <- dbGetQuery(mongo, "test_data", "{\"foo\": \"bar\"}")
+ dbDisconnect(mongo)
+
+ checkEquals("bar", output[[1]]$foo)
+}
5 tests/test.runner.R
@@ -0,0 +1,5 @@
+library("RUnit")
+testsuite.rmongo <- defineTestSuite("rmongo", dirs=file.path("."))
+
+testResult <- runTestSuite(testsuite.rmongo)
+printTextProtocol(testResult)
Please sign in to comment.
Something went wrong with that request. Please try again.