Groovy程序连接mongo db

时间:2016-03-19 13:56:11

标签: mongodb groovy

我正在尝试使用Groovy语言连接mongodb,但我收到这样的错误(在下面发布)并且我添加了必要的jar文件。我正在使用mongodb verion:mongodb-driver-3.2.2.jar。 请帮我解决这个问题

Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/operation/OperationExecutor
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
    at java.lang.Class.getDeclaredConstructors(Unknown Source)
    at org.codehaus.groovy.reflection.CachedClass$2$1.run(CachedClass.java:69)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.codehaus.groovy.reflection.CachedClass$2.initValue(CachedClass.java:67)
    at org.codehaus.groovy.reflection.CachedClass$2.initValue(CachedClass.java:64)
    at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:46)
    at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:33)
    at org.codehaus.groovy.reflection.CachedClass.getConstructors(CachedClass.java:258)
    at groovy.lang.MetaClassImpl.<init>(MetaClassImpl.java:213)
    at groovy.lang.MetaClassImpl.<init>(MetaClassImpl.java:223)
    at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.createNormalMetaClass(MetaClassRegistry.java:168)
    at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.createWithCustomLookup(MetaClassRegistry.java:158)
    at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.create(MetaClassRegistry.java:141)
    at org.codehaus.groovy.reflection.ClassInfo.getMetaClassUnderLock(ClassInfo.java:209)
    at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:241)
    at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.getMetaClass(MetaClassRegistryImpl.java:255)
    at org.codehaus.groovy.runtime.InvokerHelper.getMetaClass(InvokerHelper.java:859)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallConstructorSite(CallSiteArray.java:84)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:194)
    at com.sample.MongoService.client(MongoService.groovy:14)
    at com.sample.MongoService.collection(MongoService.groovy:21)
    at com.sample.MongoService$collection.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at com.sample.MongoDBController.method(MongoDBController.groovy:10)
    at com.sample.MongoDBController$method.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
    at com.sample.GroovyDemoApp.main(GroovyDemoApp.groovy:12)
Caused by: java.lang.ClassNotFoundException: com.mongodb.operation.OperationExecutor
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    ... 35 more

更新:

这是我的Groovy代码。使用此代码我正在尝试连接Mongodb

// error is in this line . i .e - 
// The type com.mongodb.operation.CreateIndexesOperation cannot be resolved.
// It is indirectly referenced from required .class files )
package com.sample

import com.mongodb.*

class MongoService { 
  private static MongoClient mongoClient 
  private static host = "localhost" //your host name 
  private static port = 27017 //your port no. 
  private static databaseName = "db"

  public static MongoClient client() {
    if(mongoClient == null){
        return new MongoClient(host,port)
    }else {
        return mongoClient
    }
  }

  public DBCollection collection(collectionName) {
    DB db = client().getDB(databaseName)
    return db.getCollection(collectionName)
  }
}

1 个答案:

答案 0 :(得分:1)

要使用MongoDB驱动程序,您可以使用JAR,也可以使用Maven中的@Grab

使用JAR

要使用JAR,您需要将其添加到Groovy的类路径中。这是通过-cp参数完成的:

#!/usr/bin/env groovy -cp /path/to/jar/file

println 'Hello'

使用Maven

您可以简单地使用Groovy的@Grab来处理依赖关系:

@Grab('org.mongodb:mongodb-driver:3.2.2')

println 'Hello'

工作示例

以下是基于您发布的代码的工作示例:

@Grab('org.mongodb:mongodb-driver:3.2.2')

import com.mongodb.MongoClient
import com.mongodb.DBCollection
import com.mongodb.DB
import com.mongodb.BasicDBObject

class MongoService { 
    private MongoClient mongoClient 

    def host = "localhost" //your host name 
    def port = 27017 //your port no. 
    def databaseName = 'test'

    public MongoClient client() {
        mongoClient = mongoClient ?: new MongoClient(host, port) 

        return mongoClient
    } 

    public DBCollection collection(collectionName) { 
        DB db = client().getDB(databaseName)

        return db.getCollection(collectionName) 
    }
}

def service = new MongoService(databaseName: 'db')
def foo = service.collection('foo')

def data = [
    [firstName: 'Jane', lastName: 'Doe'],
    [firstName: 'Elvis', lastName: 'Presley']
].collect { it as BasicDBObject }

foo.insert(data)

foo.find().toArray().each {
    println it
}

我之前从未使用过MongoDB,所以我的用例是否有用是值得商榷的。