Elasticsearch版本2.4.1创建索引报错

之前测试了1.7的版本,然后又下载了2.4.1的安装包进行安装。
在java的pom.xml中添加引用:

这里写代码片
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>2.4.1</version>
</dependency>

然后运行代码:

/**
 * 
 */


import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
/**
 * @author diaoxingguo
 *
 */
public class Test {
	
	public static void main(String[] args) {
		
		//创建客户端
		try {
			Client client = TransportClient.builder().build()
					.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
			
			
			System.out.println("连接成功");
			List<String> list = getData();
			for(String str : list){
				IndexResponse response = client.prepareIndex("blog","article").setSource(str).get();
				if(response.isCreated()){
					System.out.println("创建成功!");
				}
			}
			
			client.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		
	}
	
	
	public static List<String> getData(){
		List<String> list = new ArrayList<String>();
		
		String blog1 = model2Json(new Blog(1,"第1个title","2016-10-14 10:31","第1个content"));
		String blog2 = model2Json(new Blog(2,"第2个title","2016-10-14 10:32","第2一个content"));
		String blog3 = model2Json(new Blog(3,"第3个title","2016-10-14 10:33","第3个content"));
		String blog4 = model2Json(new Blog(4,"第4个title","2016-10-14 10:34","第4个content"));
		String blog5 = model2Json(new Blog(5,"第5个title","2016-10-14 10:35","第5个content"));
		String blog6 = model2Json(new Blog(6,"第6个title","2016-10-14 10:36","6个content"));
		String blog7 = model2Json(new Blog(7,"第7个title","2016-10-14 10:37","第7个content"));
		String blog8 = model2Json(new Blog(8,"第8个title","2016-10-14 10:36","8个content"));
		
		list.add(blog1);
		list.add(blog2);
		list.add(blog3);
		list.add(blog4);
		list.add(blog5);
		list.add(blog6);
		list.add(blog7);
		list.add(blog8);
		
		return list;
	}
	
	 public static String model2Json(Blog blog) {
	        String jsonData = null;
	        try {
	            XContentBuilder jsonBuild = XContentFactory.jsonBuilder();
	            jsonBuild.startObject()
	            			.field("id", blog.getId())
	            			.field("title", blog.getTitle())
	            			.field("posttime", blog.getPostTime())
	            			.field("content",blog.getContent())
	                    .endObject();

	            jsonData = jsonBuild.string();
	            System.out.println(jsonData);

	        } catch (IOException e) {
	            e.printStackTrace();
	        }

	        return jsonData;
	    }
}

结果,报错,信息如下:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.util.concurrent.MoreExecutors.directExecutor()Ljava/util/concurrent/Executor;
	at org.elasticsearch.threadpool.ThreadPool.<clinit>(ThreadPool.java:190)
	at org.elasticsearch.client.transport.TransportClient$Builder.build(TransportClient.java:131)
	at test.es.Test.main(Test.java:27)

在pom.xml引用

<dependency>
   <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>18.0</version>
</dependency>

运行,结果又报错,信息如下:

Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/filter/TokenFilter
	at org.elasticsearch.common.xcontent.json.JsonXContent.createGenerator(JsonXContent.java:73)
	at org.elasticsearch.common.xcontent.XContentBuilder.<init>(XContentBuilder.java:113)
	at org.elasticsearch.common.xcontent.XContentBuilder.<init>(XContentBuilder.java:103)
	at org.elasticsearch.common.xcontent.XContentBuilder.builder(XContentBuilder.java:81)
	at org.elasticsearch.common.xcontent.json.JsonXContent.contentBuilder(JsonXContent.java:38)
	at org.elasticsearch.common.xcontent.XContentFactory.contentBuilder(XContentFactory.java:122)
	at org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder(XContentFactory.java:49)
	at test.es.Test.model2Json(Test.java:79)
	at steel.test.es.Test.getData(Test.java:55)
	at steel.test.es.Test.main(Test.java:32)
Caused by: java.lang.ClassNotFoundException: com.fasterxml.jackson.core.filter.TokenFilter
	at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
	... 10 more

引用相应版本的jackson,注意,如果引用版本和当前依赖相一致,我引用的是:

<dependency>  
     <groupId>com.fasterxml.jackson.core</groupId>  
     <artifactId>jackson-core</artifactId>  
     <version>2.8.1</version>  
 </dependency>

添加后运行正常。当然当中也遇到的其他的一些报错,比如:

java.lang.NoSuchFieldError: FAIL_ON_SYMBOL_HASH_OVERFLOW

这个是jackson引用的版本不一致,可以看maven中已有的相关jackson版本,然后引用同样的版本。
还有一个报错:

NoNodeAvailableException[None of the configured nodes are available

这个是我创建client时的ip写成了自己的网络ip,改为127.0.0.1就成功了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值