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();
}
}
資料成功插入

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/150245.html
標籤:Java
上一篇:springboot應用docker部署呼叫phantomjs出現permission denied修復方法
下一篇:日志系統新貴 Loki,真香!!
