zoukankan      html  css  js  c++  java
  • es 相关

    1.安装 es

    2.安装 grunt

    3.安装 logstash

    4.安装 kibana

    5.引入 jar 包
    <dependency>
      <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>7.3.1</version>
        <exclusions>
          <exclusion>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
          </exclusion>
          <exclusion>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
          </exclusion>
        </exclusions>
    </dependency>

    <dependency>
      <groupId>org.elasticsearch.client</groupId>
      <artifactId>elasticsearch-rest-client</artifactId>
      <version>7.3.1</version>
    </dependency>

    <dependency>
      <groupId>org.elasticsearch</groupId>
      <artifactId>elasticsearch</artifactId>
      <version>7.3.1</version>
    </dependency>

    6.启动类中注入 bean

    @Bean
    public RestHighLevelClient client(){
      RestHighLevelClient client=new RestHighLevelClient(
        RestClient.builder(
        new HttpHost("localhost",9200,"http")
        //这里如果要用client去访问其他节点,就添加进去
        )
      );
      return client;
    }

    7.使用 client 进行增删改查

    /**
    * 新增
    * @param user
    * @return
    */
    @RequestMapping("/addEsUser")
    @ResponseBody
    public String addEsUser(@RequestBody User user){
      XContentBuilder builder=null;
      IndexRequest request=new IndexRequest("user");
      String id = "smj";
      try {
        builder = XContentFactory.jsonBuilder();
        builder.startObject()
        .field("userName",user.getUserName())
        .field("userPassword",user.getUserPassword())
        .field("create_time",new Date())
        .field("creator","smj")
        .endObject();
        request.id(id).opType("create").source(builder);
        IndexResponse response=client.index(request, RequestOptions.DEFAULT);
        logger.info("返回结果为:" + JSON.toJSONString(response.getResult()));
      } catch (IOException e) {
        e.printStackTrace();
      }
      return "嘿嘿嘿";
    }

    /**
    * 查询
    * @param id
    * @return
    */
    @RequestMapping("/getEsUser")
    @ResponseBody
    public String getEsUser(String id){
      GetRequest getRequest=new GetRequest("user",id);
      Map map=new HashMap();
      GetResponse response=null;
      try{
        response= client.get(getRequest, RequestOptions.DEFAULT);

      } catch (IOException e) {
        e.printStackTrace();
      }
      if(response.isExists()){
        map.put("success",true);
        map.put("data",response.getSource());
      }else{
        map.put("success",false);
      }
      return JSON.toJSONString(map);
    }

    /**
    * 删除
    * @param id
    * @return
    */
    @RequestMapping("/delEsUser")
    @ResponseBody
    public String delEsUser(String id){
      XContentBuilder builder=null;
      DeleteRequest request=new DeleteRequest("user",id);
      DeleteResponse response = null;
      try {
        response = client.delete(request, RequestOptions.DEFAULT);
        logger.info("返回结果为:" + JSON.toJSONString(response.getResult()));
      } catch (IOException e) {
        e.printStackTrace();
      }
      return JSON.toJSONString(response.getResult());
    }

    /**
    * 修改
    * @param user
    * @return
    */
    @RequestMapping("/updateEsUser")
    @ResponseBody
    public String updateEsUser(@RequestBody User user){
      XContentBuilder builder=null;
      UpdateRequest request = new UpdateRequest("user","smj");
      try {
        builder = XContentFactory.jsonBuilder();
        builder.startObject()
        .field("userName",user.getUserName())
        .field("userPassword",user.getUserPassword())
        .field("create_time",new Date())
        .field("creator","smj")
        .endObject();
        request.doc(builder);
        UpdateResponse response=client.update(request, RequestOptions.DEFAULT);
        logger.info("返回结果为:" + JSON.toJSONString(response.getResult()));
      } catch (IOException e) {
        e.printStackTrace();
      }
      return "嘿嘿嘿";
    }

  • 相关阅读:
    动态封杀与解封IP
    潜谈IT从业人员在传统IT和互联网之间的择业问题(下)-互联网公司
    潜谈IT从业人员在传统IT和互联网之间的择业问题(上)-传统乙方形公司
    博主2000年真实遇鬼记-仅作记录以供后人参考
    异步请求引发的Chrome死锁
    jboss规则引擎KIE Drools 6.3.0 Final 教程(3)
    jboss规则引擎KIE Drools 6.3.0 Final 教程(2)
    jboss规则引擎KIE Drools 6.3.0 Final 教程(1)
    商品搜索引擎---推荐系统设计
    大道理很多人都懂,只是坚持不下去(浅谈坚持)
  • 原文地址:https://www.cnblogs.com/smj1990/p/11771594.html
Copyright © 2011-2022 走看看