zoukankan      html  css  js  c++  java
  • springboot~集成elasticsearch的jest

    jest是一批操作es的http api接口,你可以像使用普法方法一下操作es,在springboot2.3.0之前,JestClient是支持自动注入的,而在2.3.0之后,你必须为JestClient写一个组件类,通过注入组件类来使用jest,这一点有些麻烦了。
    依赖包

            <dependency>
                <groupId>io.searchbox</groupId>
                <artifactId>jest</artifactId>
                <version>5.3.3</version>
            </dependency>
            <dependency>
                <groupId>org.elasticsearch</groupId>
                <artifactId>elasticsearch</artifactId>
                <version>5.6.7</version>
            </dependency>
    
    

    需要定义注册类

    /**
     * springboot2.3.0之后不支持自动注册,只能手动写注册配置文件.
     */
    @Component
    public class JestClientConfig {
        @Bean
        public io.searchbox.client.JestClient getJestCline() {
            JestClientFactory factory = new JestClientFactory();
            factory.setHttpClientConfig(new HttpClientConfig
                    .Builder("http://localhost:9200")
                    .multiThreaded(true)
                    .build());
            return factory.getObject();
        }
    }
    
    

    在程序中使用它

        @Autowired
        JestClient jestClient;
        /**
         * 创建所引
         *
         * @throws IOException
         */
        @Test
        public void createIndex() throws IOException {
            User user = new User("1", "张三", "test");
            Index index = new Index.Builder(user).index(INDEX).type(TYPE).build();
            try {
                JestResult jr = jestClient.execute(index);
                System.out.println(jr.isSucceeded());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    数据成功插入
    es

  • 相关阅读:
    Python 基础(二)
    3.6:手写代码题(包含sql题)
    3.2:负载均衡、集群相关
    3.1:并发、安全与性能调优
    2.6:Linux/Shell脚本
    2.5:Git/Svn
    2.4:缓存
    2.3:消息中间件
    2.2:数据库
    2.1:常用框架
  • 原文地址:https://www.cnblogs.com/lori/p/13255199.html
Copyright © 2011-2022 走看看