ES transport client使用


ES transport client bulk insert

傳輸(transport)客戶端

TransportClient利用transport模塊遠程連接一個elasticsearch集群。它並不加入到集群中,只是簡單的獲得一個或者多個初始化的transport地址,並以輪詢的方式與這些地址進行通信。

// on startup Client client = new TransportClient() .addTransportAddress(new InetSocketTransportAddress("host1", 9300)) .addTransportAddress(new InetSocketTransportAddress("host2", 9300)); // on shutdown client.close(); 

注意,如果你有一個與elasticsearch集群不同的集群,你可以設置機器的名字。

Settings settings = ImmutableSettings.settingsBuilder()
        .put("cluster.name", "myClusterName").build(); Client client = new TransportClient(settings); //Add transport addresses and do something with the client... 

你也可以用elasticsearch.yml文件來設置。

這個客戶端可以嗅到集群的其它部分,並將它們加入到機器列表。為了開啟該功能,設置client.transport.sniff為true。

Settings settings = ImmutableSettings.settingsBuilder()
        .put("client.transport.sniff", true).build(); TransportClient client = new TransportClient(settings);

利用elasticsearch幫助類生成JSON

elasticsearch提供了內置的幫助類來將數據轉換為JSON

import static org.elasticsearch.common.xcontent.XContentFactory.*; XContentBuilder builder = jsonBuilder() .startObject() .field("user", "kimchy") .field("postDate", new Date()) .field("message", "trying out Elasticsearch") .endObject() 

注意,你也可以使用startArray(String)endArray()方法添加數組。另外,field可以接收任何類型的對象,你可以直接傳遞數字、時間甚至XContentBuilder對象。

可以用下面的方法查看json。

String json = builder.string();

索引文檔

下面的例子將JSON文檔索引為一個名字為“twitter”,類型為“tweet”,id值為1的索引。

import static org.elasticsearch.common.xcontent.XContentFactory.*; IndexResponse response = client.prepareIndex("twitter", "tweet", "1") .setSource(jsonBuilder() .startObject() .field("user", "kimchy") .field("postDate", new Date()) .field("message", "trying out Elasticsearch") .endObject() ) .execute() .actionGet(); 

你也可以不提供id:

String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\"," + "\"message\":\"trying out Elasticsearch\"" + "}"; IndexResponse response = client.prepareIndex("twitter", "tweet") .setSource(json) .execute() .actionGet();

bulk API

bulk API允許開發者在一個請求中索引和刪除多個文檔。下面是使用實例。

import static org.elasticsearch.common.xcontent.XContentFactory.*; BulkRequestBuilder bulkRequest = client.prepareBulk(); // either use client#prepare, or use Requests# to directly build index/delete requests bulkRequest.add(client.prepareIndex("twitter", "tweet", "1") .setSource(jsonBuilder() .startObject() .field("user", "kimchy") .field("postDate", new Date()) .field("message", "trying out Elasticsearch") .endObject() ) ); bulkRequest.add(client.prepareIndex("twitter", "tweet", "2") .setSource(jsonBuilder() .startObject() .field("user", "kimchy") .field("postDate", new Date()) .field("message", "another post") .endObject() ) ); BulkResponse bulkResponse = bulkRequest.execute().actionGet(); if (bulkResponse.hasFailures()) { // process failures by iterating through each bulk response item }


摘自:https://endymecy.gitbooks.io/elasticsearch-guide-chinese/content/java-api/index-api.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM