標籤:tran date() set ram cep 5.0 transport name import
elasticsearch提供了多種更新索引的方式,這裡簡單介紹其中五種
1 package com.juyun.test; 2 3 import java.io.IOException; 4 import java.net.InetAddress; 5 import java.util.concurrent.ExecutionException; 6 7 import org.elasticsearch.action.index.IndexRequest; 8 import org.elasticsearch.action.update.UpdateRequest; 9 import org.elasticsearch.client.Client; 10 import org.elasticsearch.common.settings.Settings; 11 import org.elasticsearch.common.transport.InetSocketTransportAddress; 12 import org.elasticsearch.script.Script; 13 import org.elasticsearch.script.ScriptService; 14 import org.elasticsearch.transport.client.PreBuiltTransportClient; 15 import static org.elasticsearch.common.xcontent.XContentFactory.*; 16 17 public class ElasticsearchUpdate { 18 19 private static Client client; 20 21 /** 22 * 多種更新索引庫的方式 23 * @param args 24 */ 25 public static void main(String[] args) { 26 27 try { 28 // 設定叢集名稱 29 Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build(); 30 // 建立client 31 client = new PreBuiltTransportClient(settings) 32 .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.16.0.210"), 9300)); 33 34 } catch (Exception e) { 35 e.printStackTrace(); 36 } 37 upMethod5(); 38 39 // 關閉client 40 client.close(); 41 } 42 43 // 方法一:建立一個UpdateRequest,然後將其發送給client. 44 // 原文檔不存在時,使用此方法會報錯 45 public static void upMethod1() { 46 try { 47 UpdateRequest uRequest = new UpdateRequest(); 48 uRequest.index("flow"); 49 uRequest.type("data"); 50 uRequest.id("AVvl93FjL55UXUpfSV9X"); 51 uRequest.doc(jsonBuilder().startObject().field("inbyte", 200.0).endObject()); 52 client.update(uRequest).get(); 53 } catch (IOException e) { 54 e.printStackTrace(); 55 } catch (InterruptedException e) { 56 e.printStackTrace(); 57 } catch (ExecutionException e) { 58 e.printStackTrace(); 59 } 60 61 } 62 63 // 方法二:prepareUpdate() 使用指令碼更新索引 64 // 需要在elasticsearch.yml,新增一行:script.engine.groovy.inline.update: on,再重啟ES 65 public static void upMethod2() { 66 client.prepareUpdate("flow", "data", "2") 67 .setScript(new Script("ctx._source.inbyte = \"3.14\"", ScriptService.ScriptType.INLINE, null, null)) 68 .get(); 69 } 70 71 // 方法三:prepareUpdate() 使用doc更新索引 72 public static void upMethod3() { 73 try { 74 client.prepareUpdate("flow", "data", "AVvl93JtL55UXUpfSV9Y") 75 .setDoc(jsonBuilder().startObject().field("inbyte", 3.14).endObject()).get(); 76 } catch (IOException e) { 77 e.printStackTrace(); 78 } 79 80 } 81 82 // 方法四:增加新的欄位 83 public static void upMethod4() { 84 try { 85 UpdateRequest updateRequest = new UpdateRequest("flow","data", "AVvl93JtL55UXUpfSV9Y") 86 .doc(jsonBuilder().startObject().field("package", "100").endObject()); 87 client.update(updateRequest).get(); 88 } catch (IOException e) { 89 e.printStackTrace(); 90 } catch (InterruptedException e) { 91 e.printStackTrace(); 92 } catch (ExecutionException e) { 93 e.printStackTrace(); 94 } 95 } 96 97 // 方法五:upsert 如果文檔不存在則建立新的索引 98 // 如果存在,那麼根據UpdateRequest更新索引;如果不存在,建立indexRequest索引. 99 public static void upMethod5() {100 try {101 IndexRequest indexRequest = new IndexRequest("flow", "data", "5").source(jsonBuilder().startObject()102 .field("time", "20170508").field("package", "121").endObject());103 104 UpdateRequest uRequest2 = new UpdateRequest("flow", "data", "5").doc(105 jsonBuilder().startObject().field("time", "20170508").field("package", "12114").endObject())106 .upsert(indexRequest);107 client.update(uRequest2).get();108 } catch (IOException e) {109 e.printStackTrace();110 } catch (InterruptedException e) {111 e.printStackTrace();112 } catch (ExecutionException e) {113 e.printStackTrace();114 }115 116 }117 118 }ElasticsearchUpdate
Elasticsearch5.0 Java Api(二) -- 更新索引