Solr 6.0 學習(十一)solr writer自定義

CustomerResponseWriter

package com.upxiaofeng.solr;

import java.io.IOException;
import java.io.Writer;

import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.QueryResponseWriter;
import org.apache.solr.response.SolrQueryResponse;

public class CustomerResponseWriter implements QueryResponseWriter {

    public static final String ContentType_UTF_8 = "application/json; charset=UTF-8";

    private String contentType;

    /**
     * 獲取字符類型
     */
    public String getContentType(SolrQueryRequest arg0, SolrQueryResponse arg1) {
        return this.contentType;
    }

    /**
     * 初始化
     */
    public void init(NamedList arg0) {
        String content_type = (String) arg0.get("content-type");
        this.contentType = content_type == null ? ContentType_UTF_8: content_type;
    }

    public void write(Writer arg0, SolrQueryRequest arg1, SolrQueryResponse arg2) throws IOException {
        CustomerWriter writer = new CustomerWriter(arg0, arg1, arg2);
        try {
            writer.writerResponse(arg2.getValues());
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

}

CustomerWriter

package com.upxiaofeng.solr;

import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.Map;

import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.response.TextResponseWriter;
import org.apache.solr.search.ReturnFields;

import com.google.gson.Gson;

public class CustomerWriter extends TextResponseWriter {

    private Writer writer ;
    private SolrQueryRequest req ;
    private SolrQueryResponse rsp ;

    public CustomerWriter(Writer writer, SolrQueryRequest req, SolrQueryResponse rsp) {
        super(writer, req, rsp);
        this.writer=writer;
        this.rsp=rsp;
        this.req=req;
    }
    /**
     * 將響應數據返回爲指定的格式
     * 這裏是json爲例
     * @param val
     */
    public void writerResponse(NamedList val){
        Gson gson = new Gson();
        String responseVal = gson.toJson(val);
        try {
            if (responseVal!=null&&!responseVal.isEmpty()) {
                this.writer.write(responseVal);
                this.writer.flush();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void writeArray(String arg0, Iterator arg1) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeBool(String arg0, String arg1) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeDate(String arg0, String arg1) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeDouble(String arg0, String arg1) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeEndDocumentList() throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeFloat(String arg0, String arg1) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeInt(String arg0, String arg1) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeLong(String arg0, String arg1) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeMap(String arg0, Map arg1, boolean arg2, boolean arg3) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeNamedList(String arg0, NamedList arg1) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeNull(String arg0) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeSolrDocument(String arg0, SolrDocument arg1, ReturnFields arg2, int arg3) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeStartDocumentList(String arg0, long arg1, int arg2, long arg3, Float arg4) throws IOException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeStr(String arg0, String arg1, boolean arg2) throws IOException {
        // TODO Auto-generated method stub

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章