栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Elasticsearch Rest Client仍给IOException:打开的文件太多

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Elasticsearch Rest Client仍给IOException:打开的文件太多

从我的角度来看,您做错的事情很少,但是我将直接解决。

我不会编写完整的解决方案(实际上,我没有执行或测试任何东西),但是重要的是要理解它。另外,最好将所有与数据访问相关的内容移到另一层。
无论如何,这只是一个例子,因此设计并不完美

步骤1:导入正确的库。

几乎与您的示例相同。我更新了示例以使用版本5.6.2中推荐的最后一个客户端库

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.acervera</groupId>  <artifactId>elastic-example</artifactId>  <version>1.0-SNAPSHOT</version>  <packaging>jar</packaging>  <name>elastic-example</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    <es.version>5.6.2</es.version>  </properties>  <parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.5.4.RELEASE</version>  </parent>  <dependencies>    <!-- Spring -->    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-test</artifactId>      <scope>test</scope>    </dependency>    <!-- Elasticsearch -->    <dependency>      <groupId>org.elasticsearch</groupId>      <artifactId>elasticsearch</artifactId>      <version>${es.version}</version>    </dependency><dependency>  <groupId>org.elasticsearch.client</groupId>  <artifactId>elasticsearch-rest-high-level-client</artifactId>  <version>${es.version}</version></dependency>    <!-- Log4j -->    <dependency>      <groupId>log4j</groupId>      <artifactId>log4j</artifactId>      <version>1.2.17</version>    </dependency>  </dependencies></project>

步骤2:在bean工厂中创建并关闭客户端。

在bean工厂中,创建并销毁它。您可以重用相同的客户端。

import org.apache.http.HttpHost;import org.elasticsearch.client.RestClient;import org.elasticsearch.client.RestHighLevelClient;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import java.io.IOException;@Configurationpublic class ElasticsearchConfig {    // Here all init stuff with @Value(....)    RestClient lowLevelRestClient;    RestHighLevelClient client;    @PostConstruct    public void init() {        lowLevelRestClient = RestClient.builder(new HttpHost("host", 9200, "http")).build();        client = new RestHighLevelClient(lowLevelRestClient);    }    @PreDestroy    public void destroy() throws IOException {        lowLevelRestClient.close();    }    @Bean    public RestHighLevelClient getClient() {        return client;    }}

步骤3:使用Java Transport Client执行查询。

使用Java Transport Client执行查询。

import org.elasticsearch.action.search.SearchRequest;import org.elasticsearch.action.search.SearchResponse;import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.index.query.QueryBuilders;import org.elasticsearch.search.SearchHit;import org.elasticsearch.search.builder.SearchSourceBuilder;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import java.io.IOException;@RestController@RequestMapping("/api/v1")public class SearchController {    @Autowired    private RestHighLevelClient client;    @RequestMapping(value = "/search", method = RequestMethod.GET, produces="application/json" )    public ResponseEntity<Tweet> getSearchQueryResults(@RequestParam(value = "criteria") String criteria) throws IOException {        // This is only one example. Of course, this logic make non sense and you are going to put it in a DAO        // layer with more logical stuff        SearchRequest searchRequest = new SearchRequest();        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();        searchSourceBuilder.query(QueryBuilders.matchAllQuery());        SearchResponse searchResponse = client.search(searchRequest);        if(searchResponse.getHits().totalHits > 0) { SearchHit searchHit = searchResponse.getHits().iterator().next(); // Deserialize to Java. The best option is to use response.getSource() and Jackson // This is other option. Tweet tweet = new Tweet(); tweet.setId(searchHit.getField("id").getValue().toString()); tweet.setTittle(searchHit.getField("tittle").getValue().toString()); return ResponseEntity.ok(tweet);        } else { return ResponseEntity.notFound().build();        }    }}

另外,使用Bean构建响应。

public class Tweet {    private String id;    private String tittle;    public String getTittle() {        return tittle;    }    public void setTittle(String tittle) {        this.tittle = tittle;    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    // Here rest of bean stuff (equal, hash, etc) or Lombok}

第4步

享受Elasticsearch!

注意:Java
REST客户端[5.6]»Java高级REST客户端

PS。有必要重构该示例。只是了解方式。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/394548.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号