天天看點

ElasticSearch學習16_Elasticsearch java api 基本使用之增、删、改、查

原文來自:http://blog.csdn.net/asia_kobe/article/details/50159887

主要參考elk的Java官方文檔:https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/generate.html

一篇部落格:http://www.cnblogs.com/huangfox/p/3543134.html

Elasticsearch官方指南:http://es.xiaoleilu.com/010_Intro/30_Tutorial_Search.html

@xuguokun1986的代碼

主要概念

明白如下幾個名詞,就像上一篇中提到的那樣,索引(indices)->資料庫、類型(types)->表、文檔(documents)->行、字段(Fields)->列;其中->後面代表的是基本的關系型資料庫的表。 基本的依賴的jar包,在elasticsearch檔案夾下對應的lib的檔案夾中含有;其中json的檔案依賴jar包的下載下傳位址http://wiki.fasterxml.com/JacksonDownload。

遇到的問題及解決方法

client = new TransportClient().addTransportAddress(new InetSocketTransportAddress(“192.168.203.148”, 9200));

報錯No node available

解決方法:這裡9200 改寫成9300

代碼

建立index并插入資料

代碼如下所示: [html]  view plain  copy

  1. <pre name="code" class="html"><pre name="code" class="html">package com.asia.myTest;  
  2. import java.io.IOException;  
  3. import net.sf.json.JSONObject;  
  4. import org.elasticsearch.action.*;  
  5. import org.apache.lucene.search.TermQuery;  
  6. import org.codehaus.jackson.map.ObjectMapper;  
  7. import org.codehaus.jackson.map.util.JSONPObject;  
  8. import org.elasticsearch.ElasticsearchException;  
  9. import org.elasticsearch.action.count.CountResponse;  
  10. import org.elasticsearch.action.delete.DeleteResponse;  
  11. import org.elasticsearch.action.get.GetResponse;  
  12. import org.elasticsearch.action.index.IndexRequestBuilder;  
  13. import org.elasticsearch.action.index.IndexResponse;  
  14. import org.elasticsearch.action.search.SearchResponse;  
  15. import org.elasticsearch.action.search.SearchType;  
  16. import org.elasticsearch.client.Client;  
  17. import org.elasticsearch.client.transport.TransportClient;  
  18. import org.elasticsearch.common.transport.InetSocketTransportAddress;  
  19. import org.elasticsearch.common.xcontent.XContentFactory;  
  20. import org.elasticsearch.index.query.FilterBuilders;  
  21. import org.elasticsearch.index.query.QueryBuilder;  
  22. import org.elasticsearch.index.query.QueryBuilders;  
  23. import org.elasticsearch.index.query.QueryBuilders.*;  
  24. import static org.elasticsearch.index.query.FilterBuilders.*;  
  25. public class ESClient {  
  26.     private Client client;  
  27.     public void init(){  
  28.         //on start相當于連接配接叢集  
  29.         client = new TransportClient().  
  30.                  addTransportAddress(new InetSocketTransportAddress("192.168.203.148", 9300));  
  31.     }     
  32.     public void close(){  
  33.         //on shutdown 斷開叢集  
  34.         client.close();  
  35.     }  
  36.     public void createIndex() {  
  37.         for (int i=0; i<=200;i++){  
  38.             IndexResponse indexResponse = null;  
  39.             try {  
  40.                 indexResponse = client.prepareIndex("logs", "log2015",i+"")  
  41.                                                     .setSource(  
  42.                                                     XContentFactory.jsonBuilder().startObject()  
  43.                                                         .field("sourceIp" , "10.10.16."+i)  
  44.                                                         .field("sourcePort" , 389)  
  45.                                                         .field("destIp" , "114.114.114.114")  
  46.                                                         .endObject())  
  47.                                                         .execute()  
  48.                                                         .actionGet();  
  49.             } catch (ElasticsearchException e) {  
  50.                 // TODO Auto-generated catch block  
  51.                 e.printStackTrace();  
  52.             } catch (IOException e) {  
  53.                 // TODO Auto-generated catch block  
  54.                 e.printStackTrace();  
  55.             }  
  56.             System.out.println("responseIsCreated: "+indexResponse.isCreated());  
  57.         }  
  58.         System.out.println("it is ok !");  
  59.     }  
  60.     public void get(){  
  61.         GetResponse getresponse = client.prepareGet("logs", "log2015", "1")  
  62.                                      .execute()  
  63.                                      .actionGet();  
  64.         System.out.println(getresponse.getSourceAsString());  
  65.     }  
  66.     public void delete(){  
  67.         DeleteResponse deleteresponse = client.prepareDelete("logs", "log2015","150")  
  68.                                                .execute()  
  69.                                                .actionGet();  
  70.         System.out.println(deleteresponse.getVersion());  
  71.     }  
  72.     public void search(){  
  73.         SearchResponse searchresponse = client.prepareSearch("logs")  
  74.                                         .setTypes("log2015")  
  75.                                         .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)  
  76.                                         .setQuery(QueryBuilders.termQuery("destIp", "114.114.114.114"))  
  77.                                         .setPostFilter(  
  78.                                                 FilterBuilders.rangeFilter("sourceIp")  
  79.                                                 .from("10.10.16.57")  
  80.                                                 .to("10.10.16.68")  
  81.                                                 )  
  82.                                         .setFrom(0)  
  83.                                         .setSize(3).setExplain(true)  
  84.                                         .execute().actionGet();  
  85.         System.out.println(searchresponse.toString());   
  86. //      JSONObject jsonObject = JSONObject.fromObject(searchresponse.toString());  
  87. //      JSONObject hites = (JSONObject) jsonObject.get("hits");  
  88. //      System.out.println(hites.get("hits").toString());  
  89.     }  
  90.     public void count(){  
  91.         CountResponse countresponse = client.prepareCount("website")  
  92.                 .setQuery(QueryBuilders.termQuery("_type", "asia"))  
  93.                 .execute()  
  94.                 .actionGet();  
  95.         System.out.println(countresponse.getCount());  
  96.     }  
  97.     public static void main(String[] args){  
  98.         ESClient client = new ESClient();  
  99.         client.init();  
  100.         //client.createIndex();  
  101.         //client.get();  
  102.         //client.delete();  
  103.         //client.search();  
  104.         client.count();  
  105.         client.close();  
  106.     }  
  107. }  

控制台運作過程: ....... responseIsCreated: true responseIsCreated: true

responseIsCreated: true

responseIsCreated: true

responseIsCreated: true

responseIsCreated: true

responseIsCreated: true

it is ok !

通過CURL指令檢視基本結果,插入資料成功:

ElasticSearch學習16_Elasticsearch java api 基本使用之增、删、改、查

繼續閱讀