zoukankan      html  css  js  c++  java
  • ElasticSearch入门之花落红尘(三)

    上篇文章散仙介绍了ElasticSearch的入门安装和使用,那么本篇我们来看下,如何使用java api来和ElasticSearch进行交互,简单点说,就是实现一个增删改查,来找找入门的感觉。 

    在这里散仙解释一下,为啥选择使用Java api来作为一个入门的例子,主要原因如下: 
    (1)Java在大中小型企业级应用非常广泛,而且ES本身就是包装了使用java编写的Apache Lucene。 
    (2)基于java的api能和其他很多基于hadoop生态系统的开源框架对接,如Hbase,Hive,Pig等。 
    (3)学习资料比较丰富,降低了入门门槛。 


    当然散仙在这里并不是来夸赞JAVA如何牛逼,而是以一个方便于大家快速学习和入门着想的,ES的最大特点就是提供了一个轻量级的RESTful的接口来对接各种编程语言,你可以使用python,php,JavaScript,ruby,C#等等任何语言来对ES进行操作和管理,甚至你也可以使用shell+curl来搞定这件事情。 


    本篇,散仙给出一个最简单的使用java api操作ES的小例子,后面文章,散仙会继续分享关于ES更多的文章,在这之前,还是想请大家注意ES的服务在启动之后,会提供2个端口来供外部使用,一个是9300基于给java程序使用的端口,另一个是9200供其他编程语言调用,以及等一些插件的访问也会使用此端口,在编写程序时,切记不能写错。 

    Java代码  收藏代码
    1. package com.test;  
    2.   
    3. import java.util.Date;  
    4. import java.util.HashMap;  
    5. import java.util.Map;  
    6. import java.util.Map.Entry;  
    7.   
    8. import org.elasticsearch.action.delete.DeleteResponse;  
    9. import org.elasticsearch.action.get.GetResponse;  
    10. import org.elasticsearch.action.index.IndexResponse;  
    11. import org.elasticsearch.action.update.UpdateRequest;  
    12. import org.elasticsearch.client.Client;  
    13. import org.elasticsearch.client.transport.TransportClient;  
    14. import org.elasticsearch.common.transport.InetSocketTransportAddress;  
    15. import org.elasticsearch.index.get.GetField;  
    16.   
    17. import com.google.gson.Gson;  
    18.   
    19. /** 
    20.  * 使用java api 操作elasticsearch索引,包含了基本的增删改查 
    21.  * @author qindongliang 
    22.  * 欢迎大家加入下面的技术交流群(广告勿入),一起探讨交流, 
    23.  * 另外也可关注我们的公众号:我是攻城师(woshigcs) 
    24.  * 搜索技术交流群:324714439  
    25.  * 大数据技术交流群:376932160  
    26.  * **/  
    27. public class ESCommon {  
    28.       
    29.     //es的客户端实例  
    30.     Client client=null;  
    31.     {  
    32.         //连接单台机器,注意ip和端口号,不能写错  
    33.         client=new TransportClient().  
    34.                 addTransportAddress(new InetSocketTransportAddress("192.168.46.23", 9300));  
    35.           
    36.     }  
    37.       
    38.     public static void main(String[] args)throws Exception {  
    39.         ESCommon es=new ESCommon();  
    40.         //es.updatedoc();  
    41.         //es.getone();  
    42.         //es.deleteOne();  
    43.         es.indexOne();  
    44.     }  
    45.       
    46.     /** 
    47.      * delete one data 
    48.      *  
    49.      * **/  
    50.     public void deleteOne(){  
    51.         try{  
    52.         DeleteResponse de=client.prepareDelete("database", "table", "2").execute().actionGet();  
    53.         if(!de.isFound()){  
    54.             System.out.println("词条数据不存在!");  
    55.         }  
    56.         System.out.println("删除成功!");  
    57.         }catch(Exception e){  
    58.             e.printStackTrace();  
    59.         }  
    60.     }  
    61.       
    62.     /** 
    63.      * index one data 
    64.      * **/  
    65.     public void updatedoc()throws Exception{  
    66.         UpdateRequest ur=new UpdateRequest();  
    67.         ur.index("database");  
    68.         ur.type("table");  
    69.         ur.id("1");  
    70.         Map<String, Object> data = new HashMap<String, Object>();  
    71.         data.put("user","更新的用户");  
    72.         data.put("message","我也要更新了呀");  
    73.         ur.doc(data);  
    74.         client.update(ur);  
    75.         System.out.println("更新成功!");  
    76.     }  
    77.       
    78.     /** 
    79.      * get one data 
    80.      * **/  
    81.     public void getone()throws Exception{  
    82.         GetResponse response = client.prepareGet("database", "table", "22")  
    83.                 .execute()  
    84.                 .actionGet();  
    85.         if(!response.isExists()){  
    86.             System.out.println("数据不存在! ");  
    87.             return;  
    88.         }  
    89.         Map<String, Object> source = response.getSource();  
    90.         for(Entry<String, Object> eo:source.entrySet()){  
    91.             System.out.println(eo.getKey()+"  "+eo.getValue());  
    92.         }  
    93.         Map<String, GetField> fields = response.getFields();  
    94.         if(fields!=null){  
    95.         for(Entry<String, GetField> s:fields.entrySet()){  
    96.             System.out.println(s.getKey());  
    97.         }  
    98.           
    99.         }else{  
    100.             System.out.println("fields is null;");  
    101.         }  
    102.         client.close();  
    103.     }  
    104.   
    105.       
    106.     /** 
    107.      * index one data 
    108.      *  
    109.      * **/  
    110.     public void indexOne()throws Exception{  
    111.           
    112.         Map<String, Object> data = new HashMap<String, Object>();  
    113.         data.put("user","kimchy");  
    114.         data.put("postDate",new Date());  
    115.         data.put("message","trying out Elasticsearch");  
    116.         Gson g=new Gson();  
    117.         String json=g.toJson(data);  
    118.         //得到一个json串  
    119.         IndexResponse ir=client.prepareIndex("database", "table", "23").setSource(json).execute().actionGet()               ;  
    120.           
    121.         String index_name=ir.getIndex();  
    122.         String index_type=ir.getType();  
    123.         String docid=ir.getId();  
    124.         long version=ir.getVersion();  
    125.           
    126.         System.out.println("索引名: "+index_name+"  ");  
    127.         System.out.println("索引类型: "+index_type+"  ");  
    128.         System.out.println("docid: "+docid+"  ");  
    129.         System.out.println("版本号: "+version+"  ");  
    130.           
    131.         client.close();  
    132.         System.out.println("连接成功!");  
    133.           
    134.     }  
    135.       
    136. }  




    好了,本篇入门小例子,就到此结束了 
    想了解更多有关电商互联网公司的搜索技术和大数据技术的使用,请欢迎扫码关注微信公众号:我是攻城师(woshigcs) 
    本公众号的内容是有关搜索和大数据技术和互联网等方面内容的分享,也是一个温馨的技术互动交流的小家园,有什么问题随时都可以留言,欢迎大家来访! 



     

  • 相关阅读:
    horizontal line and right way to code it in html, css
    Inline vs. block-level elements: a demonstration
    How wide is the default `<body>` margin?
    Getting wrong Version from Assembly using Reflection
    Where is the default size of a div element defined or calculated?
    Why padding is included in height sometimes?
    动态分析Android App之动态调试
    学习: Linux的 date 命令
    一个有趣的安全分析场景DSL设计
    Beats Elastic中的Auditbeat使用介绍
  • 原文地址:https://www.cnblogs.com/qindongliang/p/4271086.html
Copyright © 2011-2022 走看看