Mongodb Pentaho Spoon 中的查找值

Lookup values in Mongodb Pentaho Spoon

如何查找 Mongodb 中的值? 我使用流查找,但我认为在查找具有大量数据的集合时会出现性能问题。

解决方案 1:

在市场上找到这个 "mongodblookup" 插件只有一个问题,如果查找匹配失败,它不会return记录。

解决方案 2:

UJDC - 来自输入流的 2 个字段 - artist_id,翻译(这是查找的标识符)

jsonColl - 是 UJDC 中的一个字段,如果未找到文档,它将 return 为 null。

代码如下

import com.mongodb.Mongo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.QueryBuilder;  

private Mongo m;
private DB db;
private DBCollection coll;

String getField="xxx";
String jsonField="Y";



public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException
{

    if (first) {

      first=false;
    }

    Object[] r = getRow();

    if (r == null) {
      setOutputDone();
      return false;
    }

    Object[] outputRow = createOutputRow(r, data.outputRowMeta.size());
    Long artist_id = get(Fields.In, "artist_id").getInteger(r);
    String translation = get(Fields.In, "translation").getString(r);

    DBObject query=  coll.findOne(QueryBuilder.start("itunesArtistId").is(artist_id).and("translation.translation").is(translation).get());

    if (query==null){
        jsonField=null;
    }else{
        jsonField="exist";
    }

    get(Fields.Out, "jsonColl").setValue(outputRow, jsonField );
    putRow(data.outputRowMeta, outputRow);

    // putRow will send the row on to the default output hop.
    //
    return true;
}

public boolean init(StepMetaInterface stepMetaInterface, StepDataInterface stepDataInterface)
{
    try {
            m = new Mongo("127.0.0.1", 27017);
        db = m.getDB( "databasename" );
        db.authenticate("user", "password".toCharArray());  
            coll = db.getCollection("artist");

        return parent.initImpl(stepMetaInterface, stepDataInterface);
    } catch(Exception e) {
        logError("Error connecting to MongoDB: ", e);
            return false;
    }
}