2016-04-05 67 views

回答

2

解決方案1:

發現這個市場上的地方「mongodblookup」 只有一個與插頭的問題,如果lookpup比賽失敗,它不會返回的記錄。

解決方案2:

UJDC - 2從輸入流場 - artist_id,翻譯(這是查找標識符)

jsonColl - 在UJDC場它將返回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; 
    } 
}