• 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 "嘿嘿嘿";
    }

  • 相关阅读:
    348 git远程仓库:gitee(码云)与git,git clone,git push,git pull,git remote,SSH免密码登陆及配置
    347 git分支操作:创建分支,查看分支,切换分支,创建并切换分支,删除分支,合并分支,git合并冲突
    346 git基本命令:git init,git add,git commit,git status,git log,git diff,git reset,git忽视文件
    343 git基础入门:git的安装,git config配置,git三个区
    342 版本控制系统:概述,本地版本控制系统,集中式版本控制系統,分布式版本控制系統
    341 Javascript中的Return、Return false、Return true、return 变量 【转】
    340 跨域:jsonp,jquery对于jsonp的封装,跨域资源共享(CORS)
    339 同源:同源策略的基本概念,同源策略的目的,同源策略的限制范围
    338 XMLHttpRequest 2.0
    337 模板引擎artTemplate
  • 原文地址:https://www.cnblogs.com/smj1990/p/11771594.html
Copyright © 2020-2023  润新知