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.   
  3. import java.io.IOException;  
  4.   
  5. import net.sf.json.JSONObject;  
  6.   
  7. import org.elasticsearch.action.*;  
  8. import org.apache.lucene.search.TermQuery;  
  9. import org.codehaus.jackson.map.ObjectMapper;  
  10. import org.codehaus.jackson.map.util.JSONPObject;  
  11. import org.elasticsearch.ElasticsearchException;  
  12. import org.elasticsearch.action.count.CountResponse;  
  13. import org.elasticsearch.action.delete.DeleteResponse;  
  14. import org.elasticsearch.action.get.GetResponse;  
  15. import org.elasticsearch.action.index.IndexRequestBuilder;  
  16. import org.elasticsearch.action.index.IndexResponse;  
  17. import org.elasticsearch.action.search.SearchResponse;  
  18. import org.elasticsearch.action.search.SearchType;  
  19. import org.elasticsearch.client.Client;  
  20. import org.elasticsearch.client.transport.TransportClient;  
  21. import org.elasticsearch.common.transport.InetSocketTransportAddress;  
  22. import org.elasticsearch.common.xcontent.XContentFactory;  
  23. import org.elasticsearch.index.query.FilterBuilders;  
  24. import org.elasticsearch.index.query.QueryBuilder;  
  25. import org.elasticsearch.index.query.QueryBuilders;  
  26. import org.elasticsearch.index.query.QueryBuilders.*;  
  27.   
  28. import static org.elasticsearch.index.query.FilterBuilders.*;  
  29.   
  30.   
  31. public class ESClient {  
  32.       
  33.     private Client client;  
  34.       
  35.     public void init(){  
  36.         //on start相当于连接集群  
  37.         client = new TransportClient().  
  38.                  addTransportAddress(new InetSocketTransportAddress("192.168.203.148", 9300));  
  39.     }     
  40.       
  41.     public void close(){  
  42.         //on shutdown 断开集群  
  43.         client.close();  
  44.     }  
  45.       
  46.     /*  
  47.      *创建index,把其中的文档转化为json的格式存储  
  48.     */  
  49.     public void createIndex() {  
  50.         for (int i=0; i<=200;i++){  
  51.             IndexResponse indexResponse = null;  
  52.             try {  
  53.                 indexResponse = client.prepareIndex("logs", "log2015",i+"")  
  54.                                                     .setSource(  
  55.                                                     XContentFactory.jsonBuilder().startObject()  
  56.                                                         .field("sourceIp" , "10.10.16."+i)  
  57.                                                         .field("sourcePort" , 389)  
  58.                                                         .field("destIp" , "114.114.114.114")  
  59.                                                         .endObject())  
  60.                                                         .execute()  
  61.                                                         .actionGet();  
  62.             } catch (ElasticsearchException e) {  
  63.                 // TODO Auto-generated catch block  
  64.                 e.printStackTrace();  
  65.             } catch (IOException e) {  
  66.                 // TODO Auto-generated catch block  
  67.                 e.printStackTrace();  
  68.             }  
  69.             System.out.println("responseIsCreated: "+indexResponse.isCreated());  
  70.         }  
  71.         System.out.println("it is ok !");  
  72.     }  
  73.       
  74.     /*  
  75.      * Get index 获取文档相当于读取数据库的一行数据  
  76.      */  
  77.     public void get(){  
  78.         GetResponse getresponse = client.prepareGet("logs", "log2015", "1")  
  79.                                      .execute()  
  80.                                      .actionGet();  
  81.         System.out.println(getresponse.getSourceAsString());  
  82.     }  
  83.       
  84.     /*  
  85.      *Delete index 删除文档,相当于删除一行数据  
  86.      */  
  87.     public void delete(){  
  88.         DeleteResponse deleteresponse = client.prepareDelete("logs", "log2015","150")  
  89.                                                .execute()  
  90.                                                .actionGet();  
  91.         System.out.println(deleteresponse.getVersion());  
  92.     }  
  93.       
  94.     /*  
  95.      *search 查询相当于关系型数据库的查询   
  96.     */  
  97.     public void search(){  
  98.         SearchResponse searchresponse = client.prepareSearch("logs")  
  99.                                         .setTypes("log2015")  
  100.                                         .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)  
  101.                                         .setQuery(QueryBuilders.termQuery("destIp", "114.114.114.114"))  
  102.                                         .setPostFilter(  
  103.                                                 FilterBuilders.rangeFilter("sourceIp")  
  104.                                                 .from("10.10.16.57")  
  105.                                                 .to("10.10.16.68")  
  106.                                                 )  
  107.                                         .setFrom(0)  
  108.                                         .setSize(3).setExplain(true)  
  109.                                         .execute().actionGet();  
  110.           
  111.         System.out.println(searchresponse.toString());   
  112. //      JSONObject jsonObject = JSONObject.fromObject(searchresponse.toString());  
  113. //      JSONObject hites = (JSONObject) jsonObject.get("hits");  
  114. //      System.out.println(hites.get("hits").toString());  
  115.     }  
  116.       
  117.     /*  
  118.      *Count api 统计分析结果  
  119.      */  
  120.     public void count(){  
  121.         CountResponse countresponse = client.prepareCount("website")  
  122.                 .setQuery(QueryBuilders.termQuery("_type", "asia"))  
  123.                 .execute()  
  124.                 .actionGet();  
  125.       
  126.         System.out.println(countresponse.getCount());  
  127.       
  128.     }  
  129.       
  130.       
  131.       
  132.     public static void main(String[] args){  
  133.         ESClient client = new ESClient();  
  134.         client.init();  
  135.         //client.createIndex();  
  136.         //client.get();  
  137.         //client.delete();  
  138.         //client.search();  
  139.         client.count();  
  140.         client.close();  
  141.           
  142.           
  143.     }  
  144. }  


 
  
 
  
控制台运行过程:
.......
responseIsCreated: true
responseIsCreated: true
responseIsCreated: true
responseIsCreated: true
responseIsCreated: true
responseIsCreated: true
responseIsCreated: true
it is ok !
通过CURL命令查看基本结果,插入数据成功:





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值