Elasticsearch Java API的使用__Java

來源:互聯網
上載者:User

先上代碼,後續分析。

jar包直接copy的es包下的,實現的功能有:

1.建立索引

2.查詢具體文檔

import java.util.Date;import java.util.HashMap;import java.util.Map;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.action.index.IndexResponse;import org.elasticsearch.client.Client;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.ImmutableSettings;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.InetSocketTransportAddress;public class ESAPITest {private Client client;public ESAPITest(){//首先配置es叢集名稱,到Settings對象Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "inde-test").build();//其次,基於Settings對象建立Clinet對象,串連9300this.client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("101.200.xxx.xxx", 9300));}public static void main(String[] args){ESAPITest esApITest = new ESAPITest();esApITest.createIndex();esApITest.getIndex();}//建立 索引+類型+文檔public void  createIndex() {//產生索引,使用Map儲存Map<String,Object> json = new HashMap<>();json.put("user", "cas");json.put("postDate", new Date());json.put("message", "try Es Java API");IndexResponse response = this.client.prepareIndex("es-api", "test", "2").setSource(json).execute().actionGet();System.out.println(response.isCreated());//建立成功為true;失敗為false;如果裡邊已經有了該文檔,在建立一次,返回falseclient.close();}//根據 索引+類型+文檔 擷取資訊public void getIndex(){GetResponse getResponse = client.prepareGet("es-api","test","1").execute().actionGet();Map<String,Object> res = getResponse.getSource();//查詢不到,會返回null 指標if(res == null){System.out.println("empty");}else{System.out.println(res.toString());//{message=try Es Java API, postDate=2016-08-30T01:56:32.131Z, user=cas}}client.close();}}
BulkProcessor設定批量請求的屬性
//BulkProcessorBulkProcessor bulkProcessor = BulkProcessor.builder(client, new BulkProcessor.Listener() {@Overridepublic void beforeBulk(long arg0, BulkRequest arg1) {//批量執行前做的事情System.out.println("bulk api action starting...");}@Overridepublic void afterBulk(long arg0, BulkRequest arg1, Throwable arg2) {System.out.println("exception:bukl api action ending...:"+arg2.getMessage());}@Overridepublic void afterBulk(long arg0, BulkRequest arg1, BulkResponse arg2) {//正常執行完畢後...System.out.println("normal:bukl api action ending...");}})//設定多種條件,對大量操作進行限制,達到限制中的任何一種觸發請求的批量提交.setBulkActions(1000)//設定大量操作一次性執行的action個數,根據請求個數批量提交//.setBulkSize(new ByteSizeValue(1,ByteSizeUnit.KB))//設定批量提交請求的大小允許的最大值//.setFlushInterval(TimeValue.timeValueMillis(100))//根據時間周期批量提交請求//.setConcurrentRequests(1)//設定允許並發請求的數量//佈建要求失敗時的補償措施,重複請求3次//.setBackoffPolicy(BackoffPolicy.exponentialBackoff(TimeValue.timeValueMillis(100), 3)).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()));}System.out.println("load succeed!");


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.