I wrote a transformer for DIH to get the value from a field using Http call.
Since this runs on a SOLR node when indexing, I think I would do it with
SOLRJ.

How do I take this and convert it to SOLRJ and avoid the network call ?

Also, this is pretty cool, and avoids the Entity for the call - since using
an Entity has issues which I reported on a few days ago. It is much easier
to access all fields when using a transformer.

<field column="provider_json" outfield="has_comment"
jsonprovidersurvey="true" url="http://localhost:8983/solr/survey/select";
qt="dihsurvey" source="provider_json" />

package hg;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;

import java.io.IOException;
import org.apache.solr.handler.dataimport.Context;
import org.apache.solr.handler.dataimport.DataImporter;
import org.apache.solr.handler.dataimport.Transformer;

import java.util.List;
import java.util.Map;


// Overwrite for Survey embedded entity
// <field column="provider_json" outfield="has_comment"
jsonprovidersurvey="true"
url="http://localhost:8983/solr/survey/select"; qt="dihsurvey"
source="provider_json" />

public class JSONProviderSurvey extends Transformer {

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

    public static JSONObject readJsonFromUrl(String urlPath) throws
IOException {
        URL url = new URL(urlPath);
        URLConnection con = url.openConnection();
        con.setConnectTimeout(10000);
        con.setReadTimeout(10000);
        InputStream is = con.getInputStream();
        //InputStream is = new URL(url).openStream();
        try {
            BufferedReader rd = new BufferedReader(new
InputStreamReader(is, Charset.forName("UTF-8")));
            String jsonText = readAll(rd);
            JSONObject json = JSONObject.fromObject(jsonText);
            return json;
        } finally {
            is.close();
        }
    }

    public Map<String, Object> transformRow(Map<String, Object> row,
Context context) {

        List<Map<String, String>> fields = context.getAllEntityFields();

        for (Map<String, String> field : fields) {
            String jsonf = field.get("jsonprovidersurvey");

            if ("true".equals(jsonf)) {

                String columnName = field.get(DataImporter.COLUMN);
                String url = field.get("url");
                //url = "http://hgsolr2devmstr:8983/solr/survey/select";;
                String qt = field.get("qt");
                //qt = "dihsurvey";
                String pwid = SolrUtility.GetSafeString(row.get("id"));
                String source = field.get("source");
                String outfield =
SolrUtility.GetSafeString(field.get("outfield"));
                if (row.get(source) != null) {

                    Object jobject = row.get(source);
                    JSONObject provider =
JSONObject.fromObject(jobject.toString());
                    JSONObject json;
                    try {
                        json = readJsonFromUrl(url + "?qt=" + qt +
"&q=provider_code:" + pwid.toUpperCase() +
"&wt=json&rows=1&omitHeader=true");
                        if (json.get("response") != null) {
                            JSONObject response =
json.getJSONObject("response");
                            JSONArray docs = response.getJSONArray("docs");
                            if (docs.size() > 0) {
                                JSONObject doc = docs.getJSONObject(0);
                                boolean has_comment =
SolrUtility.GetSafeBool(doc.get("has_comment"));
                                provider.element("HasOpenComment", has_comment);
                                row.put(columnName, provider.toString());
                                if (outfield.length() > 0) {
                                    row.put(outfield, has_comment);
                                }
                            }
                        }
                    } catch (Exception e) {
                        provider.element("HasOpenComment", false);
                        row.put(columnName, provider.toString());
                        if (outfield.length() > 0) {
                            row.put(outfield, false);
                        }
                    }
                }
            }
        }

        return row;
    }
}



-- 
Bill Bell
billnb...@gmail.com
cell 720-256-8076

Reply via email to