当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

ES-TEST

发表于: 2015-07-04   作者:gengzg   来源:转载   浏览:
摘要: 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
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", "gengzg@139.com");
		
		
		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", "gengzg@139.com")
	            .field("age", 24)
	            .field("UserXX","中华人民共和国万岁!!!!")
	            .endObject();
	        IndexResponse resp = client.prepareIndex("usrbase", "Usrinfo", String.valueOf(i))
	                                    .setSource(builder)
	                                    .execute()
	                                    .actionGet();
	        response.getWriter().println(resp.getIndex());
		}

		
	}

}

 

ES-TEST

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

推荐文章
编辑推荐
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号