新手学习elasticsearch 6.1.0,使用gradle来构建项目
build.gradle:
dependencies {
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.elasticsearch:elasticsearch:6.1.0'
compile 'org.elasticsearch.client:transport:6.1.0'
compile 'io.springfox:springfox-swagger2:2.7.0'
compile 'io.springfox:springfox-swagger-ui:2.7.0'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'com.google.guava:guava:23.5-jre'
runtime 'org.springframework.boot:spring-boot-devtools'
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'junit:junit:4.12'
providedCompile 'org.projectlombok:lombok:1.16.18'
}
使用的也是安装es对应版本的jar
测试用例:
@Test
public void createDoc() {
ObjectMapper objectMapper = new ObjectMapper();
TransportClient client = null;
User user = new User();
user.setAddress("beijingshi");
user.setAge(28);
user.setName("alvin");
try {
Settings settings = Settings.builder()
.put("cluster.name", "my-application").build();
client = new PreBuiltTransportClient(settings);
IndexResponse indexResponse = client.prepareIndex("my_index", "user").setSource(user).get();
System.out.println(indexResponse.getId());
System.out.println(objectMapper.writeValueAsString(indexResponse.getResult()));
System.out.println(objectMapper.writeValueAsString(indexResponse.getShardInfo()));
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (Objects.nonNull(client)) {
client.close();
}
}
}
运行报错,网上也没有查到是什么导致的,所以来此提问
java.lang.NoClassDefFoundError: org/elasticsearch/common/transport/InetSocketTransportAddress
at org.elasticsearch.transport.Netty4Plugin.getSettings(Netty4Plugin.java:55)
at org.elasticsearch.plugins.PluginsService.lambda$getPluginSettings$0(PluginsService.java:82)
at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:267)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.elasticsearch.plugins.PluginsService.getPluginSettings(PluginsService.java:82)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:135)
at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:262)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:128)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:114)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:104)
at com.alvin.es.JunitTest.createDoc(JunitTest.java:29)