solr使用http://master1:8983/solr/TESTDATA/update?commit=true更新索引数据

对于solr的索引更新官网一般建议使用solrJ的方式,但是最近我们主管考虑我们现有环境的需要,要使用http的方式进行更新。

package solr6service.solr6;


import java.io.IOException;
import java.nio.charset.Charset;
import java.util.UUID;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.xerces.xs.XSObject;

public class SolrUrlOpt {
    public static void main(String[] args) throws Exception {
        System.out.println("发送请求开始...");
        //org.apache httpclient
        String xmlStr="<add><doc><field name=\"id\">0030</field><field name=\"rowkey\">abc0030</field></doc></add>";
        String url="http://master1:8983/solr/TESTDATA/update?commit=true";
        solrPost(jsonStr,url);
        System.out.println("发送请求结束...");
    }

    public static void solrPost(String xmlStr,String url){
        HttpPost post = null;
        try {
        HttpClient httpClient = new DefaultHttpClient();
        
        // 设置超时时间
        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 2000);
        httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 2000);
        
        post = new HttpPost(url);
        // 构造消息头
        post.setHeader("Content-type", "application/xml; charset=utf-8");
        post.setHeader("Connection", "Close");
        String sessionId = getSessionId();
        post.setHeader("SessionId", sessionId);
//        post.setHeader("appid", appid);
        
     // 构建消息实体
        StringEntity entity = new StringEntity(xmlStr, Charset.forName("UTF-8"));
        entity.setContentEncoding("UTF-8");
        // 发送Json格式的数据请求
        entity.setContentType("application/json");
        post.setEntity(entity);
            
        HttpResponse response = httpClient.execute(post);
        
         // 检验返回码
        int statusCode = response.getStatusLine().getStatusCode();
        if(statusCode != HttpStatus.SC_OK){
            System.out.println("请求出错: "+statusCode);
        }else{
            int retCode = 0;
            String sessendId = "";
            // 返回码中包含retCode及会话Id
            for(Header header : response.getAllHeaders()){
                if(header.getName().equals("retcode")){
                    retCode = Integer.parseInt(header.getValue());
                }
                if(header.getName().equals("SessionId")){
                    sessendId = header.getValue();
                }
            }
        }
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
    }
    
    // 构建唯一会话Id
    public static String getSessionId(){
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        return str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24);
    }

猜你喜欢

转载自my.oschina.net/u/3197158/blog/1794717