不要急,总有办法的

Elasticsearch客户端链接问题之-Log

Elasticsearch | 作者 ifanjiaxing | 发布于2017年10月13日 | 阅读数:8495

1,我在使用官方API示例时遇到了LOG的问题,如下是环境及错误,请大神看看!
    1-1,pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0&quot; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/ma ... gt%3B
<modelVersion>4.0.0</modelVersion>

<groupId>com.aaaa.elastic</groupId>
<artifactId>elastic-simple-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>elastic-simple-test</name>
<url>http://maven.apache.org&lt;/url&gt;

<!--
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
-->

<dependencies>
<!--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
<scope>test</scope>
</dependency>
-->

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.6.2</version>
</dependency>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.2</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
    1-2,log4j2.properties
    
appender.console.type = Console
appender.console.name = console
appender.console.layout.type = PatternLayout

rootLogger.level = info
rootLogger.appenderRef.console.ref = console
    1-3,链接客户端
    
package com.aaaa.elastic.common;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

public class IndexApi {

private TransportClient initTransportClient() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "elastic-test").build();

TransportClient transportClient = new PreBuiltTransportClient(settings);
transportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
return transportClient;
}

private void closeTransportClient(TransportClient transportClient) {
if(null != transportClient) {
transportClient.close();
}
}

public void addIndex() throws UnknownHostException {
TransportClient transportClient = initTransportClient();
String testJson = "{\"index\":\"qianqu\",\"type\":\"employ\",\"doc\":\"001\"}";

IndexRequestBuilder indexRequestBuilder = transportClient.prepareIndex();
indexRequestBuilder.setIndex("qianqu");
indexRequestBuilder.setType("employ");
indexRequestBuilder.setId("001");
indexRequestBuilder.setSource(testJson);

IndexResponse indexResponse = indexRequestBuilder.get();
System.out.println(indexResponse.toString());
closeTransportClient(transportClient);
}

}
    1-4,测试类
package com.aaaa.elatic.test;

import java.net.UnknownHostException;

import com.aaaa.elastic.common.IndexApi;

public class IndexApiTest {

public static void main(String args) {
try {
IndexApi indexApi = new IndexApi();
indexApi.addIndex();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}

}
    1-5,出现错误
Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/netty/logging/InternalLoggerFactory
at org.elasticsearch.transport.Netty3Plugin.<clinit>(Netty3Plugin.java:46)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.elasticsearch.plugins.PluginsService.loadPlugin(PluginsService.java:426)
at org.elasticsearch.plugins.PluginsService.<init>(PluginsService.java:103)
at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:101)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:126)
at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:265)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:130)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:116)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:106)
at com.aaa.elastic.common.IndexApi.initTransportClient(IndexApi.java:18)
at com.aaa.elastic.common.IndexApi.addIndex(IndexApi.java:30)
at com.aaaa.elatic.test.IndexApiTest.main(IndexApiTest.java:12)
Caused by: java.lang.ClassNotFoundException: org.jboss.netty.logging.InternalLoggerFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 16 more

已邀请:

lry789

赞同来自: wishl

maven 打包的问题 ,兄弟这个打进去就好了
<dependency>
   <groupId>io.netty</groupId>
   <artifactId>netty</artifactId>
   <version>3.10.6.Final</version>
</dependency>

wishl

赞同来自:

<dependency>
   <groupId>io.netty</groupId>
   <artifactId>netty</artifactId>
   <version>3.10.6.Final</version>
</dependency>
这个版本的netty的InternalLoggerFactory类换成io.netty.util.internal.logging包里了,我遇到这个问题是引入的
<dependency>
   <groupId>io.netty</groupId>
   <artifactId>netty</artifactId>
   <version>3.7.0.Final</version>
</dependency>
这个版本的netty的InternalLoggerFactory类是在org.jboss.netty.logging,然后我这儿就好了,贼醉

要回复问题请先登录注册