Lookup values in Mongodb Pentaho Spoon

我是研究僧i 提交于 2019-12-12 10:11:41

问题


How can I lookup the values in Mongodb? I use stream lookup but i think it will have a performance issue when looking up on a collection with high volume of data.


回答1:


Solution 1:

Found this on the market place "mongodblookup" There is only one problem with the plug in, it doesn't return a record if the lookpup match fail.

Solution 2:

UJDC - 2 field from input stream - artist_id,translation (this is the identifier for the lookup)

jsonColl - is a field in UJDC it will return null if no document found.

Below is the code

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;
    }
}


来源:https://stackoverflow.com/questions/36415921/lookup-values-in-mongodb-pentaho-spoon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!