Solr 6.0 学习(十一)solr writer自定义

本文介绍了一个用于Apache Solr的自定义响应写入器实现,该写入器能够将查询响应转换为JSON格式的数据,并通过CustomerResponseWriter和CustomerWriter两个类来完成这一过程。CustomerResponseWriter负责初始化内容类型并调用CustomerWriter进行实际的写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值