博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
elasticsearch 口水篇(3)java客户端 - Jest
阅读量:6651 次
发布时间:2019-06-25

本文共 4719 字,大约阅读时间需要 15 分钟。

elasticsearch有丰富的客户端,java客户端有Jest。其原文介绍如下:

 is a Java HTTP Rest client for ElasticSearch.It is actively developed and tested by Searchly.

A sample Java application using Jest can be found on GitHub  .

[]

 

下面我们做一个很简单的实例,以下几个功能:

1)批量添加1000个user对象;

2)通过id进行查询;

3)通过name进行查询;

 

user对象定义:

package com.fox.c1;import io.searchbox.annotations.JestId;/** * @author huangfox * @date 2014年1月22日 下午5:31:52 *  */public class User {	@JestId	private Long id;	private String name;	private int age;	public Long getId() {		return id;	}	public void setId(Long id) {		this.id = id;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public int getAge() {		return age;	}	public void setAge(int age) {		this.age = age;	}}

  

简单实现一个工厂类,用来获得JestHttpClient,如下:

package com.fox.c1;import io.searchbox.client.JestClientFactory;import io.searchbox.client.config.HttpClientConfig;import io.searchbox.client.http.JestHttpClient;/** * @author huangfox * @date 2014年1月22日 下午5:16:15 *  */public class ESFactory {	private static JestHttpClient client;	private ESFactory() {	}	public synchronized static JestHttpClient getClient() {		if (client == null) {			JestClientFactory factory = new JestClientFactory();			factory.setHttpClientConfig(new HttpClientConfig.Builder(					"http://localhost:9200").multiThreaded(true).build());			client = (JestHttpClient) factory.getObject();		}		return client;	}	public static void main(String[] args) {		JestHttpClient client = ESFactory.getClient();		System.out.println(client.getAsyncClient());		System.out.println(client.getServers());		client.shutdownClient();	}}

  

测试类:

package com.fox.c1;import io.searchbox.client.JestResult;import io.searchbox.client.http.JestHttpClient;import io.searchbox.core.Get;import io.searchbox.core.Index;import io.searchbox.core.Search;import io.searchbox.indices.CreateIndex;import io.searchbox.indices.DeleteIndex;import java.io.IOException;import org.elasticsearch.index.query.QueryBuilders;import org.elasticsearch.search.builder.SearchSourceBuilder;/** * @author huangfox * @date 2014年1月22日 下午5:32:20 *  */public class ESTest {	private JestHttpClient client = ESFactory.getClient();	/**	 * indexing	 * 	 * @param indexName	 */	public void index(String indexName) {		try {			// drop			DeleteIndex dIndex = new DeleteIndex(new DeleteIndex.Builder(					indexName));			client.execute(dIndex);			// create			CreateIndex cIndex = new CreateIndex(new CreateIndex.Builder(					indexName));			client.execute(cIndex);			// add doc			for (int i = 0; i < 1000; i++) {				User user = new User();				user.setId(new Long(i));				user.setName("huang fox " + i);				user.setAge(i % 100);				Index index = new Index.Builder(user).index(indexName)						.type(indexName).build();				client.execute(index);			}			//			client.shutdownClient();		} catch (IOException e) {			e.printStackTrace();		}	}	/**	 * getting by id	 * 	 * @param indexName	 * @param query	 */	public void get(String indexName, String query) {		Get get = new Get.Builder(indexName, query).build();		try {			JestResult rs = client.execute(get);			System.out.println(rs.getJsonString());			//			client.shutdownClient();		} catch (IOException e) {			e.printStackTrace();		}	}	/**	 * query	 * 	 * @param query	 */	public void search(String query) {		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();		searchSourceBuilder.query(QueryBuilders.queryString(query));		searchSourceBuilder.field("name");		Search search = new Search.Builder(searchSourceBuilder.toString())				.build();		try {			JestResult rs = client.execute(search);			System.out.println(rs.getJsonString());			//			client.shutdownClient();		} catch (IOException e) {			e.printStackTrace();		}	}	public static void main(String[] args) {		ESTest t = new ESTest();		String indexName = "user";		// indexing		t.index(indexName);		// getting by id		t.get(indexName, "2");		// query		String query = "fox";		t.search(query);	}}

  

index 批量添加建议使用bulk,效率高(减少通讯次数):

public void indexBulk(String indexName) {		try {			// drop			DeleteIndex dIndex = new DeleteIndex(new DeleteIndex.Builder(					indexName));			client.execute(dIndex);			// create			CreateIndex cIndex = new CreateIndex(new CreateIndex.Builder(					indexName));			client.execute(cIndex);			// add doc			Bulk.Builder bulkBuilder = new Bulk.Builder();			for (int i = 0; i < 1000; i++) {				User user = new User();				user.setId(new Long(i));				user.setName("huang fox " + i);				user.setAge(i % 100);				Index index = new Index.Builder(user).index(indexName)						.type(indexName).build();				bulkBuilder.addAction(index);			}			client.execute(bulkBuilder.build());			//			client.shutdownClient();		} catch (IOException e) {			e.printStackTrace();		}	}

  

 

 

 

 

转载地址:http://eujto.baihongyu.com/

你可能感兴趣的文章
示波器X1探头和X10探头
查看>>
HDU 5795 A Simple Nim
查看>>
Centos7下配置Java web环境(JDK、Tomcat、Mysql)
查看>>
javascript导图 ...
查看>>
Liferay——代码调试(Debug)方法
查看>>
BZOJ 2929 网络流
查看>>
业务安全的一些实例
查看>>
【转】Windows(server2008)下使用VisualSVN Server搭建SVN服务器
查看>>
题解 P1000 【超级玛丽游戏】
查看>>
移动端html5页面导航栏悬浮遮挡内容第一行解决办法
查看>>
Linux非阻塞IO(六)使用poll实现非阻塞的服务器端
查看>>
JS设置CSS样式的几种方式
查看>>
asp.net网站Application_Start疑是不执行的现象
查看>>
信号量同步编程
查看>>
Spring MVC 异常处理优解
查看>>
硬盘性能测试工具fio
查看>>
802.11帧
查看>>
css解决内联元素间的空白间隔
查看>>
lvs+keepalive 实现高可用软负载均衡集群
查看>>
log 的 debug()、 error()、 info()方法的区别
查看>>