本篇学习笔记来源于ES官方网站的指导文档:ES JAVA API
Client
Client是一个类,通过这个类可以实现对ES集群的各种操作:Index, Get, Delete , Search,以及对ES集群的管理任务。
Client的构造需要基于TransportClient
TransportClient
TransportClient可以远程连接ES集群,通过一个传输模块,但是它不真正的连接到集群,只是获取集群的一个或多个初始传输地址,在每次请求动作时,才真正连接到ES集群。
Settings
Settings类主要是在启动Client之前,配置一些属性参数,主要配置集群名称cluster.name,还有其他参数:
client.transport.sniff 是否为传输client添加嗅探功能
client.transport.ignore_cluster_name 设为true,忽略连接节点的集群名称验证
client.transport.ping_timeout 设置ping节点时的时间限,默认5s
client.transport.nodes_sampler_interval 设置sample/ping nodes listed 间隔时间,默认5s
-
- Settings settings = Settings.settingsBuilder().put("cluster.name","index-name").build();
-
-
- Client client = TransportClient.builder().settings(settings).build().
- addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.xxx.xxx"),9300));
-
-
-
-
-
-
- client.close();
Document API
主要分为以下类:Index API , Get API , Delete API , Update API, Multi Get API, Bulk API
es中的增删改查
Index API可以索引一个典型的JSON文档到指定的索引中,并且可以使它可以检索。
产生JSON
JSON产生可以有以下几种方式:
手动拼接一个JSON字符串
使用Map
使用第三方库,比如Jackson
使用内置的XContentFactory.jsonBuilder()
每种类型都会转换为byte[],因此如果对象已经是这种形式,可以直接使用,jsonBuilder是一个高度优化了的JSON产生器,它直接构造byte[]
通过下边的代码讲解四种方法:
index-api, get-api, delete-api, update-api
-
-
-
-
-
-
-
-
-
-
- public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
-
- Settings settings = Settings.settingsBuilder().put("cluster.name","myApp").build();
-
-
- Client client = null;
- try {
- client = TransportClient.builder().settings(settings).build().
- addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("101.200.124.27"),9300));
- } catch (UnknownHostException e) {
- e.printStackTrace();
- }
-
- SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
- XContentBuilder jsonBuilder = XContentFactory.jsonBuilder()
- .startObject()
- .field("user","yuchen")
- .field("interest","reading book")
- .field("insert_time",df.format(new Date()))
- .endObject();
-
- IndexResponse response = client.prepareIndex("index-test","weibo","4")
- .setSource(jsonBuilder)
- .get();
-
- String _index = response.getIndex();
- String _type = response.getType();
- String _id = response.getId();
- long _version = response.getVersion();
- boolean created = response.isCreated();
- System.out.println(_index+" "+_type+" "+_id+" "+_version+" "+created);
-
-
- GetResponse getResponse = client.prepareGet("index-test","weibo","1").get();
- System.out.println(getResponse.getSourceAsString());
-
-
- DeleteResponse deleteResponse = client.prepareDelete("index-test","weibo","4").get();
- System.out.println(deleteResponse.isFound());
-
-
- UpdateRequest updateRequest = new UpdateRequest();
- updateRequest.index("index-test");
- updateRequest.type("weibo");
- updateRequest.id("1");
- updateRequest.doc(XContentFactory.jsonBuilder().startObject().field("interest","music").endObject());
- UpdateResponse updateResponse = client.update(updateRequest).get();
- System.out.println(updateResponse.isCreated());
-
- UpdateResponse updateResponse2 = client.prepareUpdate("index-test", "weibo", "1")
- .setDoc(XContentFactory.jsonBuilder()
- .startObject()
- .field("interest2","reading")
- .endObject()).get();
- System.out.println(updateResponse2.isCreated());
-
-
-
- IndexRequest indexRequest = new IndexRequest("index-test","weibo","14");
- indexRequest.source(XContentFactory.jsonBuilder()
- .startObject()
- .field("user","yuchen2")
- .field("interest","eating")
- .field("insert_time",df.format(new Date()))
- .endObject());
-
- UpdateRequest updateRequest3 = new UpdateRequest("index-test","weibo","14");
- updateRequest3.doc(XContentFactory.jsonBuilder()
- .startObject()
- .field("interest2","love")
- .endObject()
- ).upsert(indexRequest);
- client.update(updateRequest3).get();
-
- if(client != null){
- client.close();
- }
- }
批量操作
Multi Get Api 和 Bulk Api可进行批量的增删改查
使用Multi Get Api 批量获取:
-
-
- MultiGetResponse multiGetResponse = client.prepareMultiGet()
- .add("index-test","weibo","1")
- .add("index-test","weibo","11","13","14")
- .add("index-other","news","1","3").get();
- for(MultiGetItemResponse item:multiGetResponse){
- GetResponse response = item.getResponse();
- System.out.println(response.getSourceAsString());
- }
Bulk Api批量增加:
-
-
- BulkRequestBuilder bulkRequest = client.prepareBulk();
- bulkRequest.add(client.prepareIndex("index-test", "weibo", "20")
- .setSource(XContentFactory.jsonBuilder()
- .startObject()
- .field("user", "yuchen20")
- .field("postDate", new Date())
- .field("message", "trying out Elasticsearch")
- .endObject()
- )
- );
- bulkRequest.add(client.prepareIndex("index-test", "weibo", "21")
- .setSource(XContentFactory.jsonBuilder()
- .startObject()
- .field("user", "yuchen21")
- .field("postDate", new Date())
- .field("message", "trying out Elasticsearch")
- .endObject()
- )
- );
-
- BulkResponse bulkResponse = bulkRequest.get();
- if(bulkResponse.hasFailures()){
-
- }
Bulk Api批量删除:
-
- BulkRequestBuilder bulkRequest = client.prepareBulk();
- bulkRequest.add(client.prepareDelete("index-test", "weibo", "20")
- );
- bulkRequest.add(client.prepareDelete("index-test", "weibo", "21")
- );
-
- BulkResponse bulkResponse = bulkRequest.get();
- if(bulkResponse.hasFailures()){
- System.out.println("bulk error:"+bulkResponse.buildFailureMessage());
- }
Bulk Api 批量更新
-
- BulkRequestBuilder bulkRequest = client.prepareBulk();
- bulkRequest.add(client.prepareUpdate("index-test", "weibo", "11").setDoc(XContentFactory
- .jsonBuilder().startObject()
- .field("country","China")
- .endObject()
- )
- );
- bulkRequest.add(client.prepareUpdate("index-test", "weibo", "13").setDoc(XContentFactory
- .jsonBuilder().startObject()
- .field("user","yuchen13")
- .endObject()
- )
- );
-
- BulkResponse bulkResponse = bulkRequest.get();
- if(bulkResponse.hasFailures()){
- System.out.println("bulk error:"+bulkResponse.buildFailureMessage());
- }
BulkProcessor设置批量请求的属性
-
- BulkProcessor bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() {
- @Override
- public void beforeBulk(long arg0, BulkRequest arg1) {
-
- System.out.println("bulk api action starting...");
- }
- @Override
- public void afterBulk(long arg0, BulkRequest arg1, Throwable arg2) {
- System.out.println("exception:bukl api action ending...:"+arg2.getMessage());
- }
- @Override
- public void afterBulk(long arg0, BulkRequest arg1, BulkResponse arg2) {
-
- System.out.println("normal:bukl api action ending...");
- }
- })
-
- .setBulkActions(1000)
-
-
-
-
-
- .build();
- for(int i =0;i<100000;i++){
- bulkProcessor.add(new IndexRequest("index-test","weibo2",""+i).source(
- XContentFactory
- .jsonBuilder()
- .startObject()
- .field("name","yuchen"+i)
- .field("interest","love"+i)
- .endObject()));
- }
- bulkProcessor.awaitClose(5, TimeUnit.MINUTES);
- System.out.println("load succeed!");
默认的参数:
- sets bulkActions to
1000
- sets bulkSize to
5mb
- does not set flushInterval
- sets concurrentRequests to 1
- sets backoffPolicy to an exponential backoff with 8 retries and a start delay of 50ms. The total wait time is roughly 5.1 seconds.