ES-TEST

package com.MarkNum;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.collect.ImmutableCollection;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;

/**
 * Servlet implementation class TestSrvlet
 */
@WebServlet("/TestSrvlet")
public class TestSrvlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestSrvlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		ESClient client=new ESClient();
//		client.init();
//		client.createIndex();
//		client.close();
		Map<Object, Object> map=new HashMap<Object, Object>();
		map.put("userid", 1);
		map.put("username", "gengzg");
		map.put("useremaiil", "[email protected]");
		
		
		Client client=new TransportClient(ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch").build())
		.addTransportAddress(new InetSocketTransportAddress("127.0.0.1",9300));
		
		for(int i=0;i<1000;i++)
		{
		XContentBuilder builder = XContentFactory.jsonBuilder()
	            .startObject()
	            .field("UsrID", 111)
	            .field("UserName", "耿宗国")
	            .field("UserEmail", "[email protected]")
	            .field("age", 24)
	            .field("UserXX","中华人民共和国万岁!!!!")
	            .endObject();
	        IndexResponse resp = client.prepareIndex("usrbase", "Usrinfo", String.valueOf(i))
	                                    .setSource(builder)
	                                    .execute()
	                                    .actionGet();
	        response.getWriter().println(resp.getIndex());
		}

		
	}

}

 

你可能感兴趣的:(test)